MongoDB, C#, Reflection, Dynamic Execution [duplicate] - c#

Here's my class:
public class MyClass<T>
{
public void MyMethod(T a)
{
}
public void MyMethod(int a)
{
}
}
How would I Invoke MyMethod(T) using Reflection even if int is the generic type parameter for MyClass?
Here's a single statement that does it (Might be inefficient, but I like brevity):
var mc = new MyClass<int>();
typeof(MyClass<int>).GetMethods().ElementAt(
typeof(MyClass<int>).
GetGenericTypeDefinition().
GetMethods().ToList().FindIndex(m =>
m.Name.Equals("MyMethod") &&
m.GetParameters().Count() == 1 &&
m.GetParameters()[0].ParameterType.IsGenericParameter)
).Invoke(mc, new object[] { 1 });

Revised Answer
Okay, so I think I've figured out why the IsGenericParameter property evaluates as false is because you were creating the MyClass type with an explicit type for <T>.
Since the compiler new what type the a parameter was (inferring from the instantiation of the class), I'm guessing that the compiler was treating the parameter as a non-generic type.
Also, based upon what I was reading in MSDN, I think that the ParameterType.IsGenericParameter and Type.IsGenericType property would only evaluate to true
whenever you had a method like MyMethod<T>() vs. MyMethod(T a), where the type for <T> is inferred from the type instantiated with the class.
Here's a little program that demonstrates this:
using System;
using System.Linq;
using System.Reflection;
namespace GenericParametersViaReflectionTest
{
class Program
{
static void Main(string[] args)
{
// Note: we're using the type without specifying a type for <T>.
var classType = typeof(MyClass<>);
foreach (MethodInfo method in classType.GetMembers()
.Where(method => method.Name == "MyMethod"))
{
// Iterate through each parameter of the method
foreach (var param in method.GetParameters())
{
// For generic methods, the name will be "T" and the FullName
// will be null; you can use which ever check you prefer.
if (param.ParameterType.Name == "T"
|| param.ParameterType.FullName == null)
Console.WriteLine("We found our generic method!");
else
Console.WriteLine("We found the non-generic method:");
Console.WriteLine("Method Name: {0}", method.Name);
Console.WriteLine("Parameter Name: {0}", param.Name);
Console.WriteLine("Type: {0}", param.ParameterType.Name);
Console.WriteLine("Type Full Name: {0}",
param.ParameterType.FullName ?? "null");
Console.WriteLine("");
}
}
Console.Read();
}
}
public class MyClass<T>
{
public void MyMethod(T a) { }
public void MyMethod(int a) { }
}
}
And the results we end up with are:
We found or generic method!
Method Name: MyMethod
Parameter Name: a
Type: T
Type Full Name: null
We found the non-generic method:
Method Name: MyMethod
Parameter Name: a
Type: Int32
Type Full Name: System.Int32
If you need to create an instance of the class using a particular type, you might find the Activator.CreateInstance class useful too.
Original Answer
I think that if you pass in a parameter of the same datatype that matched one of the explictly set methods (e.g. the Int32 method), then the compiler would automatically select that over the one that accepts a generic parameter. Otherwise, the generic method would be selected by the compiler.
However, if you want to be able to control which method is selected, you could modify each method to have different parameter names, while maintaining identical signatures, like so:
public class MyClass<T>
{
public void MyMethod(T genericA) {}
public void MyMethod(int intA) {}
}
Then, using named parameters, you could explicitly call the desired method, like so:
var foo = new MyClass<int>();
foo.MyMethod(genericA: 24); // This will call the "MyMethod" that only accepts <T>.
foo.MyMethod(intA: 19); // This will call the "MyMethod" that only accepts integers.
Edit
For some reason, in my original answer I missed the part where you mentioned using reflection, but it looks my original answer could be coupled with these other answers to give you a viable solution:
Invoking methods with optional parameters through reflection
How can you get the names of method parameters?

The only way I was able to do this was to use GetGenericTypeDefinition with IsGenericParameter. In the generic type definition, one method will have IsGenericParameter set to true on the parameter. However, for closed types none of the parameters will have this as true. Then, you can't use the MethodInfo from the generic type definition to invoke the method, so I stored the index and used that to lookup the corresponding MethodInfo in the closed type.
public class Program
{
public static void Main(string[] args)
{
bool invokeGeneric = true;
MyClass<int> b = new MyClass<int>();
var type = b.GetType().GetGenericTypeDefinition();
int index = 0;
foreach(var mi in type.GetMethods().Where(mi => mi.Name == "MyMethod"))
{
if (mi.GetParameters()[0].ParameterType.IsGenericParameter == invokeGeneric)
{
break;
}
index++;
}
var method = b.GetType().GetMethods().Where(mi => mi.Name == "MyMethod").ElementAt(index);
method.Invoke(b, new object[] { 1 });
}
}
public class MyClass<T>
{
public void MyMethod(T a)
{
Console.WriteLine("In generic method");
}
public void MyMethod(int a)
{
Console.WriteLine("In non-generic method");
}
}

Related

Comparing generic parameter type using reflection

I want to get a method definition that accepts an Action<T> parameter using reflection. I'm using .NET core 1.1.
Since the class has two methods with the same name, I'm trying to check the type of the accepted parameters to make sure that I'm getting the correct method definition (and not the other overload), but the comparison does not seem to work.
Here is some code that shows this problem:
using System;
using System.Linq;
using System.Reflection;
class ReflectMe {
public void SomeMethod<T>(Action<T> action) {
action(default(T));
}
public T SomeMethod<T>() {
return default(T);
}
}
class Program {
static void Main(string[] args) {
var reflectedMethod = typeof(ReflectMe).GetTypeInfo().GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.Name == "SomeMethod" && m.IsGenericMethodDefinition)
.Where(m => {
var parameters = m.GetParameters();
if (parameters.Count() != 1) {
// this filters out the 1st method
return false;
}
var actionType = typeof(Action<>);
var parameterType = parameters[0].ParameterType;
if (parameterType == actionType) {
// this is always false, even if in the debugger both
// types are displayed as System.Action`1[T]
return true;
}
return false;
})
.FirstOrDefault();
}
}
The problem is that parameterType and actionType are not equal, yet when I check in the debugger they look identical.
Why is that comparison failing?
You need to instantiate the generic definition of Action<T> to use the generic argument of the method:
var methodTypeArg = m.GetGenericArguments().First();
var actionType = typeof(Action<>).MakeGenericType(methodTypeArg);
Note: I do not have .NET Core 1.1 handy right now, I hope the api is the same, but your problem would be the same in any .NET version.
The reason you need to call MakeGenericType becomes more obvious if you change the name of the generic argument to the method:
public void SomeMethod<TMethodArg>(Action<TMethodArg> action)
Then it becomes more obvious that typeof(Action<>) != typeof(Action<TMethod>). You are comparing the generic definition of Action<> (which has it's on T) with an instantiation of that definition for the generic argument (TMethod) of the generic method SomeMethod

Is there a workaround to C# not being able to infer generic type arguments using type constraints?

Eric Lippert has explained in his blog post at http://blogs.msdn.com/b/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx why constraints are not considered for type inference, which makes sense given that methods cannot be overloaded by simply changing type constraints. However, I would like to find a way to instantiate an object using two generic types, one which can be inferred and another which could be inferred if constraints were considered, without having to specify any of the types.
Given the types:
public interface I<T>
{
Other<T> CreateOther();
}
public class C : I<string>
{
public Other<string> CreateOther()
{
return new Other<string>();
}
}
public class Other<T>
{
}
and the factory:
public static class Factory1
{
public static Tuple<T, Other<T1>> Create<T, T1>(T o) where T : I<T1>
{
return new Tuple<T, Other<T1>>(o, o.CreateOther());
}
}
the following desired code will not compile:
public void WontCompile()
{
C c = new C();
var v = Factory1.Create(c); // won't compile
}
The error message is "error CS0411: The type arguments for method 'yo.Factory1.Create(T)' cannot be inferred from the usage. Try specifying the type arguments explicitly.", which is in line with what Eric said in his blog post.
Thus, we can simply specify the generic type arguments explicitly, as the error message suggests:
public void SpecifyAllTypes()
{
C c = new C();
var v = Factory1.Create<C, string>(c); // type is Tuple<C, Other<string>>
}
If we don't wish to specify type arguments and we don't need to retain type C, we can use the following factory:
public static class Factory2
{
public static Tuple<I<T1>, Other<T1>> CreateUntyped<T1>(I<T1> o)
{
return new Tuple<I<T1>, Other<T1>>(o, o.CreateOther());
}
}
and now specify:
public void Untyped()
{
C c = new C();
var v = Factory2.CreateUntyped(c); // type is Tuple<I<string>, Other<string>>
}
However, I wish to retain type C in the returned object and not specify the types.
I came up with a solution to this problem, but it seems to be a kludge of a workaround, where the object of type C is used twice in a two-step factory call.
To do this, the following factories are used:
public static class Factory3
{
public static Factory<T1> CreateFactory<T1>(I<T1> o)
{
return new Factory<T1>();
}
}
public class Factory<T1>
{
public Tuple<T, Other<T1>> Create<T>(T o) where T : I<T1>
{
return new Tuple<T, Other<T1>>(o, o.CreateOther());
}
}
which can then be used as follows:
public void Inferred()
{
C c = new C();
var v = Factory3.CreateFactory(c).Create(c); // type is Tuple<C, Other<string>>
}
This just feels odd since c is used twice. The first time it is used it is actually discarded as it is just being used to infer the base type argument.
Is there a better solution to this problem where the object does not need to be used twice and the types do not need to be specified?
edit: I just realized that, although the object must be used twice, the second factory class is not needed. Rather, both parameters could just be used in the same factory method as follows:
public class Factory
{
public Tuple<T, Other<T1>> Create<T, T1>(T o, I<T1> o2) where T : I<T1>
{
return new Tuple<T, Other<T1>>(o, o.CreateOther());
}
}
This would be used as follows:
public void Inferred()
{
C c = new C();
var v = Factory.Create(c, c); // type is Tuple<C, Other<string>>
}
It's still not ideal, but better than having to create a second factory class, and at least XMLDoc comments could be used to indicate that both parameters should be the same object. Once again, the one parameter (o2 in this case) is only used to infer the constrained types for T.

Setting a generic type parameter from a method parameter

Is there any way to do something like this?
void SomeMethod(Type generic)
{
SomeGenericMethod<generic>();
}
I need to pass the type in as a "normal" parameter and not a generic type parameter.
You can do it with reflection:
public class foo
{
public void SomeMethod(Type type)
{
var methodInfo = this.GetType().GetMethod("SomeGenericMethod");
var method = methodInfo.MakeGenericMethod(new[] { type });
method.Invoke(this, null);
}
public void SomeGenericMethod<T>()
{
Debug.WriteLine(typeof(T).FullName);
}
}
class Program
{
static void Main(string[] args)
{
var foo = new foo();
foo.SomeMethod(typeof(string));
foo.SomeMethod(typeof(foo));
}
}
That said, using reflection in this way means you are losing some of the benefits of using generics in the first place, so you might want to look at other design alternatives.
Assuming that your method is defined in a class called MyClass, this should do it:
var MyObject = new MyClass();
typeof(MyClass).GetMethod("SomeGenericMethod").MakeGenericMethod(generic).Invoke(myObject, null);
Type.GetMethod() gets an object which describes a method defined in the Type it's called on.
The method is generic, so we need to call MakeGenericMethod, and pass its one generic parameter.
We then invoke the method, passing the object we want the method called on and any arguments it takes. As it takes no arguments, we just pass null.

How do I call DynamicInvoke on an Expression<Action<T>> using Compile?

Why doesn't this work?
namespace InvokeTest
{
public class MethodInvoker
{
public static void Execute<T>(Expression<Action<T>> call)
{
// Parameter count mismatch
call.Compile().DynamicInvoke();
// Also attempted this:
//call.Compile().DynamicInvoke(1);
// but it threw: "Object of type 'System.Int32' cannot be converted to type 'InvokeTest.TestClass'."
}
}
public class TestClass
{
public TestClass()
{ }
public void DoSomething(int num)
{
System.Console.WriteLine("It works");
}
public void KickOff()
{
MethodInvoker.Execute<TestClass>(m => m.DoSomething(1));
}
}
}
An Action<T> is the same as a delegate void F<T>(T t). That is, an Action<T> is an method that has return type void and consumes instances of T.
When you call
MethodInvoker.Execute<TestClass>(m => m.DoSomething(1));
you have set the type parameter T to be TestClass. Therefore, you need to pass in a parameter, and that parameter has to be instance of TestClass.
This is why in the first case you get a parameter count mismatch, and in the second case the compiler wants to convert the parameter to an instance of TestClass. You need to pass one parameter, and that parameter needs to be an instance of TestClass.
Note that your action is
m => m.DoSomething(1).
So your action takes an instance of TestClass which you are parameterizing by m and invokes m.DoSomething(1) on that instance. Now, when you dynamically invoke this method, you need to be giving it an instance of TestClass. You aren't doing that.
Since you defined the parameter type
Expression<Action<T>>
the function will require expression in accordance with the following delegate:
Action<TestClass>
which is:
public void SampleMethod(TestClass a)
Based on this the correct invocation should look like this:
var t = new TestClass();
call.Compile().DynamicInvoke(t);

How do I determine if a method is a generic instance of a generic method

I have a MethodInfo passed in to a function and I want to do the following
MethodInfo containsMethod = typeof(ICollection<>).GetMethod("Contains");
if (methodInfo.Equals(containsMethod)
{
// do something
}
But this doesn't work because the methodInfo has a specific generic type. For the example does work if I knew that the ICollection was always of type string.
MethodInfo containsMethod = typeof(ICollection<string>).GetMethod("Contains");
if (methodInfo.Equals(containsMethod)
{
// do something
}
How can I check whether the MethodInfo is a ANY typed instance of the generic method without caring what the type is?
Thanks.
EDIT: Question clarification
As correctly pointed out the method is not generic but the containing class is so the question is more how to I find out if the MethodInfo is for a Type which is a typed instance of ICollection<>.
EDIT: more context
I am writing a Linq provider and trying to handle the "in" case
IList<string> myList = new List<string>{ "1", "2" };
from Something s in ...
where myList.Contains(s.name)
select s;
Note that ICollection<T>.Contains is not a generic method - it is a non-generic method of a generic type. Otherwise IsGenericMethod and GetGenericTypeDefinition would help. You could obtain the generic type definition (DeclaringType.GetGenericTypeDefinition()) and work back up to Contains, but I wonder if you are approaching this problem the hard way.
Usually, if you are using reflection, it may be pragmatic to drop to non-generic IList - unless you need the type data (for example, for meta-programming). And in that case, I would consider looking closely to see if you can simplify the setup here.
You could check the declaring type:
if( methodInfo.Name == "Contains"
&& methodInfo.DeclaringType.IsGenericType
&& methodInfo.DeclaringType.GetGenericTypeDefinition() == typeof(ICollection<>))
{
Some error checking would need to be added to this, but I believe this roughly does what you want. You can use a method with or without a type argument as the parameter.
static bool IsContainsMethod(MethodInfo methodInfo)
{
Type[] types = { methodInfo.GetParameters().First().ParameterType };
MethodInfo containsMethod = typeof(ICollection<>).MakeGenericType(types).GetMethod("Contains");
return methodInfo.Equals(containsMethod);
}
The problem is that you don't have a generic method: you have a non-generic method on a generic type. I don't know of a way to use reflection to go directly from a method definition on an open generic type to that same method on a closed generic type or vice versa. However, you can take advantage of the fact that the methods returned by GetMethods() on the open and closed generic types should always be in the same order and do the translation by index:
MethodInfo containsMethod = typeof(ICollection<>).GetMethod("Contains");
var methodIndex = Array.IndexOf(methodInfo.DeclaringType.GetMethods(), methodInfo);
var methodOnTypeDefinition = methodInfo.DeclaringType.GetGenericTypeDefinition().GetMethods()[methodIndex];
if (methodOnTypeDefinition.Equals(containsMethod))
{
// do something
}
try this method
public static bool CheckGenericMethod(MethodInfo methodInfo)
{
bool areSimilarMethods = false;
MethodInfo methodToCompare = typeof(ISomeInterface<>).GetMethod("func");
Type interfaceInfo = methodInfo.DeclaringType.GetInterface(methodToCompare.DeclaringType.FullName);
if (interfaceInfo != null)
areSimilarMethods = (methodToCompare.Name.Equals(methodInfo.Name)
&& interfaceInfo.FullName.Contains(methodToCompare.DeclaringType.FullName));
else
{
areSimilarMethods = methodToCompare.DeclaringType.Equals(methodInfo.DeclaringType);
}
return areSimilarMethods;
}
and here is the full example usage.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace TestReflection
{
public class Program
{
static void Main(string[] args)
{
MethodInfo info1 = typeof(ISomeInterface<>).GetMethod("func");
MethodInfo info2 = typeof(MyStringCollection).GetMethod("func");
MethodInfo info3 = typeof(MyProgramCollection).GetMethod("func");
MethodInfo info4 = typeof(MyXCollection).GetMethod("func");
if (CheckGenericMethod(info1)) Console.WriteLine("true");else Console.WriteLine("false");
if (CheckGenericMethod(info2)) Console.WriteLine("true");else Console.WriteLine("false");
if (CheckGenericMethod(info3)) Console.WriteLine("true");else Console.WriteLine("false");
if (CheckGenericMethod(info4)) Console.WriteLine("true"); else Console.WriteLine("false");
Console.ReadKey();
}
public static bool CheckGenericMethod(MethodInfo methodInfo)
{
bool areSimilarMethods = false;
MethodInfo methodToCompare = typeof(ISomeInterface<>).GetMethod("func");
Type interfaceInfo = methodInfo.DeclaringType.GetInterface(methodToCompare.DeclaringType.FullName);
if (interfaceInfo != null)
areSimilarMethods = (methodToCompare.Name.Equals(methodInfo.Name)
&& interfaceInfo.FullName.Contains(methodToCompare.DeclaringType.FullName));
else
{
areSimilarMethods = methodToCompare.DeclaringType.Equals(methodInfo.DeclaringType);
}
return areSimilarMethods;
}
}
public interface ISomeInterface<T> where T : class
{
T func(T s);
}
public class MyStringCollection : ISomeInterface<string>
{
public string func(string s)
{
return s;
}
}
public class MyProgramCollection : ISomeInterface<Program>
{
public Program func(Program s)
{
return s;
}
}
public class MyXCollection
{
public int func(int s)
{
return s;
}
}
}

Categories