如何获取类中所有公共变量的列表? (C#)

发布于 2024-10-04 23:48:24 字数 343 浏览 11 评论 0原文

我有一个包含很多公共变量的类,我需要能够获取它们的列表。

这是我的班级的一个例子:

public class FeatList: MonoBehaviour {
public static Feat  Acrobatic = new Feat("Acrobatic", false, "");
public static Feat AgileManeuvers = new Feat("Agile Maneuvers", false, "" ); void Start(){}}

除了还有大约 100 个变量。有没有可能的方法将所有这些成员变量放入一个可管理的数组中?还是我把自己搞砸了?

I have a class that has A LOT of public variables and I need to be able to get a list of them.

Here's an example of my class:

public class FeatList: MonoBehaviour {
public static Feat  Acrobatic = new Feat("Acrobatic", false, "");
public static Feat AgileManeuvers = new Feat("Agile Maneuvers", false, "" ); void Start(){}}

Except there are about 100 more variables. Is there any possible way to get all these member variables in one manageable array? Or have I screwed myself over?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想你的星星会说话 2024-10-11 23:48:24

如果您在变量NAMES之后 - 那么这将为您提供它们:

IEnumerable<string> variableNames =
    typeof(FeatList).GetFields(BindingFlags.Instance | 
            BindingFlags.Static | BindingFlags.Public)
        .Select(f => f.Name);

但如果您想要VALUES,那么这将起作用:

Dictionary<string,object> variableValues =
    typeof (FeatList).GetFields(BindingFlags.Instance | 
            BindingFlags.Static | BindingFlags.Public)
        .ToDictionary(f => f.Name, f => f.GetValue(myFeatList));

if you're after the varaible NAMES - then this will give them to you:

IEnumerable<string> variableNames =
    typeof(FeatList).GetFields(BindingFlags.Instance | 
            BindingFlags.Static | BindingFlags.Public)
        .Select(f => f.Name);

but if you want the VALUES, then this will work:

Dictionary<string,object> variableValues =
    typeof (FeatList).GetFields(BindingFlags.Instance | 
            BindingFlags.Static | BindingFlags.Public)
        .ToDictionary(f => f.Name, f => f.GetValue(myFeatList));
撑一把青伞 2024-10-11 23:48:24

如果“变量”指的是类字段(如类级别的变量),您可以使用反射来获取访问权限,就像在这个 MSDN Microsoft 示例中使用 FieldInfo 类(有关详细信息,请参阅 MSDN 链接)

using System;
using System.Reflection;

public class FieldInfoClass
{
    public int myField1 = 0;
    protected string myField2 = null;
    public static void Main()
    {
        FieldInfo[] myFieldInfo;
        Type myType = typeof(FieldInfoClass);
        // Get the type and fields of FieldInfoClass.
        myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
            | BindingFlags.Public);
        Console.WriteLine("\nThe fields of " + 
            "FieldInfoClass are \n");
        // Display the field information of FieldInfoClass.
        for(int i = 0; i < myFieldInfo.Length; i++)
        {
            Console.WriteLine("\nName            : {0}", myFieldInfo[i].Name);
            Console.WriteLine("Declaring Type  : {0}", myFieldInfo[i].DeclaringType);
            Console.WriteLine("IsPublic        : {0}", myFieldInfo[i].IsPublic);
            Console.WriteLine("MemberType      : {0}", myFieldInfo[i].MemberType);
            Console.WriteLine("FieldType       : {0}", myFieldInfo[i].FieldType);
            Console.WriteLine("IsFamily        : {0}", myFieldInfo[i].IsFamily);
        }
    }
}

而不是查询在此示例中,您可以从 Main 方法中的 FieldInfoClass 中选择您的 FeatList 类。逻辑不需要位于同一类的主方法中。您可以将您的逻辑版本放置在要查询的实体外部,实际上可以使用这种逻辑查询任何对象或类。

这些字段是私有的、公共的还是其他字段并不重要——通过反射,您可以访问所有这些字段。

请参阅 MSDN 示例代码 FieldInfo.GetValue( ..) 方法(MSDN 链接) 了解如何使用反射提取字段的值。

If by "Variables" you mean class fields (like variables at the class level) you can use reflection to get access, like in this MSDN Microsoft example using the FieldInfo class (see MSDN link for more info)

using System;
using System.Reflection;

public class FieldInfoClass
{
    public int myField1 = 0;
    protected string myField2 = null;
    public static void Main()
    {
        FieldInfo[] myFieldInfo;
        Type myType = typeof(FieldInfoClass);
        // Get the type and fields of FieldInfoClass.
        myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
            | BindingFlags.Public);
        Console.WriteLine("\nThe fields of " + 
            "FieldInfoClass are \n");
        // Display the field information of FieldInfoClass.
        for(int i = 0; i < myFieldInfo.Length; i++)
        {
            Console.WriteLine("\nName            : {0}", myFieldInfo[i].Name);
            Console.WriteLine("Declaring Type  : {0}", myFieldInfo[i].DeclaringType);
            Console.WriteLine("IsPublic        : {0}", myFieldInfo[i].IsPublic);
            Console.WriteLine("MemberType      : {0}", myFieldInfo[i].MemberType);
            Console.WriteLine("FieldType       : {0}", myFieldInfo[i].FieldType);
            Console.WriteLine("IsFamily        : {0}", myFieldInfo[i].IsFamily);
        }
    }
}

Instead of querying the FieldInfoClass in this example from the Main method you can choose your FeatList class. The logic does not need to be in a main method of the same class. You can place your version of the logic external to the entity you want to query and in fact query any object or class with this kind of logic.

It doesn't matter if the fields are private or public or something else - through reflection you can get access to all of them.

See the MSDN sample code at FieldInfo.GetValue(..) method (MSDN link) for how to extract the field's value using reflection.

还给你自由 2024-10-11 23:48:24

这将返回一个包含 Feat 类型的所有公共字段的 FieldInfo 数组:

var fields = typeof(Feat).GetFields();

然后您可以像这样读/写字段:

var field1 = fields[0];
var field1value = field1.GetValue(Acrobatic);

当然,GetValue 返回无类型对象,因此您需要根据需要将其转换为正确的类型。

This will returns an FieldInfo array of all public fields of Feat type:

var fields = typeof(Feat).GetFields();

Then you may read/write fields like this:

var field1 = fields[0];
var field1value = field1.GetValue(Acrobatic);

Of cource, GetValue returns untyped object, so you need to cast it to the correct type on demand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文