[英]C# Get Instance of type char*
is it possible to get an instance of an pointer during runtime? 是否有可能在运行时获取指针的实例? Something like:
就像是:
Type x = typeof(char*);
How do I get a instance of this pointer? 我如何获得该指针的实例? The problem is that I only have a Object with an Type inside.
问题是我只有一个带有类型的对象。 And I know that the Type is a pointer.
而且我知道Type是一个指针。 But now I need a instance of the pointer.
但是现在我需要一个指针实例。
You can: 您可以:
Type typeChar = typeof(char);
Type ptrChar = typeChar.MakePointerType(); // char*
and back: 然后回来:
if (ptrChar.IsPointer)
{
Type typeChar2 = ptrChar.GetElementType(); // char
}
But perhaps what you want is something different: 但是也许您想要的是不同的东西:
public class TestBool
{
unsafe public void Test(bool* pointer)
{
*pointer = true;
}
public void Test2(ref bool reference)
{
reference = true;
}
}
public class TestChar
{
unsafe public void Test(char* pointer)
{
*pointer = 'A';
}
public void Test2(ref char reference)
{
reference = 'B';
}
}
public static void TestPointer(object obj, Type parType)
{
var pointerMethod = obj.GetType().GetMethod("Test");
Type parType2;
// Non-blittable types aren't directly supported.
// See https://msdn.microsoft.com/en-us/library/75dwhxf7.aspx
// We cheat a little.
if (parType == typeof(bool))
{
parType2 = typeof(byte);
}
else if (parType == typeof(char))
{
parType2 = typeof(short);
}
else if (parType.IsEnum)
{
parType2 = Enum.GetUnderlyingType(parType);
}
else
{
parType2 = parType;
}
object obj2 = Activator.CreateInstance(parType2);
GCHandle handle = default(GCHandle);
try
{
handle = GCHandle.Alloc(obj2, GCHandleType.Pinned);
pointerMethod.Invoke(obj, new object[] { handle.AddrOfPinnedObject() });
}
finally
{
if (handle.IsAllocated)
{
handle.Free();
}
}
if (parType == typeof(bool))
{
obj2 = (byte)obj2 != 0;
}
else if (parType == typeof(char))
{
obj2 = (char)(short)obj2;
}
else if (parType.IsEnum)
{
obj2 = Enum.ToObject(parType, obj2);
}
Console.WriteLine(obj2);
}
public static void TestReference(object obj, Type parType)
{
var referenceMethod = obj.GetType().GetMethod("Test2");
object obj2 = Activator.CreateInstance(parType);
var pars = new object[] { obj2 };
referenceMethod.Invoke(obj, pars);
Console.WriteLine(pars[0]);
}
private static void Main(string[] args)
{
{
var obj = new TestBool();
TestPointer(obj, typeof(bool));
TestReference(obj, typeof(bool));
}
{
var obj = new TestChar();
TestPointer(obj, typeof(char));
TestReference(obj, typeof(char));
}
}
See how for the TestPointer
, ignoring some problems with non-blittable types, I simply use GCHandle
to obtain a pointer to the value and then pass it directly to the method. 看看
TestPointer
,忽略非可迁移类型的一些问题,我简单地使用GCHandle
来获取指向该值的指针,然后将其直接传递给该方法。 No unsafe code necessary :-) 无需不安全的代码:-)
For TestReference
it is much easier: I only need to create the parameters array separately, and then the CLR will put in that array the modified values. 对于
TestReference
它要容易得多:我只需要单独创建parameters数组,然后CLR会将修改后的值放入该数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.