我有下面的类(如通过反射镜看到的)调用“内部外部的”构造函数使用反射
public class W : IDisposable
{
public W(string s);
public W(string s, byte[] data);
// more constructors
[MethodImpl(MethodImplOptions.InternalCall)]
internal extern W(string s, int i);
public static W Func(string s, int i);
}
我想称之为“内部外部的”构造函数或函数功能使用反射
MethodInfo dynMethod = typeof(W).GetMethod("Func", BindingFlags.Static);
object[] argVals = new object[] { "hi", 1 };
dynMethod.Invoke(null, argVals);
和
Type type = typeof(W);
Type[] argTypes = new Type[] { typeof(System.String), typeof(System.Int32) };
ConstructorInfo dynMethod = type.GetConstructor(BindingFlags.InvokeMethod | BindingFlags.NonPublic, null, argTypes, null);
object[] argVals = new object[] { "hi", 1 };
dynMethod.Invoke(null, argVals);
不幸的是,两种变体在尝试调用时都会出现NullReferenceException,所以,我一定做错了什么?
你用调试器检查了什么是空的?它是'dynMethod'吗? – OregonGhost 2010-04-16 16:48:31
是的,dynMethod在这两个示例中均为空 – Riz 2010-04-16 17:21:24