Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling Type.MakeGenericType() with null arguments

Tags:

c#

generics

I have a generic type:

MyType<T1, T2, T3>

and i want to do this:

typeof(MyType<,,>).MakeGenericType(new [] { null, null, string});

so i end up with:

MyType<,,string>

But, you can't pass null types into MakeGenericType (see: http://msdn.microsoft.com/en-us/library/system.type.makegenerictype.aspx).

How do I achieve this?

Thanks

like image 463
Andrew Bullock Avatar asked May 27 '26 05:05

Andrew Bullock


1 Answers

Ok I avoided it like this:

var args = typeof(MyType<,,>).GetGenericArguments();
args[2] = typeof(string);
typeof(MyType<,,>).MakeGenericType(args);
like image 82
Andrew Bullock Avatar answered May 28 '26 17:05

Andrew Bullock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!