C# 传递类型参数并声明列表 公共静态列表getList(类型customModel) { 列表圣殿骑士=新列表(10); 返回圣殿骑士; }
是否可以返回从其他地方传递的类型列表? 我一直在做我自己的项目,注意到如果有任何方法可以做到这一点,我的代码会简单得多。你的意思是这样使用:C# 传递类型参数并声明列表 公共静态列表getList(类型customModel) { 列表圣殿骑士=新列表(10); 返回圣殿骑士; },c#,reflection,C#,Reflection,是否可以返回从其他地方传递的类型列表? 我一直在做我自己的项目,注意到如果有任何方法可以做到这一点,我的代码会简单得多。你的意思是这样使用: public static List<customModel> getList(type customModel) { List<customModel> tempList = new List<customModel>(10); return tempList; } public static List&l
public static List<customModel> getList(type customModel)
{
List<customModel> tempList = new List<customModel>(10);
return tempList;
}
public static List<T> getList<T>()
{
List<T> tempList = new List<T>(10);
return tempList;
}
public static List<T> getList<T>()
{
return new List<T>(10);
}
问题是,您可以同样轻松地执行以下操作:
List<customModel> newList = getList<customModel>();
var newList=新列表(10);
您的意思是这样使用:
public static List<customModel> getList(type customModel)
{
List<customModel> tempList = new List<customModel>(10);
return tempList;
}
public static List<T> getList<T>()
{
List<T> tempList = new List<T>(10);
return tempList;
}
public static List<T> getList<T>()
{
return new List<T>(10);
}
问题是,您可以同样轻松地执行以下操作:
List<customModel> newList = getList<customModel>();
var newList=新列表(10);
使用通用的:
var newList = new List<customModel>(10);
公共静态列表getList()
{
返回新列表(10);
}
可以这样称呼:
public static List<customModel> getList(type customModel)
{
List<customModel> tempList = new List<customModel>(10);
return tempList;
}
public static List<T> getList<T>()
{
List<T> tempList = new List<T>(10);
return tempList;
}
public static List<T> getList<T>()
{
return new List<T>(10);
}
var myList=getList();
使用通用的:
var newList = new List<customModel>(10);
公共静态列表getList()
{
返回新列表(10);
}
可以这样称呼:
public static List<customModel> getList(type customModel)
{
List<customModel> tempList = new List<customModel>(10);
return tempList;
}
public static List<T> getList<T>()
{
List<T> tempList = new List<T>(10);
return tempList;
}
public static List<T> getList<T>()
{
return new List<T>(10);
}
var myList=getList();
向我们展示一些您希望发生的事情的伪代码。也许我们可以按照你的意图理解你的问题。我不明白为什么有这么多人讨厌这个question@climbage我也在想同样的事情。一旦你知道了它是什么,答案可能会非常明显,但如果你还不知道“泛型”这个词,我想这是一个很难找到的答案。@ean5533对,当你从来没有听说过它的时候,你怎么用谷歌搜索“泛型”?@Grampage-我认为它最初的措辞很糟糕,我们需要一些例子,没有人是读心术的。给我们看一些你想发生的事情的伪代码。也许我们可以按照你的意图理解你的问题。我不明白为什么有这么多人讨厌这个question@climbage我也在想同样的事情。一旦你知道了它是什么,答案可能会非常明显,但如果你还不知道“泛型”这个词,我想这是一个很难找到的答案。@ean5533对,当你从来没有听说过它的时候,你怎么用谷歌搜索“泛型”?@Grampage-我认为它最初的措辞很糟糕,我们需要一些例子,没有人是读心术的,我相信你失踪了something@user1713153:如果var
是一个问题,请参阅我编辑的代码var
是C#3.0中引入的一项功能,var
在这里是不必要的,不幸的是,它在很少需要的时候被过度使用了。我相信你错过了something@user1713153:如果var
是一个问题,请参阅我编辑的代码var
是C#3.0中引入的一项功能,var
在这里是不必要的,不幸的是,它在很少需要时被过度使用。