与REF /指针参数C#反射调用方法 [英] C# Reflection Call Method with ref/Pointer Parameter
问题描述
我想与反思的每个类的方法来调用,但我不能调用方法与指针作为参考。通常我只可以传递null每个指针我发现,一切都将被罚款。问题是,当函数改掉访问我传递指针。指针将是内存地址0的参考,这是很明显对我的计划瞬间崩溃。
所以我需要一个指针传递给一个有效的内存ADRESS,但我不知道我怎么可以创建在运行时的指针
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
一个压缩我的代码版本
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() });
}
}
和TestClass的:
And TestClass:
public class TestClass
{
unsafe public void Test(char* pointer)
{
}
public void Test2(ref int reference)
{
}
}
我总是得到异常System.RuntimeType不能转换为类型'System.Char *'
奇怪的是,pointerInstance.GetType ().MakePointerType()返回一个char *,正是我要传递给函数...
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...
推荐答案
第一,让我们这个分成指针VS REF
- 他们正在处理略有不同。
First, let's separate this into pointer vs ref
- they're handled slightly differently.
目前还不清楚是什么指针值您的有望的方法来领取 - 尤其当你传递给调用
是一个类型,而不是类型的实例的价值 - 但你应该使用 Pointer.Box
:
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
有关 REF
参数,它是稍微简单 - 你只允许正常的拳击,和参数数组改变了:
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;
}
}
这篇关于与REF /指针参数C#反射调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!