How to Instantiate a Generic Type With Multiple Input Parameters?

No Instead, you can accept a delegate that creates them for you: GenericMethod(int a, int b, Func creator) { T t = creator(a, b); } GenericMethod(8, 9, (a, b) => new YourType(a, b)) You could also store these creators in a generic static class : static class Creator { public static Func Func { get; set; } } GenericMethod(int a, int b) { T t = Creator. Func(a, b); } Creator. Func = (a, b) => new YourType(a, b).

No. Instead, you can accept a delegate that creates them for you: GenericMethod(int a, int b, Func creator) { T t = creator(a, b); } GenericMethod(8, 9, (a, b) => new YourType(a, b)); You could also store these creators in a generic static class: static class Creator { public static Func Func { get; set; } } GenericMethod(int a, int b) { T t = Creator. Func(a, b); } Creator.

Func = (a, b) => new YourType(a, b).

Aka the factory pattern. ;) – Sven Jun 16 at 15:16 I quite like the static generic class approach, thanks. Does it have the highest performance in comparison with other approaches?

– William Jun 16 at 22:30.

If you don't want to use Activator, you could use an expression tree. Create an Instance of a Class with Expression Trees.

To make that worth doing, you need to store the compiled expression tree (eg, in a static generic dictionary, as in my answer) so that you can reuse it later. Otherwise, it will end up being even slower. – SLaks?

Jun 16 at 15:27 bloggingabout. Net/blogs/vagif/archive/2010/04/02/… Is this approach the fastest one? – William Jun 17 at 6:39 This seems to be the best way; thanks.

– William Jun 18 at 12:14.

Create your factory class: public static class TypeFactory { private static Func Func { get; set; } static TypeFactory() { TypeFactory. Func = (a, b) => new Type1(a, b); TypeFactory. Func = (a, b) => new Type2(a, b); } public static T Create(int a, int b) { return Func(a, b); } } Then use it like this: var type1 = TypeFactory.

Create(1, 2); var type2 = TypeFactory. Create(1, 2).

You shouldn't initialize it in the class itself. Your initializer will run once per type parameter. – SLaks?

Jun 17 at 1:38 what would you suggest then? I can use a lock inside the class to make sure the static constructor only runs once but I doubt it will gain any performance benefit over Activator.CreateInstance()? – William Jun 17 at 7:57 1 Move the code in the static ctor to a non-generic type.

– SLaks? Jun 17 at 12:10 have you got a code sample for this? I can't just move the static ctor out of the class.

I mean I can move it but when should it be called and from where? I'd need it to be called only once throughout the application. I can put it in the Application_Start event!

– William Jun 17 at 19:24.

Public static class MyTypeFactory { static MyTypeFactory() { MethodRunner. Func = (a, b) => new Type1(a, b); MethodRunner. Func = (a, b) => new Type2(a, b); } public static T Create(int a, int b) { return MethodRunner.

Func(a, b); } static class MethodRunner { public static Func Func { get; set; } } } This looks promising?! Is a static ctor thread-safe by nature (CLR) like static field initializers?

Yes. Static field initializers are compiled into the static ctor. – SLaks?

Jul 1 at 13:33.

This is a good post: pooyakhamooshi.blogspot.com/2011/06/how-....

In theory, you need to use a generic type constraint. However, the only constructor constraint available is support for a parameterless constructor where T : new(). If Typ1 and Typ2 share a base class which defines properties using the 2 integers or both support an interface guaranteeing setters for those integers you could define a parameterless constructor on each class and use an additional constraint to allow later access to the properties.

A constraint of T to the base type would not open up the constructors on T. For one, there is no guarantee that for a base with a parameterized constructor that the derived children also provide parameterized constructors. – Anthony Pegram Jun 16 at 15:11 Even with a new() constraint, the compiler just translates that into a call to Activator.CreateInstance(), so there's no performance gain.

The only thing you gain is compile time checking for the presence of a constructor. – Sven Jun 16 at 15:17 @Sven, where do you get that? I see no such evidence in the IL or the C# language specification on the constraint.

– Anthony Pegram Jun 16 at 15:23 I'm not sure if it's required behaviour by the specification, but it is what both the Microsoft and Mono C# compilers do; you can check it with Reflector of ILSpy or something similar. – Sven Jun 16 at 15:26 @Anthony: I belive Sven is correct; I tried it a while ago. – SLaks?

Jun 16 at 15:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions