1

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.

Ishan Arora
  • 314
  • 2
  • 16

3 Answers3

8

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.

Moeri
  • 9,104
  • 5
  • 43
  • 56
  • 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. – King King Nov 19 '13 at 16:18
  • @Moeri, maybe use `Type.GetType(className)`? – Alex Filipovici Nov 19 '13 at 16:18
  • If you want to access any -independent properties you need to extract those to an interface. Otherwise you're stuck with reflection, like you say. – Moeri Nov 19 '13 at 16:19
  • @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 Nov 19 '13 at 16:20
  • @Moeri Now i want to call my generic class's method How can i do something like Instance.Mymethod(parameters); – Ishan Arora Nov 19 '13 at 16:42
  • @KingKing Now i want to call my generic class's method How can i do something like Instance.Mymethod(parameters); – Ishan Arora Nov 19 '13 at 16:53
  • @AlexFilipovici Now i want to call my generic class's method How can i do something like Instance.Mymethod(parameters); – Ishan Arora Nov 19 '13 at 16:53
  • @IshanArora that's **the most difficult thing** and I realized that requirement of yours at first right after reading the question, looks like all the dynamic stuff would involve some part of `Reflection`. – King King Nov 19 '13 at 16:58
  • @KingKing.. Please help if you have some idea king – Ishan Arora Nov 19 '13 at 17:04
  • @IshanArora: please provide a full example on what you are trying to achieve, and I'll see what I can do. Because right now, two roads are open to call methods on dynamically resolved types: interfaces or reflection. But we would like to avoid reflection as much as possible, so we need to see your scenario if possible. – Moeri Nov 19 '13 at 17:26
  • Hey Ishan. Seeing as your Translate method is also fully generic, you will have to use reflection. Can I ask: why do you need to pass the class names as strings? If you can avoid using strings for class names, this could become a lot simpler. – Moeri Nov 20 '13 at 07:27
  • @Moeri.. i have found the solution .. please see below and mark it as answer:) – Ishan Arora Nov 20 '13 at 12:48
  • Hello Ishan. You are the only one who can mark it as answer ;-) – Moeri Nov 20 '13 at 13:09
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

Ishan Arora
  • 314
  • 2
  • 16
0
public T CreateEmptyObject<T>()
{
    return (T)Activator.CreateInstance(typeof(T));;
}

To call it:

YourClassObject yourClassObject = CreateEmptyObject<YourClassObject>();
Chizl
  • 2,004
  • 17
  • 32