How to create an Action<T> with Reflection where T is a discovered generic Type

BuddyJoe

How can accomplish the following in C#?

var someType = new CustomerMessageHandler<CustomerMessage>();
var nType = someType.GetGenericArguments().First().GetType();
// except the next line of code would be a (Action<nType>)
var a = new Action<string>();

Does Reflection.Emit, Expressions, or F# have a good solution for this?

Note: A 'convention-based' approach is not an option for me. And I'm asking to see if this is even possible to push my C#/F# skills.

Haney

Sure, MakeGenericType will be able to do this for you.

Type genericAction = typeof(Action<>);
var someType = new CustomerMessageHandler<CustomerMessage>();
var nType = someType.GetGenericArguments().First().GetType();
var actionType = genericAction.MakeGenericType(new[] { nType });

Note however that an Action<T> is a delegate that should actually DO something... So you'd need to write a body for that:

var actualMethod = Delegate.CreateDelegate(actionType, **your method info here**);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check that MethodInfo matches Delegate of generic type T, where T is either Action or Func?

From Dev

How to create an instance just by generic type T

From Dev

How to create a new List<T> knowing the Type through reflection?

From Dev

Generic type where T can be anything

From Dev

Generics Fun: Where typeof(List<T>) != typeof(List<T>), and using Reflection to get a generic method, with generic parameters

From Dev

How to get type of T generic?

From Dev

How to cast a generic object of type A<T> to A<T'>?

From Dev

How to create new object of Generic Type T from a parametrized List<T>

From Dev

Create Action<T> where T is baseclass of Method parameter

From Dev

Create Action<T> where T is baseclass of Method parameter

From Dev

Create a special dictionary<T, EventDelegate<T>> with generic delegate EventDelegate<T>(T e) where T : GameEventBase

From Dev

Create generic List<T> of T4 generated type

From Dev

Reflection Get List<> from List<T> (Container type from generic container type)

From Dev

How to Autowire Bean of generic type <T> in Spring?

From Dev

How to provide generic type parameter to Sequence[T]?

From Dev

Get the type of generic T

From Dev

C# How do I Access this generic list, when i didn't create the type?

From Dev

How to create generic type?

From Dev

How to determine the type of an generic array element with reflection?

From Dev

Reflection can't get the actual parameter type of a method that overrides from a generic-typed interface?

From Dev

C# generics: what's the point of the "X<T> where T: X<T>" generic type constraint?

From Dev

constraint generic type T and K where K extends keyof T and T[K] is boolean?

From Dev

Why can't I create an array of an inner class of a generic type?

From Dev

Create a generic class with an array of type Comparable<T> in kotlin?

From Dev

Create a generic class with an array of type Comparable<T> in kotlin?

From Dev

How to configure Moq to make use of generic action<T>

From Dev

How to create Action<Tᴺ> or Func<Tᴺ, out TResult> from MethodInfo

From Dev

How to identify the <T> type for Int,String,Generic Type or class

From Dev

Reflection subclass of generic type

Related Related

  1. 1

    How to check that MethodInfo matches Delegate of generic type T, where T is either Action or Func?

  2. 2

    How to create an instance just by generic type T

  3. 3

    How to create a new List<T> knowing the Type through reflection?

  4. 4

    Generic type where T can be anything

  5. 5

    Generics Fun: Where typeof(List<T>) != typeof(List<T>), and using Reflection to get a generic method, with generic parameters

  6. 6

    How to get type of T generic?

  7. 7

    How to cast a generic object of type A<T> to A<T'>?

  8. 8

    How to create new object of Generic Type T from a parametrized List<T>

  9. 9

    Create Action<T> where T is baseclass of Method parameter

  10. 10

    Create Action<T> where T is baseclass of Method parameter

  11. 11

    Create a special dictionary<T, EventDelegate<T>> with generic delegate EventDelegate<T>(T e) where T : GameEventBase

  12. 12

    Create generic List<T> of T4 generated type

  13. 13

    Reflection Get List<> from List<T> (Container type from generic container type)

  14. 14

    How to Autowire Bean of generic type <T> in Spring?

  15. 15

    How to provide generic type parameter to Sequence[T]?

  16. 16

    Get the type of generic T

  17. 17

    C# How do I Access this generic list, when i didn't create the type?

  18. 18

    How to create generic type?

  19. 19

    How to determine the type of an generic array element with reflection?

  20. 20

    Reflection can't get the actual parameter type of a method that overrides from a generic-typed interface?

  21. 21

    C# generics: what's the point of the "X<T> where T: X<T>" generic type constraint?

  22. 22

    constraint generic type T and K where K extends keyof T and T[K] is boolean?

  23. 23

    Why can't I create an array of an inner class of a generic type?

  24. 24

    Create a generic class with an array of type Comparable<T> in kotlin?

  25. 25

    Create a generic class with an array of type Comparable<T> in kotlin?

  26. 26

    How to configure Moq to make use of generic action<T>

  27. 27

    How to create Action<Tᴺ> or Func<Tᴺ, out TResult> from MethodInfo

  28. 28

    How to identify the <T> type for Int,String,Generic Type or class

  29. 29

    Reflection subclass of generic type

HotTag

Archive