public class Base
{
private List<string> _stringList;
protected void InitStringList()
{
_stringList= new List<string>();
Type type = this.GetType();
FieldInfo[] fieldInfos = type.GetFields();
foreach (var field in fieldInfos)
{
foreach (var customAttribute in field.CustomAttributes)
{
if(customAttribute.AttributeType == typeof(ComponentAttribute))
{
//---------------打印测试-----------------//
Log((string)field.GetValue(this));
this._stringList.Add((string)field.GetValue(this));
}
}
}
}
}
[AttributeUsage(AttributeTargets.Field)]
class ComponentAttribute:Attribute
{
}
public class Son : Base
{
[Component]
private string a = "test";
public Son()
{
base.InitStringList();
}
}