gpt4 book ai didi

c# - 在 C# 中调用泛型 T 作为属性。我 :e [typeof(List)]

转载 作者:行者123 更新时间:2023-11-30 16:04:15 25 4
gpt4 key购买 nike

所有的remove、add和contains方法都在这个类中

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) 将是 SomeClass 的列表。

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

最佳答案

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

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

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

关于c# - 在 C# 中调用泛型 T 作为属性。我 :e [typeof(List<T>)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35336029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com