How do I get a list of all the public variables I have in a Class? (C#)
I have a class that has A LOT of开发者_StackOverflow社区 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?
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));
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.
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.
精彩评论