How do I create an instance from a string in C#?

  • You need to specify the full type name to Type.GetType(), including namespace, e.g. “Company.Project2.Type”
  • If the type isn’t in the same assembly (or mscorlib), you need to give the assembly name too, including version information if it’s strongly typed. For example, for a non-strongly typed assembly Company.Project2.dll, you might specify “Company.Project2.Type, Company.Project2”.
  • To call a constructor with parameters you can call Activator.CreateInstance(Type, Object[]) or get the exact constructor you want with Type.GetConstructor() and then call ConstructorInfo.Invoke().

If that doesn’t help, please give more information.

Leave a Comment