C# 返回列表<;这>;
是否可以使用C# 返回列表<;这>;,c#,generics,inheritance,.net-4.5,C#,Generics,Inheritance,.net 4.5,是否可以使用this.GetType()作为列表的类型? 我有以下由多个对象继承的类: public class MainRepository { // ??? should be the type of this public List<???> GetAll() { return new List<???>(); } } 公共类存储库 { //??应该是这种类型 公共列表GetAll() { 返回新列表(); } }
this.GetType()
作为列表的类型?
我有以下由多个对象继承的类
:
public class MainRepository
{
// ??? should be the type of this
public List<???> GetAll()
{
return new List<???>();
}
}
公共类存储库
{
//??应该是这种类型
公共列表GetAll()
{
返回新列表();
}
}
我知道它可以作为一种通用方法来完成,如下所示:
public List<T> GetAll<T>()
{
return new List<T>();
}
public List GetAll()
{
返回新列表();
}
但是我想知道,如果不在调用方法中明确定义类型,是否可以这样做。我相信您可以这样做,如果这对您来说是一个可行的选择的话:
public class MainRepository<T>
{
public List<T> GetAll()
{
return new List<T>();
}
}
公共类存储库
{
公共列表GetAll()
{
返回新列表();
}
}
如果我没有弄错的话,这应该允许您在不指定方法调用中的类型的情况下调用方法(尽管您显然必须为类指定类型)
我假设您这样做是为了拥有一些通用的存储库,它可以是子类的,或者类似的东西?然后你可以做一些类似的事情(只是一个粗略的想法):
公共类BaseRepo{
}
公共类MainRepository:BaseRepo,其中T:BaseRepo{
公共列表GetAll(){
返回新列表();
}
}
如果这对您来说是一个可行的选择,我相信您可以这样做:
public class MainRepository<T>
{
public List<T> GetAll()
{
return new List<T>();
}
}
公共类存储库
{
公共列表GetAll()
{
返回新列表();
}
}
如果我没有弄错的话,这应该允许您在不指定方法调用中的类型的情况下调用方法(尽管您显然必须为类指定类型)
我假设您这样做是为了拥有一些通用的存储库,它可以是子类的,或者类似的东西?然后你可以做一些类似的事情(只是一个粗略的想法):
公共类BaseRepo{
}
公共类MainRepository:BaseRepo,其中T:BaseRepo{
公共列表GetAll(){
返回新列表();
}
}
如果这对您来说是一个可行的选择,我相信您可以这样做:
public class MainRepository<T>
{
public List<T> GetAll()
{
return new List<T>();
}
}
公共类存储库
{
公共列表GetAll()
{
返回新列表();
}
}
如果我没有弄错的话,这应该允许您在不指定方法调用中的类型的情况下调用方法(尽管您显然必须为类指定类型)
我假设您这样做是为了拥有一些通用的存储库,它可以是子类的,或者类似的东西?然后你可以做一些类似的事情(只是一个粗略的想法):
公共类BaseRepo{
}
公共类MainRepository:BaseRepo,其中T:BaseRepo{
公共列表GetAll(){
返回新列表();
}
}
如果这对您来说是一个可行的选择,我相信您可以这样做:
public class MainRepository<T>
{
public List<T> GetAll()
{
return new List<T>();
}
}
公共类存储库
{
公共列表GetAll()
{
返回新列表();
}
}
如果我没有弄错的话,这应该允许您在不指定方法调用中的类型的情况下调用方法(尽管您显然必须为类指定类型)
我假设您这样做是为了拥有一些通用的存储库,它可以是子类的,或者类似的东西?然后你可以做一些类似的事情(只是一个粗略的想法):
公共类BaseRepo{
}
公共类MainRepository:BaseRepo,其中T:BaseRepo{
公共列表GetAll(){
返回新列表();
}
}
这只能通过反射实现,因为只有在运行时才知道对象的真实类型。因此,您必须设计方法以返回所有列表的公共基类,即.NET中的对象,并在方法中动态创建列表
public object GetAll()
{
return System.Activator.CreateInstance(typeof(List<>).MakeGenericType(this.GetType()));
}
public对象GetAll()
{
return System.Activator.CreateInstance(typeof(List).MakeGenericType(this.GetType());
}
但是,我不明白您为什么要这样做。这只能通过反射实现,因为对象的真实类型只有在运行时才知道。因此,您必须设计方法以返回所有列表的公共基类,即.NET中的对象,并在方法中动态创建列表
public object GetAll()
{
return System.Activator.CreateInstance(typeof(List<>).MakeGenericType(this.GetType()));
}
public对象GetAll()
{
return System.Activator.CreateInstance(typeof(List).MakeGenericType(this.GetType());
}
但是,我不明白您为什么要这样做。这只能通过反射实现,因为对象的真实类型只有在运行时才知道。因此,您必须设计方法以返回所有列表的公共基类,即.NET中的对象,并在方法中动态创建列表
public object GetAll()
{
return System.Activator.CreateInstance(typeof(List<>).MakeGenericType(this.GetType()));
}
public对象GetAll()
{
return System.Activator.CreateInstance(typeof(List).MakeGenericType(this.GetType());
}
但是,我不明白您为什么要这样做。这只能通过反射实现,因为对象的真实类型只有在运行时才知道。因此,您必须设计方法以返回所有列表的公共基类,即.NET中的对象,并在方法中动态创建列表
public object GetAll()
{
return System.Activator.CreateInstance(typeof(List<>).MakeGenericType(this.GetType()));
}
public对象GetAll()
{
return System.Activator.CreateInstance(typeof(List).MakeGenericType(this.GetType());
}
但是,我不明白您为什么要这样做。您可以始终将列表定义为list
,然后将项目强制转换为您想要的任何类型。为什么不使用静态方法返回类类型?public static List GetAll()泛型方法有什么问题?您可以将类本身设置为泛型public class main repository
,这样您就可以将方法更改为public List GetAll()
,但我认为没有其他方法了。您想解决什么更大的问题?为什么不直接返回List
您可以将列表定义为List
,然后将项目强制转换为您想要的任何类型。为什么不使用静态方法返回类类型?public static List GetAll()通用方法有什么问题吗?您