can I use invoke to abstract class method

Pravin Sharma

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....!

David Pine

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?

  1. You either need an instance from a class that inherits the abstract class
  2. Or, you need to make the method static -- which defeats the purpose of abstract classes

Here is the .NET fiddle.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Abstract class non abstract method invoke

From Dev

How can I use reflection to invoke a method on this internal class that exists in a library?

From Dev

How can we use the abstract getScreenSize() method in Toolkit class?

From Dev

How can I access injected Grails beans in an abstract class method?

From Dev

Can I make a static factory method in an abstract class?

From Dev

Why can I call an abstract class method in Python?

From Dev

Why I can not use abstract class in std::vector?

From Dev

Can I use an abstract base class as a Unity Editor element?

From Dev

Why can I abstract override an abstract method?

From Dev

Invoke non-abstract method of super interface in concrete class

From Dev

How to invoke the method fun of an abstract class in the following code?

From Dev

How should i invoke an instance which implements abstract method

From Dev

Why cannot ceate an abstract class instance but can invoke its constructor?

From Dev

Can I make a method in an abstract class which constructs an instance for the instantiating class?

From Dev

Can't invoke the protected method in StAXOMBuilder() class

From Dev

Can't invoke the protected method in StAXOMBuilder() class

From Dev

why abstract class can have a instance method?

From Dev

Can we create abstract class without abstract method in php?

From Dev

Can I use the same decorator on a class method and on a static method?

From Dev

How can I use a Method from a class in a different class?

From Dev

How can I use class property use extension method

From Dev

Why can I invoke a private method on an instance of the sub-class when it shouldn't be visible to the instance?

From Dev

How can I invoke a method or access a field from other class in the same package in JAVA

From Dev

Why can't I initialize an abstract class's method without changing them to static?

From Dev

how can i implement specific method from two different abstract class?

From Dev

Can you use an abstract function in an abstract class in c++?

From Dev

Can I invoke the delegate method after popViewcontroller?

From Dev

Should I use an interface or abstract class in this scenario?

From Dev

Abstract Class with only abstract methods and Interface - Which should I use?

Related Related

  1. 1

    Abstract class non abstract method invoke

  2. 2

    How can I use reflection to invoke a method on this internal class that exists in a library?

  3. 3

    How can we use the abstract getScreenSize() method in Toolkit class?

  4. 4

    How can I access injected Grails beans in an abstract class method?

  5. 5

    Can I make a static factory method in an abstract class?

  6. 6

    Why can I call an abstract class method in Python?

  7. 7

    Why I can not use abstract class in std::vector?

  8. 8

    Can I use an abstract base class as a Unity Editor element?

  9. 9

    Why can I abstract override an abstract method?

  10. 10

    Invoke non-abstract method of super interface in concrete class

  11. 11

    How to invoke the method fun of an abstract class in the following code?

  12. 12

    How should i invoke an instance which implements abstract method

  13. 13

    Why cannot ceate an abstract class instance but can invoke its constructor?

  14. 14

    Can I make a method in an abstract class which constructs an instance for the instantiating class?

  15. 15

    Can't invoke the protected method in StAXOMBuilder() class

  16. 16

    Can't invoke the protected method in StAXOMBuilder() class

  17. 17

    why abstract class can have a instance method?

  18. 18

    Can we create abstract class without abstract method in php?

  19. 19

    Can I use the same decorator on a class method and on a static method?

  20. 20

    How can I use a Method from a class in a different class?

  21. 21

    How can I use class property use extension method

  22. 22

    Why can I invoke a private method on an instance of the sub-class when it shouldn't be visible to the instance?

  23. 23

    How can I invoke a method or access a field from other class in the same package in JAVA

  24. 24

    Why can't I initialize an abstract class's method without changing them to static?

  25. 25

    how can i implement specific method from two different abstract class?

  26. 26

    Can you use an abstract function in an abstract class in c++?

  27. 27

    Can I invoke the delegate method after popViewcontroller?

  28. 28

    Should I use an interface or abstract class in this scenario?

  29. 29

    Abstract Class with only abstract methods and Interface - Which should I use?

HotTag

Archive