Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get type name of T using reflection [duplicate]

Tags:

c#

reflection

I have:

public class MyUserControl : WebUserControlBase <MyDocumentType>{...}

How do I get the TypeName of MyDocumentType if I'm in another class?

like image 559
Karlo Medallo Avatar asked Mar 20 '13 09:03

Karlo Medallo


People also ask

How do you find a parameter type?

You can access the type of a specific parameter by using square brackets, in the same way you would access an array element at index. Here is an example of how you would use the Parameters utility type for 2 functions that take the same object as a parameter. Copied!

How do I use reflection to call a generic method?

The first step to dynamically invoking a generic method with reflection is to use reflection to get access to the MethodInfo of the generic method. To do that simply do this: var methodInfo = typeof(ClassWithGenericMethod). GetMethod("MethodName");

Does GetType use reflection?

The most basic way to do reflection is to use the GetType() method, but we can also use reflection to get information about methods, constructors, properties, and more.

What is Reflaction C#?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.


1 Answers

You can use something like this:

typeof(MyUserControl).BaseType.GetGenericArguments()[0]
like image 51
Daniel Hilgarth Avatar answered Oct 28 '22 08:10

Daniel Hilgarth