C# FAQ: How create an instance of a type using only its name

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How create an instance of a type using only its name?

Firstly, get a Type reference for the type. If the type to be instantiated is in either the current assembly or mscorlib, simply use Type.GetType(name). If the type is in a different assembly, there are two options: either call Type.GetType passing in the full type name including assembly information; or, find or load the assembly then call Assembly.GetType(name) using the assembly reference.

With the Type reference, use either Activator.CreateInstance(type) to create an instance; or, call Type.GetConstructor to get a specific constructor which can then be used instantiate by calling Invoke.



Personal tools