Код: Выделить всё
internal class Foo {
// Non-static. This works!
private T TestThisMethod1(T value) {
Console.WriteLine("Called TestThisMethod1");
return value;
}
// Static. Can't get this to work!
private static T TestThisMethod2(T value) {
Console.WriteLine("Called TestThisMethod2");
return value;
}
// Static. Can't get this to work!
private static void TestThisMethod3(T value) {
Console.WriteLine("Called TestThisMethod3");
}
// Static. Can't get this to work!
private static void TestThisMethod4(T value, T2 value2) {
Console.WriteLine("Called TestThisMethod4");
}
}
Код: Выделить всё
[TestMethod]
public void PrivateStaticGenericMethodTest() {
int value = 40;
var foo = new Foo();
// This works. It's not static though.
PrivateObject privateObject = new PrivateObject(foo);
int result1 = (int)privateObject.Invoke("TestThisMethod1", new Type[] { typeof(int) }, new Object[] { value }, new Type[] { typeof(int) });
// Fails
int result2 = (int)privateObject.Invoke("TestThisMethod2", BindingFlags.Static | BindingFlags.NonPublic, new Type[] { typeof(int) }, new Object[] { value }, new Type[] { typeof(int) });
// Fails
PrivateType privateType = new PrivateType(typeof(Foo));
int result2_1 = (int)privateType.InvokeStatic("TestThisMethod2", new Type[] { typeof(int) }, new Object[] { value }, new Type[] { typeof(int) });
// Fails
int result2_2 = (int)privateType.InvokeStatic("TestThisMethod2", BindingFlags.Static | BindingFlags.NonPublic, new Type[] { typeof(int) }, new Object[] { value }, new Type[] { typeof(int) });
// Stopping here. I can't even get TestThisMethod2 to work...
}
Подробнее здесь: https://stackoverflow.com/questions/420 ... in-c-sharp