A
Anders Borum
Hi
I need to invoke a generic method determined at runtime. The method has two
arguments, a string and a generic type that is constrained to a struct:
public void Add<T>(string key, T value) where T : struct
The method is an instance member located on a class called
CmsPropertyManager. I also have a number of other "Add" methods with
different overloads.
Usually, I find it quite easy to go by reflecting the type and bind to the
method, but this time it's been really hard to determine the correct binding
parameters. When I inspect the method info, I can see that the generic
argument is having a base type of System.ValueType. However, using this as
the binding signature doesn't yield any results.
// Acquire the method info (this is the part that I'm having trouble with)
MethodInfo methodInfo = typeof(CmsPropertyManager).GetMethod("Add", new
Type[] { typeof(string), typeof(System.ValueType) });
// Acquire the generic method info
MethodInfo genericInfo = methodInfo.MakeGenericMethod(type.Type);
// Invoke with a struct (represented by o)
genericInfo.Invoke(null, new object[] { this.uiKeyName.Value, o });
Please note that once I have the method / generic method info everything is
fine and dandy; that is, if I hardcode the methodinfo reference to the right
index in the reflected type (CmsPropertyManager) the rest of the reflection
part works perfectly).
Thanks in advance.
I need to invoke a generic method determined at runtime. The method has two
arguments, a string and a generic type that is constrained to a struct:
public void Add<T>(string key, T value) where T : struct
The method is an instance member located on a class called
CmsPropertyManager. I also have a number of other "Add" methods with
different overloads.
Usually, I find it quite easy to go by reflecting the type and bind to the
method, but this time it's been really hard to determine the correct binding
parameters. When I inspect the method info, I can see that the generic
argument is having a base type of System.ValueType. However, using this as
the binding signature doesn't yield any results.
// Acquire the method info (this is the part that I'm having trouble with)
MethodInfo methodInfo = typeof(CmsPropertyManager).GetMethod("Add", new
Type[] { typeof(string), typeof(System.ValueType) });
// Acquire the generic method info
MethodInfo genericInfo = methodInfo.MakeGenericMethod(type.Type);
// Invoke with a struct (represented by o)
genericInfo.Invoke(null, new object[] { this.uiKeyName.Value, o });
Please note that once I have the method / generic method info everything is
fine and dandy; that is, if I hardcode the methodinfo reference to the right
index in the reflected type (CmsPropertyManager) the rest of the reflection
part works perfectly).
Thanks in advance.