Call a generic method and set the generic type at runtime

MikeAlike234

In the example bellow, is it possible to set the Classname type parameter dynamically?

UpdateAndSave<Classname>>().Execute(sql)
Marc Gravell

If your type is coming from an object, then you can cheat using dynamic - hijacking it to perform (and cache, via meta-programming etc) the type resolution for you. For example - if you currently have:

object obj = ...
Type type = obj.GetType();
// now want to call UpdateAndSave<type>(...)

then you can do:

public void Voodoo<T>(T obj, string sql) {
    UpdateAndSave<T>().Execute(sql);
}

and just:

object obj = ...
Voodoo((dynamic)obj, sql);

The runtime will detect the type of obj, determine the best overload to use (which will be Voodoo<TheActualType>(...)), and call that. Plus it will cache the strategy for that type, so it is fast next time (it only does any significant work once per unique type).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call generic method by type

From Dev

How to call IQueryable.OrderBy() method with a runtime type for generic parameter?

From Dev

Create instance of class with generic type and call method of same generic type from string name of object at runtime

From Dev

Method with Generic Type, set value

From Dev

Method with Generic Type, set value

From Dev

Call generic method with class determined at runtime

From Dev

Create list of runtime-known type from object list and call generic method

From Dev

Strange generic method call with explicit generic type behaviour

From Dev

Call a "static" method belonging to a generic type in scala

From Dev

How to call an overloaded method from a generic type

From Dev

How to pass an object type to call a generic method

From Dev

Call static method from generic type

From Dev

Call generic method which T is a Type

From Dev

Call static method from trait on generic type

From Dev

Call static method from generic type

From Dev

Call method based on the type of the generic list element

From Dev

Runtime generic type determination

From Dev

Set no type for a static method in a generic class

From Dev

Set only second argument type in generic method

From Dev

Generic method returning generic type

From Dev

Generic type conversion in generic method

From Dev

Getting a generic method to infer the type parameter from the runtime type

From Dev

Call generic Method with multiple generic Types without specifying every single generic Type

From Dev

How to call a generic method?

From Dev

Java generic method call

From Dev

Call object method of generic

From Dev

c# find method overload from generic type at runtime

From Dev

Overriding necessary to avoid runtime type check for Java generic method?

From Dev

Method that accepts a "generic" Enum as a parameter, then cast to a diff enum type at runtime

Related Related

  1. 1

    Call generic method by type

  2. 2

    How to call IQueryable.OrderBy() method with a runtime type for generic parameter?

  3. 3

    Create instance of class with generic type and call method of same generic type from string name of object at runtime

  4. 4

    Method with Generic Type, set value

  5. 5

    Method with Generic Type, set value

  6. 6

    Call generic method with class determined at runtime

  7. 7

    Create list of runtime-known type from object list and call generic method

  8. 8

    Strange generic method call with explicit generic type behaviour

  9. 9

    Call a "static" method belonging to a generic type in scala

  10. 10

    How to call an overloaded method from a generic type

  11. 11

    How to pass an object type to call a generic method

  12. 12

    Call static method from generic type

  13. 13

    Call generic method which T is a Type

  14. 14

    Call static method from trait on generic type

  15. 15

    Call static method from generic type

  16. 16

    Call method based on the type of the generic list element

  17. 17

    Runtime generic type determination

  18. 18

    Set no type for a static method in a generic class

  19. 19

    Set only second argument type in generic method

  20. 20

    Generic method returning generic type

  21. 21

    Generic type conversion in generic method

  22. 22

    Getting a generic method to infer the type parameter from the runtime type

  23. 23

    Call generic Method with multiple generic Types without specifying every single generic Type

  24. 24

    How to call a generic method?

  25. 25

    Java generic method call

  26. 26

    Call object method of generic

  27. 27

    c# find method overload from generic type at runtime

  28. 28

    Overriding necessary to avoid runtime type check for Java generic method?

  29. 29

    Method that accepts a "generic" Enum as a parameter, then cast to a diff enum type at runtime

HotTag

Archive