具有固定缓冲区类型的ExpressionTree [英] ExpressionTree with fixed buffer type
问题描述
如何用表达式树表示固定缓冲区并获取其长度?
在下面的代码中,当我尝试获取固定字符数组的长度时,它通过异常。
我如何构造一个可以返回固定char []长度的表达式?
我的结构定义如下。
  struct 测试
{
public int MyVal ;
; public uint MyVal2 ;
公共跨度> 不安全 < span style ="color:#009695"> fixed char 名称 [ 10 ];
  ; public char [] Str ;
}
var structExp = 表达式 。 参数 ( typeof ( 测试 ), " result" );
var fieldStrExp = 表达式 。 字段 ( structExp , typeof ( 测试 ) 。 GetField ( " ; Str" ));
var < span style ="color:#222222"> fieldNameExp < span style ="color:#222222"> = 表达式 。 字段 ( structExp , typeof ( 测试 ) 。 GetField ( "名称" ));
var newArray = 表达式 。 NewArrayInit ( fieldStrExp 。
表达式 。 分配 ( fieldStrExp , newArray );
表达式 。 ArrayLength ( fieldStrExp );
//这很好,得到正确的值2
表达式 。 ArrayLength ( fieldNameExp ) ;
这不合适。
Rongchen Jia,
感谢您发帖这里。
对于你的问题,我用不安全的代码测试代码。你可以获得固定char名称的长度,因为Expression.ArrayLength()用于数组而不是char名称。
最好的问候,
温迪&NBSP;
how can i represent fixed buffer with expression tree and get its length?
in the code below, when i try to get length of fixed char array, it through an exception.
how can i construct an expression that can return fixed char[]'s length?
my struct is defined like this.
struct Test
{
public int MyVal;
public uint MyVal2;
public unsafe fixed char Name[10];
public char[] Str;
}
var structExp = Expression.Parameter(typeof(Test), "result");
var fieldStrExp = Expression.Field(structExp, typeof(Test).GetField("Str"));
var fieldNameExp = Expression.Field(structExp, typeof(Test).GetField("Name"));
var newArray = Expression.NewArrayInit(fieldStrExp.Type.GetElementType(), new[] { Expression.Constant('c'), Expression.Constant('d') });
Expression.Assign(fieldStrExp, newArray);
Expression.ArrayLength(fieldStrExp); //this is fine and get right value of 2
Expression.ArrayLength(fieldNameExp); this is not fine.
Hi Rongchen Jia,
Thank you for posting here.
For your question, I test the code with unsafe code. You could get the length of fixed char Name because the Expression.ArrayLength() is used for array not for char Name.
Best Regards,
Wendy
这篇关于具有固定缓冲区类型的ExpressionTree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!