具有不常见泛型类型的接口列表
List of interfaces with uncommon generic type
我知道 Java 可以为 ex 创建容器。通用接口
无需指定类型。这在 C# 中有可能吗?
public interface Interface1<T>{
void ProcessModel(T source);
}
public class Implement : Interface1<ModelClass>{
.....implementation
}
我需要像这样实例化一些容器
public List<Interface1> temp = new List<Interface1>();
而不是这个
public List<Interface1<Implement>> temp = new List<Interface1<Implement>>();
您可以简单地创建一个 non-generic base-interface 来扩展您的容器,而不是为您的容器使用通用接口。现在您可以将所有这些派生实例放入您的容器中:
interface IBase { }
interface IGenericInterface<T> : IBase { }
class MyClass : IGenericInterface<string> { }
class MyContainer {
List<IBase> impl = new List<IBase>();
void Main() {
impl.Add(new MyClass());
}
}
我知道 Java 可以为 ex 创建容器。通用接口 无需指定类型。这在 C# 中有可能吗?
public interface Interface1<T>{
void ProcessModel(T source);
}
public class Implement : Interface1<ModelClass>{
.....implementation
}
我需要像这样实例化一些容器
public List<Interface1> temp = new List<Interface1>();
而不是这个
public List<Interface1<Implement>> temp = new List<Interface1<Implement>>();
您可以简单地创建一个 non-generic base-interface 来扩展您的容器,而不是为您的容器使用通用接口。现在您可以将所有这些派生实例放入您的容器中:
interface IBase { }
interface IGenericInterface<T> : IBase { }
class MyClass : IGenericInterface<string> { }
class MyContainer {
List<IBase> impl = new List<IBase>();
void Main() {
impl.Add(new MyClass());
}
}