简体   繁体   中英

UNITY C# - How can I get all the fields of the an instance of a class marked with my custom atribute(the fields)?

In Unity Ive got a object of a class with many fields. Some of them are marked with [CockpitControlled] attribute, which I defined as:

[AttributeUsage(AttributeTargets.Field)]
public class CockpitControlled : Attribute
{

}

In some other class I would to get a list of all those field as I would like to change them. Question is how?

By reflection, you get the fields of a type and check if the attribute is present.

Example:

[AttributeUsage(AttributeTargets.Field)]
public class CockpitControlled : Attribute
{
    public int SomeValue { get; set; }
}

public class SomeData
{
    private string _data1;
    [CockpitControlled]
    private string _data2;
    private string _data3;
    [CockpitControlled( SomeValue = 42)]
    private string _data4;
}

static void Main(string[] args)
{
    foreach(var field in typeof(SomeData).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
    {
        var att = field.GetCustomAttributes(typeof(CockpitControlled), true).FirstOrDefault() as CockpitControlled;
        if(att == null)
        {
            Console.WriteLine($"The field {field.Name} isn't CockpitControlled");
        }
        else
        {
            Console.WriteLine($"The field {field.Name} is CockpitControlled with the value {att.SomeValue}");
        }
    }
}

Ouput:

The field _data1 isn't CockpitControlled
The field _data2 is CockpitControlled with the value 0
The field _data3 isn't CockpitControlled
The field _data4 is CockpitControlled with the value 42

Edit:

This example set "bbb" in instance's fields that are CockpitControlled with the value "aaa":

[AttributeUsage(AttributeTargets.Field)]
public class CockpitControlled : Attribute
{ }

public class SomeData
{
    public string _data1;
    [CockpitControlled]
    public string _data2;
    [CockpitControlled]
    public string _data3;
}

static void Main()
{
    // Create a instance
    var datas = new SomeData { _data1 = "aaa", _data2 = "aaa", _data3 = "zzz",  };
    // For each field of the instance
    foreach (var field in datas.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
    {
        // Check if the field is CockpitControlled
        var att = field.GetCustomAttributes(typeof(CockpitControlled), true).FirstOrDefault() as CockpitControlled;
        if (att != null)
        {
            // Get the value of the field
            var curentValue = (string)field.GetValue(datas);
            // If the value of the field equal "aaa"
            if (curentValue == "aaa")
                // Set "bbb"
                field.SetValue(datas, "bbb");
        }
    }

    Console.WriteLine(datas._data1);
    Console.WriteLine(datas._data2);
    Console.WriteLine(datas._data3);
}

Result:

aaa
bbb
zzz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM