У меня есть следующий настраиваемый атрибут, который можно применить к свойствам:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class IdentifierAttribute : Attribute
{
}
Например:
public class MyClass
{
[Identifier()]
public string Name { get; set; }
public int SomeNumber { get; set; }
public string SomeOtherProperty { get; set; }
}
Будут также другие классы, к которым атрибут Идентификатор может быть добавлен к свойствам другого типа:
public class MyOtherClass
{
public string Name { get; set; }
[Identifier()]
public int SomeNumber { get; set; }
public string SomeOtherProperty { get; set; }
}
Затем мне нужно получить эту информацию в моем классе-потребителе. Например:
public class TestClass<T>
{
public void GetIDForPassedInObject(T obj)
{
var type = obj.GetType();
//type.GetCustomAttributes(true)???
}
}
Какой лучший способ сделать это? Мне нужно получить тип поля [Идентификатор()] (int, string и т.д.) И фактическое значение, очевидно, основанное на типе.