sel*_*ary 6 reflection unmanaged c++-cli
我的应用程序使用反射来分析运行时的c ++/cli代码.
我需要确定一个类型是否有一个没有非托管参数的构造函数(指针等),因为我希望以后使用:
ConstructorInfo constructorInfo;
// ...
var ret = constructorInfo.Invoke(BindingFlags..., null, myParameters, null);
Run Code Online (Sandbox Code Playgroud)
如果构造函数具有指向非托管对象的指针作为参数,则在向其传递null时会出现转换异常.
所以我如何确定?没有IsManaged ......并且IsPointer在这种情况下没有帮助.
目前尚不清楚您的问题实际上是什么,但这里有一个简短的演示程序,它显示了传递null给构造函数,该构造函数将指针作为参数并使用以下命令检测它IsPointer:
using System;
using System.Reflection;
namespace pointers
{
unsafe class Program
{
public Program(int* x)
{
Console.WriteLine("It worked!");
}
static void Main(string[] args)
{
ConstructorInfo[] c = typeof(Program).GetConstructors();
c[0].Invoke(BindingFlags.Default, null, new object[] { null }, null);
Console.WriteLine(c[0].GetParameters()[0].ParameterType.IsPointer);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它打印:
有效! 真的