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...
                        
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
Invokewas a type, not an instance of the type - but you should usePointer.Box:Output:
For
refparameters, it's slightly simpler - you just allow for normal boxing, and the argument array is changed: