Calling a Generic Method from BaseEntity on unknown object

Mert

this is what I do if I know the type;

TypeA.Get<TypeA>(Id).Delete();

and what I am trying to do is this;

object ObjectA;

(BaseEntity<typeof(ObjectA)>).Get<(BaseEntity<typeof(ObjectA)>)>(Id).Delete();

which didn't work, any idea how to do this properly?

dcastro

You can either:

  • use reflection to call both Get and Delete methods, or:
  • use reflection to call Get and dynamic to invoke Delete


Since you're giving up static typing either way, I'd go with the 2nd option for simplicity.

var typeA = objectA.GetType();
var fEntityType = typeof(FEntity<>).MakeGenericType(typeA);

var getMethod = fEntityType.GetMethod("Get").MakeGenericMethod(typeA);

dynamic result = getMethod.Invoke(null, new object[]{ Id });

result.Delete();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling generic method from viewmodel

From Dev

Calling a MaybeNull generic method from another generic method

From Dev

Calling non-generic method from generic method in C#

From Java

Calling an overloaded java generic method from scala

From Dev

Calling constructor from a subclass method with a generic parameter

From Dev

Calling a generic Java varargs method from Kotlin

From Dev

Calling a static method from a generic constraint Dart

From Dev

Calling method from member object

From Dev

calling an object method from another method not working

From Dev

Calling generic method with generic collection

From Dev

“Object does not match target type” when calling generic method with reflection

From Dev

Calling a generic method

From Dev

Generic parameter T not implicitly assignable to object -- generic method calling nongeneric overloaded method

From Dev

Generic solution to create an Object of unknown deepth from an Array

From Dev

Is it possible to return raw object from generic method?

From Dev

Cannot return object from generic method

From Dev

Return a derived object from a generic method

From Dev

Java get method from generic Class object

From Dev

How to call a method from a Java generic object?

From Dev

Cast unknown object to generic interface of interface (from initially explicit generic type of generic collection type of type)

From Dev

Parse error from calling object method in ReactJS?

From Dev

Calling a method of a global object from a function

From Dev

Calling prototype method from another object

From Dev

Calling javascript Service/Method from array object

From Dev

calling an abstract method from the subclass object

From Dev

Access calling object from Javascript method

From Dev

Calling an object and its method from another object class method

From Dev

Calling an object's prototype method from another object's method

From Dev

Calling c++ generic method from C#

Related Related

  1. 1

    Calling generic method from viewmodel

  2. 2

    Calling a MaybeNull generic method from another generic method

  3. 3

    Calling non-generic method from generic method in C#

  4. 4

    Calling an overloaded java generic method from scala

  5. 5

    Calling constructor from a subclass method with a generic parameter

  6. 6

    Calling a generic Java varargs method from Kotlin

  7. 7

    Calling a static method from a generic constraint Dart

  8. 8

    Calling method from member object

  9. 9

    calling an object method from another method not working

  10. 10

    Calling generic method with generic collection

  11. 11

    “Object does not match target type” when calling generic method with reflection

  12. 12

    Calling a generic method

  13. 13

    Generic parameter T not implicitly assignable to object -- generic method calling nongeneric overloaded method

  14. 14

    Generic solution to create an Object of unknown deepth from an Array

  15. 15

    Is it possible to return raw object from generic method?

  16. 16

    Cannot return object from generic method

  17. 17

    Return a derived object from a generic method

  18. 18

    Java get method from generic Class object

  19. 19

    How to call a method from a Java generic object?

  20. 20

    Cast unknown object to generic interface of interface (from initially explicit generic type of generic collection type of type)

  21. 21

    Parse error from calling object method in ReactJS?

  22. 22

    Calling a method of a global object from a function

  23. 23

    Calling prototype method from another object

  24. 24

    Calling javascript Service/Method from array object

  25. 25

    calling an abstract method from the subclass object

  26. 26

    Access calling object from Javascript method

  27. 27

    Calling an object and its method from another object class method

  28. 28

    Calling an object's prototype method from another object's method

  29. 29

    Calling c++ generic method from C#

HotTag

Archive