Dynamically create an object of <Type>

96,371

Solution 1

This link should help:
https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance

Activator.CreateInstance will create an instance of the specified type.

You could wrap that in a generic method like this:

public T GetInstance<T>(string type)
{
    return (T)Activator.CreateInstance(Type.GetType(type));
}

Solution 2

If the type is known by the caller, there's a better, faster way than using Activator.CreateInstance: you can instead use a generic constraint on the method that specifies it has a default parameterless constructor.

Doing it this way is type-safe and doesn't require reflection.

T CreateType<T>() where T : new()
{
   return new T();
}

Solution 3

public static T GetInstance<T>(params object[] args)
{
     return (T)Activator.CreateInstance(typeof(T), args);
}

I would use Activator.CreateInstance() instead of casting, as the Activator has a constructor for generics.

Solution 4

You want to use Activator.CreateInstance.

Here is an example of how it works:

using System;
using System.Runtime.Remoting;

class Program
{
    static void Main()
    {
        ObjectHandle o = Activator.CreateInstance("mscorlib.dll", "System.Int32");

        Int32 i = (Int32)o.Unwrap();
    }
}

Solution 5

Here is a function I wrote that clones a record of type T, using reflection. This is a very simple implementation, I did not handle complex types etc.

 public static T Clone<T>(T original)
    {
        T newObject = (T)Activator.CreateInstance(original.GetType());

        foreach (var prop in original.GetType().GetProperties())
        {
            prop.SetValue(newObject, prop.GetValue(original));
        }

        return newObject;
    }

I hope this can help someone.

Assaf

Share:
96,371
thaBadDawg
Author by

thaBadDawg

Updated on July 09, 2022

Comments

  • thaBadDawg
    thaBadDawg almost 2 years

    I have a table in my database that I use to manage relationships across my application. it's pretty basic in it's nature - parentType,parentId, childType, childId... all as ints. I've done this setup before, but I did it with a switch/case setup when I had 6 different tables I was trying to link. Now I have 30 tables that I'm trying to do this with and I would like to be able to do this without having to write 30 case entries in my switch command.

    Is there a way that I can make reference to a .Net class using a string? I know this isn't valid (because I've tried several variations of this):

    Type t = Type.GetType("WebCore.Models.Page");
    object page = new t();
    

    I know how to get the Type of an object, but how do I use that on the fly to create a new object?

  • Jason Miesionczek
    Jason Miesionczek over 15 years
    reflection is required because the OP specified using a string to identify the Type.
  • Andrew Hare
    Andrew Hare over 15 years
    Also in order to call that method then the OP must have access to the type at compile time - the original question asked how to create an instance at execution time from a string.
  • Jason Miesionczek
    Jason Miesionczek over 15 years
    i am merely basing my answer on the example provided in the question.
  • Andrew Hare
    Andrew Hare over 15 years
    Ah you are right - OP does appear to have access to the type! My mistake - (-1) removed!
  • Jason Miesionczek
    Jason Miesionczek over 15 years
    Your example would require knowledge of the type at compile time as well :)
  • Tod
    Tod over 6 years
    I get this error: "The type arguments for method 'GetInstance<T>(string)' cannot be inferred from the usage. Try specifying the type arguments explicitly". If I'm specifying the type arguments, that kinda defeats the purpose of this.
  • Jason Miesionczek
    Jason Miesionczek over 6 years
    One possible use case of this function is if you have an object hierarchy with an Interface as the root. You could specify the interface as the explicit type, and then pass in the concrete class that implements that interface as the function parameter.