C# 遍历结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/661834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:20:43  来源:igfitidea点击:

Iterate Through a Struct?

c#.net

提问by MagicAndi

I would like to be able to iterate through the values of a struct in C# (.Net 2.0). This is to be done at runtime, with no knowledge of the possible values in the struct.

我希望能够遍历 C# (.Net 2.0) 中结构的值。这将在运行时完成,不知道结构中可能的值。

I was thinking along the lines of either using Reflection to add the struct values to a list, or converting the struct to a data structure that implements the IEnumerable interface. Can anyone provide any pointers?

我正在考虑使用反射将结构值添加到列表中,或者将结构转换为实现 IEnumerable 接口的数据结构。任何人都可以提供任何指示吗?

Thanks in advance for your help.

在此先感谢您的帮助。

Regards, Andy.

问候,安迪。

采纳答案by Jon Skeet

What exactly do you mean - the various fields within a struct? Or properties perhaps? If so, Type.GetFields()or Type.GetProperties()is the way to go.

你到底是什么意思 - 结构中的各个字段?或者可能是属性?如果是这样,Type.GetFields()或者Type.GetProperties()是要走的路。

Are you absolutely sure you need to use a struct, by the way? That's rarely the best design decision in C#, particularly if the struct contains multiple values.

顺便说一句,您绝对确定需要使用结构吗?这很少是 C# 中的最佳设计决策,尤其是在结构包含多个值的情况下。

EDIT: Yes, it seems that structs are being used for legacy reasons.

编辑:是的,似乎由于遗留原因正在使用结构。

One thing I didn't mention before: if the struct's fields aren't public, you'll need to specify appropriate BindingFlags (e.g. BindingFlags.Instance | BindingFlags.NonPublic).

我之前没有提到的一件事:如果结构体的字段不是公开的,则需要指定适当的 BindingFlags(例如BindingFlags.Instance | BindingFlags.NonPublic)。

回答by Mehrdad Afshari

To make it work on every struct, you have to use reflection. If you want to declare a set of structs with this ability, you can make them implement IEnumerable<KeyValuePair<string, object>>and define GetEnumerator()as:

为了使其适用于每个结构,您必须使用反射。如果要声明一组具有此功能的结构,可以使它们实现IEnumerable<KeyValuePair<string, object>>并定义GetEnumerator()为:

yield return new KeyValuePair<string, object>("Field1", Field1);
yield return new KeyValuePair<string, object>("Field2", Field2);
// ... and so forth

回答by jason

At the simplest level, assuming that you want to iterate over the properties:

在最简单的层面上,假设您要遍历属性:

PropertyInfo[] properties = myStructInstance.GetType().GetProperties();
foreach (var property in properties) {
    Console.WriteLine(property.GetValue(myStructInstance, null).ToString());
}

回答by gimel

See the system.reflection.propertyinfodocument for an example of using Reflection.

有关system.reflection.propertyinfo使用反射的示例,请参阅文档。

回答by blfuentes

I use something like the following:

我使用类似以下内容:

[StructLayout(LayoutKind.Sequential)]
public struct MY_STRUCT
{
   public uint aa;
   public uint ab;
   public uint ac;
}

MY_STRUCT pMS = new MY_STRUCT();

FieldInfo[] fields = pMS.GetType().GetFields();
foreach (var xInfo in fields)
   Console.WriteLine(xInfo.GetValue(pMS).ToString());