C# Reflection Call Method with ref/Pointer Parameter

I want to call with reflection each Method of an Class but I cant call Methods with an pointer as Reference. Normally I just could pass null for each pointer I find and everything would be fine. The Problem is when the function trys to access my passed pointer. The pointer would be an reference to the memory address 0 and that is obviously an instant crash for my Program. So I need to pass an pointer to an valid memory adress, but I dont know how I can create an pointer during runtime

A compressed Version of my Code:

class Program
{
    static void Main(string[] args)
    {
        var obj = new TestClass();
        var pointerMethod = obj.GetType().GetMethod("Test");
        var referenceMethod = obj.GetType().GetMethod("Test2");

        var pointerParameter = pointerMethod.GetParameters()[0];
        var referenceParameter = referenceMethod.GetParameters()[0];

        //Get the instance of the class that is referred by the pointer/reference
        var pointerInstance = Activator.CreateInstance(pointerParameter.ParameterType.GetElementType());
        var referenceInstance = Activator.CreateInstance(referenceParameter.ParameterType.GetElementType());

        //Call the method and "cast" the char instance into an pointer
        pointerMethod.Invoke(obj, new[] {pointerInstance.GetType().MakePointerType()});
        referenceMethod.Invoke(obj, new[] { referenceInstance.GetType().MakeByRefType() });
     }
}

And TestClass:

public class TestClass
{
        unsafe public void Test(char* pointer)
        {

        }

        public void Test2(ref int reference)
        {

        }
}

I've always get the Exception "System.RuntimeType" cannot be converted to type 'System.Char*' The weird thing is that pointerInstance.GetType().MakePointerType() returns a char*, exactly that what I have to pass into the function...

Jon Skeet
people
quotationmark

First, let's separate this into pointer vs ref - they're handled slightly differently.

It's not clear what pointer value you expected the method to receive - particularly as the value you passed to Invoke was a type, not an instance of the type - but you should use Pointer.Box:

using System.Reflection;

class TestPointer
{    
    unsafe static void Main()
    {
        char c = 'x';
        char* p = &c;
        object boxedPointer = Pointer.Box(p, typeof(char*));

        var method = typeof(TestPointer).GetMethod("Foo");
        method.Invoke(null, new[] { boxedPointer });
        Console.WriteLine("After call, c = {0}", c);
    }

    public static unsafe void Foo(char *pointer)
    {
        Console.WriteLine("Received: {0}", *pointer);
        *pointer = 'y';
    }
}

Output:

Received: x
After call, c = y

For ref parameters, it's slightly simpler - you just allow for normal boxing, and the argument array is changed:

using System.Reflection;

class TestRef
{    
    unsafe static void Main()
    {
        var method = typeof(TestRef).GetMethod("Foo");
        var args = new object[] { 10 };
        method.Invoke(null, args);
        Console.WriteLine("After call, args[0] = {0}", args[0]);
    }

    public static unsafe void Foo(ref int x)
    {
        Console.WriteLine("Received: {0}", x);
        x = 20;
    }
}

people

See more on this question at Stackoverflow