C# 在接口中创建泛型列表属性
我需要制作一个这样的界面:C# 在接口中创建泛型列表属性,c#,C#,我需要制作一个这样的界面: public interface ISomething { List<T> ListA { get; set; } List<T> ListB { get; set; } } public class Something01: ISomething { public List<string> ListA { get; set; } public List<Person> ListB {
public interface ISomething
{
List<T> ListA { get; set; }
List<T> ListB { get; set; }
}
public class Something01: ISomething
{
public List<string> ListA { get; set; }
public List<Person> ListB { get; set; }
}
public class Something02: ISomething
{
public List<int> ListA { get; set; }
public List<string> ListB { get; set; }
}
public class Something01: ISomething<string, Person>
{
public List<string> ListA { get; set; }
public List<Person> ListB { get; set; }
}
公共接口
{
列表ListA{get;set;}
列表ListB{get;set;}
}
然后像这样实现它:
public interface ISomething
{
List<T> ListA { get; set; }
List<T> ListB { get; set; }
}
public class Something01: ISomething
{
public List<string> ListA { get; set; }
public List<Person> ListB { get; set; }
}
public class Something02: ISomething
{
public List<int> ListA { get; set; }
public List<string> ListB { get; set; }
}
public class Something01: ISomething<string, Person>
{
public List<string> ListA { get; set; }
public List<Person> ListB { get; set; }
}
public class something 01:ISomething
{
公共列表列表a{get;set;}
公共列表列表b{get;set;}
}
或者像这样:
public interface ISomething
{
List<T> ListA { get; set; }
List<T> ListB { get; set; }
}
public class Something01: ISomething
{
public List<string> ListA { get; set; }
public List<Person> ListB { get; set; }
}
public class Something02: ISomething
{
public List<int> ListA { get; set; }
public List<string> ListB { get; set; }
}
public class Something01: ISomething<string, Person>
{
public List<string> ListA { get; set; }
public List<Person> ListB { get; set; }
}
public class something 02:ISomething
{
公共列表列表a{get;set;}
公共列表列表b{get;set;}
}
但从这个角度看,似乎我必须在接口类的顶部定义T。这迫使我在实现时为所有属性指定一种特定类型。这能做到吗
谢谢您可以使接口成为通用接口,为每个需要不同类型的属性提供一个类型参数,例如:
public interface ISomething<TA, TB>
{
List<TA> ListA{ get; set; }
List<TB> ListB {get; set; }
}
公共接口
{
列表ListA{get;set;}
列表ListB{get;set;}
}
然后像这样使用它:
public interface ISomething
{
List<T> ListA { get; set; }
List<T> ListB { get; set; }
}
public class Something01: ISomething
{
public List<string> ListA { get; set; }
public List<Person> ListB { get; set; }
}
public class Something02: ISomething
{
public List<int> ListA { get; set; }
public List<string> ListB { get; set; }
}
public class Something01: ISomething<string, Person>
{
public List<string> ListA { get; set; }
public List<Person> ListB { get; set; }
}
public class something 01:ISomething
{
公共列表列表a{get;set;}
公共列表列表b{get;set;}
}
您可以使用
public interface ISomething<T, U>
{
List<T> ListA{ get; set; }
List<U> ListB {get; set;}
}
公共接口
{
列表ListA{get;set;}
列表ListB{get;set;}
}
所以当你定义你的类时
public class Something : ISomething<Person, string>
{
List<Person> ListA{ get; set; }
List<string> ListB {get; set;}
}
公共类某物:等轴物
{
列表ListA{get;set;}
列表ListB{get;set;}
}
“但从其他帖子来看,我似乎不得不在最上面定义T
我的接口类。它强制我为所有
实现时的属性。”
对。但您可以定义任意多个通用参数,而不仅仅是单个参数。因此,在您的情况下,这应该可以做到:
public interface ISomething<T, S>
{
List<T> ListA{ get; set; }
List<S> ListB {get; set;}
}
公共接口
{
列表ListA{get;set;}
列表ListB{get;set;}
}
现在,您可以提供两种完全独立的类型:
class MyClass : ISomething<Type1, Type2> { ... }
class MyClass:ISomething{…}
试试这段代码
public interface ISomething<T, K>
{
List<T> ListA { get; set; }
List<K> ListB { get; set; }
}
public class Something01 : ISomething<string, Person>
{
public List<string> ListA { get; set; }
public List<Person> ListB { get; set; }
}
公共接口
{
列表ListA{get;set;}
列表ListB{get;set;}
}
公共类某物01:等轴物
{
公共列表列表a{get;set;}
公共列表列表b{get;set;}
}
您也可以将接口设置为通用的:公共接口是一件事
,但是您需要两种通用类型来完成您想要的操作。我知道这是摆在我面前的事情-谢谢大家!