Search code examples
c#typescreateinstance

Use Type.GetType() to get the type and how to new an object in this typical type?


 Type typesample = packet.GetType();

Then I want to new an object in the type of typesample


Solution

  • You can instantiate the type by using the Activator class:

    var item = Activator.CreateInstance(typesample);