相关疑难解决方法(0)

属性上的自定义属性 - 获取属性的类型和值

我有以下自定义属性,可以应用于属性:

[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)

.net c# custom-attributes

29
推荐指数
1
解决办法
6万
查看次数

标签 统计

.net ×1

c# ×1

custom-attributes ×1