开发者

How do i determine if a ConstructorInfo object has an unmanaged parameter?

My app uses reflection to analyze c++/cli code in runtime.

I need to determine if a type ha开发者_如何学Pythons a constructor without unmanaged parameters (pointers and such), because i want later on to use:

ConstructorInfo constructorInfo;  
// ...  
var ret = constructorInfo.Invoke(BindingFlags..., null, myParameters, null);  

if the constructor has a pointer to an unmanaged object as a parameter, there is a casting exception when i pass null to it.

So i how do i determine that? there is no IsManaged... and IsPointer does not help in this case.


It's not clear what your problem actually is, but here is a short demonstration program that shows passing null to a constructor that takes a pointer as an argument and detects it with 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);
        }
    }
}

It prints:

It worked!
True


Try testing if the parameter is a value type. null isn't a valid value for any value type, whether unmanaged pointer or simply int.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜