我有以下自定义属性,可以应用于属性:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class IdentifierAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
例如:
public class MyClass
{
[Identifier()]
public string Name { get; set; }
public int SomeNumber { get; set; }
public string SomeOtherProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
还有其他类,Identifier属性可以添加到不同类型的属性中:
public class MyOtherClass
{
public string Name { get; set; }
[Identifier()]
public int SomeNumber { get; set; }
public string SomeOtherProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后,我需要能够在我的消费类中获取此信息.例如:
public class TestClass<T>
{
public void GetIDForPassedInObject(T obj)
{
var type …Run Code Online (Sandbox Code Playgroud)