C# 访问泛型参数的属性

C# 访问泛型参数的属性,c#,generics,attributes,C#,Generics,Attributes,如何访问泛型参数的属性?我的代码无法获取属性: [AttributeUsage(AttributeTargets.GenericParameter)] class MyAttribute : Attribute { public string name; } class A<[MyAttribute(name = "Genric")] Type> { public static void f() { MyAttribute w = Attrib

如何访问泛型参数的属性?我的代码无法获取属性:

[AttributeUsage(AttributeTargets.GenericParameter)]
class MyAttribute : Attribute
{
    public string name;
}
class A<[MyAttribute(name = "Genric")] Type>
{
    public static void f()
    {
        MyAttribute w = Attribute.GetCustomAttributes(typeof(Type))[0] as MyAttribute; // fails
        Console.WriteLine(w?.name);
    }
}
[AttributeUsage(AttributeTargets.GenericParameter)]
类MyAttribute:Attribute
{
公共字符串名称;
}
甲级
{
公共静态void f()
{
MyAttribute w=Attribute.GetCustomAttributes(typeof(Type))[0]作为MyAttribute;//失败
控制台写入线(w?.name);
}
}

您要查找的属性与
A
相关,而不是
类型
。所以你必须从那里开始。看看这个例子:

using System;

public class Program
{
    public static void Main()
    {
        var genericArguments = typeof(A<>).GetGenericArguments();
        var attributes = Attribute.GetCustomAttributes(genericArguments[0]);
        Console.WriteLine((attributes[0] as MyAttribute).Name);
    }
}

[AttributeUsage(AttributeTargets.GenericParameter)]
class MyAttribute : Attribute
{
    public string Name;
}

class A<[MyAttribute(Name = "MyAttributeValue")] Type>
{
}
使用系统;
公共课程
{
公共静态void Main()
{
var genericalarguments=typeof(A).getgenericalarguments();
var attributes=Attribute.GetCustomAttributes(genericArguments[0]);
Console.WriteLine((属性[0]作为MyAttribute.Name);
}
}
[AttributeUsage(AttributeTargets.GenericParameter)]
类MyAttribute:Attribute
{
公共字符串名称;
}
甲级
{
}
输出是

MyAttributeValue


您要查找的属性与
A
相关,而不是
类型
。所以你必须从那里开始。看看这个例子:

using System;

public class Program
{
    public static void Main()
    {
        var genericArguments = typeof(A<>).GetGenericArguments();
        var attributes = Attribute.GetCustomAttributes(genericArguments[0]);
        Console.WriteLine((attributes[0] as MyAttribute).Name);
    }
}

[AttributeUsage(AttributeTargets.GenericParameter)]
class MyAttribute : Attribute
{
    public string Name;
}

class A<[MyAttribute(Name = "MyAttributeValue")] Type>
{
}
使用系统;
公共课程
{
公共静态void Main()
{
var genericalarguments=typeof(A).getgenericalarguments();
var attributes=Attribute.GetCustomAttributes(genericArguments[0]);
Console.WriteLine((属性[0]作为MyAttribute.Name);
}
}
[AttributeUsage(AttributeTargets.GenericParameter)]
类MyAttribute:Attribute
{
公共字符串名称;
}
甲级
{
}
输出是

MyAttributeValue


该属性应用于泛型参数,而不是类型本身,因此您当前的方法不起作用

请尝试以下方法:

MyAttribute w = typeof(A<>)
    .GetGenericArguments()
    .Select(t => t.GetCustomAttribute<MyAttribute>())
    .SingleOrDefault();
MyAttribute w=typeof(A)
.GetGenericArguments()的
.Select(t=>t.GetCustomAttribute())
.SingleOrDefault();

该属性应用于泛型参数,而不是类型本身,因此您当前的方法不起作用

请尝试以下方法:

MyAttribute w = typeof(A<>)
    .GetGenericArguments()
    .Select(t => t.GetCustomAttribute<MyAttribute>())
    .SingleOrDefault();
MyAttribute w=typeof(A)
.GetGenericArguments()的
.Select(t=>t.GetCustomAttribute())
.SingleOrDefault();
可能的重复:可能的重复: