Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 has 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.

like image 430
seldary Avatar asked May 08 '11 05:05

seldary


1 Answers

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
like image 132
Gabe Avatar answered Oct 04 '22 08:10

Gabe