2

I have a generic Class

public  class MyClass<T>
{
   public List<T> Translate(List<T> list, params string[] names)
           {
            //do something here,modify list and return list
       }
    }

Now i can easily create its instances like

MyClass<Employee> obj= new MyClass<Employee>(); OR
MyClass<Vehicle> obj = new MyClass<Vehicle>();

I can call my method like

    obj.Translate(Mylist of employee or vehicle type,"param1","param2")

But in my case i dont know the type T as its generated on runtime,see the code below

String classname = string.Empty;

if(Classtype == 1)
{
    classname = "Employee"
}
else if(classtype == 2)
{
    classname = "Vehicle"
}

I want something like below..so that i can creat an instance of this generic class

MyClass<typeof(classname)> empp = new MyClass<typeof(classname)>();

    empp.Translate(MyList,"param1","param2")

Please suggest,how can i do that.

7
  • Is it actually useful to you to have T, or do you just need the runtime Type? Commented Nov 19, 2013 at 16:19
  • possible duplicate of How to dynamically create generic C# object using reflection? Commented Nov 19, 2013 at 16:22
  • @TimS. Now i want to call my generic class's method How can i do something like Instance.Mymethod(parameters); Commented Nov 19, 2013 at 16:54
  • @gunr2171 Now i want to call my generic class's method How can i do something like Instance.Mymethod(parameters); Commented Nov 19, 2013 at 16:54
  • I believe that you should also describe how you are supposed to use the generic type in your future code. Commented Nov 19, 2013 at 17:21

3 Answers 3

9

Try

var someType = Type.GetType("MyProject.Employee");
var yourGenericType = typeof(MyClass<>).MakeGenericType(new [] { someType });
var instance = Activator.CreateInstance(yourGenericType);

Note that Type.GetType(...) only works with the full namespace of a type, or even the full assembly qualified name if your class is not in the same dll as the code that is executing this.

Sign up to request clarification or add additional context in comments.

13 Comments

So how the OP calls some method or access some properties of the instance directly without using Reflection? This is not really a perfect solution.
@Moeri, maybe use Type.GetType(className)?
If you want to access any <T>-independent properties you need to extract those to an interface. Otherwise you're stuck with reflection, like you say.
@AlexFilipovici in his example his classnames are only the actual class names, not the assembly qualified or full namespace names. But I'll add something to the post, good thinking. :)
@Moeri Now i want to call my generic class's method How can i do something like Instance.Mymethod(parameters);
|
0

Thanks All for the Help I was able to do it by this way

        var someType = Type.GetType("MyProject.Employee");
        var genericType = typeof(Convert<>).MakeGenericType(new[] { someType });
        var instance = Activator.CreateInstance(genericType);

        string instanceType = instance.GetType().FullName;

        MethodInfo methodInfo = Type.GetType(instanceType).GetMethod("Translate");
        genericType.InvokeMember(methodInfo.Name, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, instance, new object[] { lstEmp, "Name", "City" });

ishan

Comments

0
public T CreateEmptyObject<T>()
{
    return (T)Activator.CreateInstance(typeof(T));;
}

To call it:

YourClassObject yourClassObject = CreateEmptyObject<YourClassObject>();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.