如何使用反射API调用MemoryMarshal.Createspan?
假设我有此C#结构:
public struct Test
{
public int Code1;
public int Code2;
}
此代码正常工作:
var test = new Test { Code1 = 1, Code2 = 2 };
var bytes = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref test, 1));
在bytes
中,我将获得8个字节的数组)。
现在,如何使用反射API而不是预定义/通用类型来调用相同的方法 createSpan + asbytes ?
我已经为createSpan
part:
private static object GetBytes(object value)
{
var method = typeof(MemoryMarshal).GetMethod(nameof(MemoryMarshal.CreateSpan));
var gmethod = method.MakeGenericMethod(value.GetType());
var parameters = new object[] { value, 1 };
return gmethod.Invoke(null, parameters);
}
但是在Invoke
(使用相同的测试实例)上,它说:system.notsupportedexception:'指定方法不支持指定方法。'
ps:我想避免指针和Unsafe
关键字。
Let's suppose I have this C# structure:
public struct Test
{
public int Code1;
public int Code2;
}
This code works fine:
var test = new Test { Code1 = 1, Code2 = 2 };
var bytes = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref test, 1));
In bytes
, I will get an array of 8 bytes (1, 0, 0, 0, 2, 0, 0, 0).
Now, how can I call the same methods CreateSpan + AsBytes using Reflection API instead of a predefined/generic type?
I have tried this for the CreateSpan
part:
private static object GetBytes(object value)
{
var method = typeof(MemoryMarshal).GetMethod(nameof(MemoryMarshal.CreateSpan));
var gmethod = method.MakeGenericMethod(value.GetType());
var parameters = new object[] { value, 1 };
return gmethod.Invoke(null, parameters);
}
But on Invoke
(using the same test instance), it says: System.NotSupportedException: 'Specified method is not supported.'
PS: I would like to avoid pointers and unsafe
keyword.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用反射访问
span< t>
,因为它不能装箱(以防止堆栈引用逃脱)。您唯一可以做的就是创建一个通用函数,该功能可以完成您想要的所有操作,然后使用反射调用。
例如,您可以返回
byte []
阵列中的数组/strong>
You cannot access
Span<T>
using reflection because it cannot be boxed (to prevent stack references escaping).The only thing you can do is create a generic function that does all you want it to do, then call that using reflection.
For example, you could return a
byte[]
array out of a generic functiondotnetfiddle