0
Answer

How can we create an instance of a type referred to by an object of System.Type?

nikhilwason

nikhilwason

16y
1.7k
1

I am using the System.Reflection namespace to interpret DLLs. Lets assume that I have an object of MethodInfo (say miTest) which refers to a particular method in an assembly. Now the return type of this method is:

miTest.ReturnType

which is an object of the type System.Type.

I can also get the name of the return type as a string by using miTest.ReturnType.Name.

Is it possible to create an instance of the type referred to by miTest.ReturnType?

For example,

object obj = miTest.Invoke(null, null);

Now I want to store the value of obj in an object of the type referred to by miTest.ReturnType. So, if miTest returns an int, then I should be able to create an integer by using miTest.ReturnType. Is it possible to do so? Lets restrict this functionality only to primitive types for the moment.


--
Nikhil Wason