简体   繁体   English

UNITY C# - 如何获取标有自定义属性(字段)的 class 实例的所有字段?

[英]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.在 Unity 中,我得到了一个 object 的 class 有很多字段。 Some of them are marked with [CockpitControlled] attribute, which I defined as:其中一些标有 [CockpitControlled] 属性,我将其定义为:

[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.在其他一些 class 中,我想获得所有这些字段的列表,因为我想更改它们。 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":此示例在 CockpitControlled 的实例字段中设置“bbb”,值为“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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 如何从基类中的派生类获取特定类型的所有字段? - C# How do I get all the fields of a specific type from a derived class within the base class? 自定义ViewModel类-除非指定前缀,否则并非所有字段都标记为无效 - Custom ViewModel Class - Not all fields are marked invalid unless a prefix is specified 如何在C#中找到对象的所有公共字段? - How can I find all the public fields of an object in C#? 我如何从C#中的类对象提取字段 - How can i extract fields from a class object in c# UNITY,我如何从另一个 C# class 获取所有属性并将它们放入枚举 - UNITY, How can I get all properties from another C# class and put them into an enum 迭代获取ac#类的所有字段和数据类型 - get all fields and datatypes of a c# class iteratively Unity C#-如何做到这一点,以便可以通过一个类访问所有其他类,变量和方法? - Unity c# - How can I make it so that I can access all my other classes, variables and methods via a single class? Roslyn C#:如何获取方法访问的所有字段和属性(及其所属类) - Roslyn C#: How to get all fields and properties (and their belonging class) accessed by a method C#:如何使用嵌套类和所有字段枚举类 - C#: How to enumerate class with nested classes and with all fields 当我需要更新 LinQ C#.Net Framework 中的记录时,如何验证所有字段? - How can I validate all fields when I need to update a record in LinQ C# .Net Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM