我如何得到所有的公共变​​量我有一个类的列表? (C#) [英] How do I get a list of all the public variables I have in a Class? (C#)

查看:102
本文介绍了我如何得到所有的公共变​​量我有一个类的列表? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有公共变量了很多类,我需要能够得到它们的列表。

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

下面是我的类的例子:

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

除了有大约100多个变量。是否有任何可能的方式来获得一个可管理的阵列中的所有这些成员变量?还是我自己搞砸了?

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?

推荐答案

如果你varaible之后是的名字 - 那么这将给予他们给你:

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);

但如果你想在,然后这将工作:

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

这篇关于我如何得到所有的公共变​​量我有一个类的列表? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆