3 ms·
I'm not sure what you are asking for, but you can find the generic type arguments of any object at runtime: Code snippet: public class Foo<T>{} Foo<int> f
by n00b101 10y ago
I'm not sure what you are asking for, but you can find the generic type arguments of any object at runtime:
Code snippet:
public class Foo<T>{}
Foo<int> foo = new Foo<int>();
Console.WriteLine(foo.GetType().GetGenericArguments()[0]);
Output:
System.Int32
Complete code: http://rextester.com/RAQO69918 http://rextester.com/RAQO69918
- eropple 10y agoI think he may mean that a lot of reflection methods take Type rather than a generic parameter? A lot of those have generic overloads now though.
- naasking 10y agoWhat would really make reflection fast is the dual operation of typeof(T) -> Type, that is, Type -> T. Then you can do all kinds of neat but efficient things by exploiting the CLR's runtime generics [1]. This is something that would be easily done in the CLR itself, a sort of hidden universal method that could perform a single, maybe double, dispatch into the correct generic overload, but alas, we are left only to simulate this using poor imitations of inline caches. [1] https://github.com/naasking/Dynamics.NET https://github.com/naasking/Dynamics.NET
- matthewwarren 10y agoCool, Dynamics.NET look like a cool library, I'd not heard of it before, thanks
- __s 10y agoThat is what I meant. I may be a bit outdated with C# versions, still working with 4.5