How to use class name as parameter in C#

C#

C# Problem Overview


what i want to do is to automatically create some object.

In Java, class can be pass as parameter, for example

Class A{

}


Object createObjectBy(class clazz){
       // .. do construction work here
}

when using it, just ---> createObjectBy(A.class)

it is benefit for a lot of things.

so, how can i do similar thing in C#????

C# Solutions


Solution 1 - C#

Object createObjectBy(Type clazz){
   // .. do construction work here
    Object theObject = Activator.CreateInstance(clazz);
    return theObject;
}

Usage:

createObjectBy(typeof(A));

Or you could simply use Activator.CreateInstance directly :-)

Solution 2 - C#

The ideal way would be to use generics

###Type is known at design time###

public static T CreateInstance<T>() where T: new()
{
    // Do some business logic
    Logger.LogObjectCreation(typeof(T));

    // Actualy instanciate the object
    return new T();
}

A call example would look like

var employee = CreateInstance<Employee>();

###Type is unknown at runtime###

If the type of object is unknown at runtime, for example through a plugin system, you need to use the Type class:

public static object CreateInstance(Type type)
{
    // Do some business logic
    Logger.LogObjectCreation(type);

    // Actualy instanciate the object
    return Activator.CreateInstance(type);
}

A call example would look like

var instance = CreateInstance(someType);

###Performance###

Of course, nothing beats instanciating an object than by using the keyword new. Except maybe not instanciating, but instead reusing an object, like through caching.

If you have to settle for the second method where the type is unknown, there are [some alternatives][1] to Activator.CreateInstance. Although the article recommend using lambda expression, your biggest consideration is:

  • Does your unknown object need to be instantiated often in a short period of time, or do you only create it once

If you only need to create your object once, just stick with the Activator.CreateInstance method. If you need to create it multiple time in a short time, try the lambda approach. That last approach is similar to a compiled regular expression vs. an on-the-fly regular expression. [1]: http://vagifabilov.wordpress.com/2010/04/02/dont-use-activator-createinstance-or-constructorinfo-invoke-use-compiled-lambda-expressions/

Solution 3 - C#

C# doesn't support this. But what are you trying to do?

You probably could use:

createObjectBy(Type type);

or

createObjectBy<T>();

Solution 4 - C#

Use the class Type. You can return an instance of it by call

obj.GetType();

or without an object instance

typeof(className);

I hope it helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjojoView Question on Stackoverflow
Solution 1 - C#Aviad P.View Answer on Stackoverflow
Solution 2 - C#Pierre-Alain VigeantView Answer on Stackoverflow
Solution 3 - C#hackerhasidView Answer on Stackoverflow
Solution 4 - C#CARLOS LOTHView Answer on Stackoverflow