按字段逐字段读取结构 [英] Reading a struct, field by field
问题描述
有没有办法按字段读取结构字段?
我想编写一个相当通用的函数,例如:
公共字符串EncodeStruct(struct strct)
{
....
}
哪个接受一般结构类型,逐字段读取,每个字段编码
,具体取决于其类型(即整数字段编码是不同于布尔值的
),连接将结果转换为字符串并返回它。
Is there a way to read a struct field by field?
I want to write a rather general function, like:
public string EncodeStruct (struct strct)
{
....
}
which accepts a general struct type, reads it field by field, encodes
each field depending on its type (i.e. an integer field is encoding is
different than a boolean), concatenates the results into a string and
returns it.
推荐答案
Sadeq写道:
有没有办法按字段读取结构域?
我想编写一个相当通用的函数,如:
公共字符串EncodeStruct(struct strct)<接受一般结构类型,逐字段读取,根据类型对每个字段进行编码(即{> /
一个整数字段的编码与布尔值不同,连接起来将结果转换为字符串并将其返回。
Is there a way to read a struct field by field?
I want to write a rather general function, like:
public string EncodeStruct (struct strct)
{
...
}
which accepts a general struct type, reads it field by field, encodes
each field depending on its type (i.e. an integer field is encoding is
different than a boolean), concatenates the results into a string and
returns it.
查看Type.GetFields。
注意你不可能有像你现在那样的方法签名
。如果您使用的是.NET 2.0,则可以执行以下操作:
公共字符串EncodeStruct< T> (T值)其中T:struct
{
类型类型= typeof(T);
...
}
如果您使用的是1.1,那么您可能需要获得拳击的价值
值并签名方法只是EncodeStruct(对象
值)。
Jon
Have a look at Type.GetFields.
Note that you can''t have a method signature like the one you''ve got at
the moment. If you''re using .NET 2.0, you can do something like:
public string EncodeStruct<T> (T value) where T : struct
{
Type type = typeof(T);
...
}
If you''re using 1.1, you''ll probably need to take the hit of boxing the
value and make the signature of the method just EncodeStruct(object
value).
Jon
Jon Skeet [C#MVP]写道:
< snip>
Jon Skeet [C# MVP] wrote:
<snip>
请注意,你不能拥有像你这样的方法签名那时候已经到了。如果您使用的是.NET 2.0,则可以执行以下操作:
公共字符串EncodeStruct< T> (T值)其中T:struct
{
类型类型= typeof(T);
...
}
如果你是使用1.1,你可能需要攻击装箱的值
并使方法的签名只是EncodeStruct(对象
值)。
Note that you can''t have a method signature like the one you''ve got at
the moment. If you''re using .NET 2.0, you can do something like:
public string EncodeStruct<T> (T value) where T : struct
{
Type type = typeof(T);
...
}
If you''re using 1.1, you''ll probably need to take the hit of boxing the
value and make the signature of the method just EncodeStruct(object
value).
>
实际上,进一步考虑它,你必须至少一次击中
拳击,因为FieldInfo.GetValue将对象视为
参数。
Jon
Actually, thinking about it further, you''ll have to take the hit of
boxing at least once anyway, as FieldInfo.GetValue takes object as a
parameter.
Jon
非常感谢。这对我有很大的帮助。
我使用的是.NET 2.0语法,现在我可以实现自己的编码器了。
但是当我想编码嵌套结构时出现了另一个问题。我尝试递归使用它是
,但我编译器无法将获得的
字段(通常是一个对象)转换为结构。
有没有办法解决这个问题?
Thanks a lot. That was of great help.
I used .NET 2.0 syntax, and now I''m able to implement my own encoder.
But another issue arose when I wanted to encode nested structs. I tried
to use it recursively, but I the compiler cannot cast the obtained
field (which is an object, in general) to a struct.
Is there a way to overcome this issue?
这篇关于按字段逐字段读取结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!