在 C# 中调用泛型类型 T 作为属性.i:e [typeof(List)]

本文关键字:List typeof 调用 泛型类型 属性 | 更新日期: 2023-09-27 18:34:06

所有删除、添加和包含方法都在这个类中

public class ListClass<T>{}

我的属性类

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class ListMyDefault 
{
    public ListMyDefault(string name, Type objectType)
        : base(typeof(ListClass<>).MakeGenericType(objectType)) 
    {
            DefaultName = name;
            ObjectType = objectType;
    }
}

如何在属性中将上述属性与泛型 T 一起使用?typeof(T) 将是 List of SomeClass。

public myclass<T>
{ 
    [ListMyDefault("sample",typeof(T)] 
    public static List<T> Details{get;set;}
}

在 C# 中调用泛型类型 T 作为属性.i:e [typeof(List<T>)]

引用泛型类型参数的属性将导致编译时错误:

[CustomAttribute(info = typeof(GenericClass3<int, T, string>))]  //Error
class ClassD<T> { }

https://msdn.microsoft.com/en-us/library/ms173129.aspx

相关文章: