This question already has an answer here:
Is it possible to invoke a static generic method via PrivateType? I have tried everything I can think of, but have not been successful. Below is a source file that demonstrates the problem I am having. The calls that fail are commented out. I am successful with every other kind of method, including generic methods, but not when the method is static and generic. Can anyone help? I am using VS2017 with .NET 4.7.2.
using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ConsoleApp1
{
public abstract class IExpression
{
}
public class Expression : IExpression
{
}
public class NonStaticClass
{
private string NonStaticNonGenericMethod(string str, IExpression e)
{
return "NonStaticNonGenericMethod";
}
private string NonStaticGenericMethod<T>(string str, T e) where T : IExpression
{
return "NonStaticGenericMethod";
}
private static string StaticNonGenericMethod(string str, IExpression e)
{
return "StaticNonGenericMethod";
}
private static string StaticGenericMethod<T>(string str, T e) where T : IExpression
{
return "StaticGenericMethod";
}
}
public static class StaticClass
{
private static string StaticNonGenericMethod(string str, IExpression e)
{
return "StaticNonGenericMethod";
}
private static string StaticGenericMethod<T>(string str, T e) where T : IExpression
{
return "StaticGenericMethod";
}
}
class Program
{
static void Main(string[] args)
{
string result;
PrivateObject nonStaticObject = new PrivateObject(typeof(NonStaticClass));
result = (string)nonStaticObject.Invoke("NonStaticNonGenericMethod",
new Type[] { typeof(string), typeof(Expression) },
new object[] { "", new Expression() });
Assert.AreEqual("NonStaticNonGenericMethod", result);
result = (string)nonStaticObject.Invoke("NonStaticGenericMethod",
new Type[] { typeof(string), typeof(Expression) },
new object[] { "", new Expression() },
new Type[] { typeof(Expression) });
Assert.AreEqual("NonStaticGenericMethod", result);
result = (string)nonStaticObject.Invoke("StaticNonGenericMethod",
BindingFlags.Static,
new Type[] { typeof(string), typeof(Expression) },
new object[] { "", new Expression() });
Assert.AreEqual("StaticNonGenericMethod", result);
//result = (string)nonStaticObject.Invoke("StaticGenericMethod",
// BindingFlags.Static,
// new Type[] { typeof(string), typeof(Expression) },
// new object[] { "", new Expression() },
// new Type[] { typeof(Expression) });
//Assert.AreEqual("StaticGenericMethod", result);
PrivateType nonStaticClass = new PrivateType(typeof(NonStaticClass));
result = (string) nonStaticClass.InvokeStatic("StaticNonGenericMethod",
new Type[] { typeof(string), typeof(Expression) },
new object[] {"", new Expression() });
Assert.AreEqual("StaticNonGenericMethod", result);
//result = (string)nonStaticClass.InvokeStatic("StaticGenericMethod",
// new Type[] { typeof(string), typeof(Expression) },
// new object[] { "", new Expression() },
// new Type[] { typeof(Expression) });
//Assert.AreEqual("StaticGenericMethod", result);
PrivateType staticClass = new PrivateType(typeof(StaticClass));
result = (string) staticClass.InvokeStatic("StaticNonGenericMethod",
new Type[] { typeof(string), typeof(Expression) },
new object[] {"", new Expression() });
Assert.AreEqual("StaticNonGenericMethod", result);
//result = (string)staticClass.InvokeStatic("StaticGenericMethod",
// new Type[] { typeof(string), typeof(Expression) },
// new object[] { "", new Expression() },
// new Type[] { typeof(Expression) });
//Assert.AreEqual("StaticGenericMethod", result);
}
}
}
Aucun commentaire:
Enregistrer un commentaire