I have a class that has a generic type and I'm trying to create an instance using the activator but having trouble with it. This is what I have:
public abstract class MyBaseClass<T> where T : new()
{
...
public abstract T DoSomething();
}
public class MyClass<T> : MyBaseClass<T> where T : new()
{
...
public override T DoSomething() { .. }
}
And this is what I'm trying to do but didn't work. This gives my a null error: System.ArgumentNullException: Value cannot be null:
string someStr = "MyClass";
MyBaseClass<Customer> customerProvider =
Activator.CreateInstance(Type.GetType(someStr));
I also tried this but didn't work. It gave a compiler error about Activator.CreateInstance can't find that overloaded method. :
string someStr = "MyClass";
MyBaseClass<Customer> customerProvider =
Activator.CreateInstance<Customer>(Type.GetType(someStr));
Any other ideas on how to pass <Customer> to the Activator? There's gotta be some kind of syntax for it but couldn't find it anywhere.
public abstract class MyBaseClass<T> where T : new()
{
...
public abstract T DoSomething();
}
public class MyClass<T> : MyBaseClass<T> where T : new()
{
...
public override T DoSomething() { .. }
}
And this is what I'm trying to do but didn't work. This gives my a null error: System.ArgumentNullException: Value cannot be null:
string someStr = "MyClass";
MyBaseClass<Customer> customerProvider =
Activator.CreateInstance(Type.GetType(someStr));
I also tried this but didn't work. It gave a compiler error about Activator.CreateInstance can't find that overloaded method. :
string someStr = "MyClass";
MyBaseClass<Customer> customerProvider =
Activator.CreateInstance<Customer>(Type.GetType(someStr));
Any other ideas on how to pass <Customer> to the Activator? There's gotta be some kind of syntax for it but couldn't find it anywhere.