As below code i just want to know can I use Invoke() for abstract class
public abstract class genericDefine
{
public void Foo<T>(T item)
{
Console.WriteLine(typeof(T).Name);
}
}
var bar = typeof(Bar);
var fooMethod = typeof(genericDefine).GetMethod("Foo");
var fooOfBarMethod = fooMethod.MakeGenericMethod(new[] { bar });
fooOfBarMethod.Invoke(new genericDefine(), new object[] { new Bar() });
I also tried for use Derived class object but it wont work for me....!
In order to invoke the MethodInfo
without passing in an instance of the object you need to have that method defined as static
. So if your method was defined as such it does work. Otherwise you need to have an subclass instance of the abstract base class.
public abstract class AbstractClass
{
public static void Foo<T>(T item)
{
Console.WriteLine(typeof(T).Name + ": " + item);
}
}
But then why have the class be abstract
?
abstract
classstatic
-- which defeats the purpose of abstract
classesCollected from the Internet
Please contact [email protected] to delete if infringement.
Comments