6

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.

seldary
  • 6,186
  • 4
  • 40
  • 55
  • Why doesn't `IsPointer` help? – Gabe May 09 '11 at 04:53
  • Because many other managed objects will return True for IsPointer (i.e arrays)... – seldary May 09 '11 at 08:42
  • A *pointer* to an array will return True for `IsPointer`. Also, a C++ array is represented as a pointer, so a pointer to an object and an array of objects are essentially identical. None of those are considered actual array types in .NET though. – Gabe May 09 '11 at 14:55

2 Answers2

2

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
Gabe
  • 84,912
  • 12
  • 139
  • 238
  • See comment for the question... IsPointer does return True, but so as for other managed types - I need a test that will return true for unmanaged AND false for any other type. Thanks! – seldary May 09 '11 at 08:46
  • @seldary: If you think there's a non-pointer type whose `IsPointer` returns True, please tell me what it is. But I'm still unclear on why you can't pass `null` for an actual pointer. – Gabe May 09 '11 at 14:45
-1

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • In particular, you can say `int* x = null;` so any pointer can be null. – Gabe May 09 '11 at 05:02
  • @Gabe: All pointers are value types. And the null value for a pointer is different from a `null` object reference, which is what it sounds like @seldary is passing. Or at least that's my understanding of what the question says... – Ben Voigt May 09 '11 at 05:18
  • They may technically be values, but `Console.WriteLine(typeof(int*).IsValueType);` prints `False`, so pointers are not considered value types by the runtime. In particular, they do not derive from `System.ValueType`. – Gabe May 09 '11 at 05:22
  • @Gabe: .NET represents unmanaged pointers with `System::IntPtr`, which I believe does derive from `System::ValueType`. – Ben Voigt May 09 '11 at 05:47
  • An unmanaged pointer may be cast back and forth between `IntPtr`, but pointers have their own representation in the CTS. Section 8 of the CLR spec (ECMA 335 Partition I) makes it quite clear that pointers are reference types (but not objects). See Figure 1, Section 8.2.1, and Section 8.9.2 for places where this is made explicit. – Gabe May 09 '11 at 06:03