实体框架 4:方法的通用返回类型
我有以下代码 -
var db = new DBEntities();
var entity = //get entity;
lblName.Text = string.Empty;
var names = entity.Names.OrderBy(x => x.Value).ToList();
for (var i = 0; i < names .Count; i++)
{
if (i == names .Count - 1) lblName.Text += names [i].Value + ".";
else lblName.Text += names [i].Value + ", ";
}
我将有几个像上面这样的 For 循环,它将格式化要在标签中显示的值。我正在尝试创建一个方法,当我传入集合和标签时,该方法将进行格式化,例如 -
void FormatValue(List<??> items, Label label)
{
//For loop
//Format value
}
我为列表传入什么。如何使这个足够通用,以便我能够将它用于所有 entity.Names
、entity.Xxx
、entity.Yyy
ETC?
I have the following code -
var db = new DBEntities();
var entity = //get entity;
lblName.Text = string.Empty;
var names = entity.Names.OrderBy(x => x.Value).ToList();
for (var i = 0; i < names .Count; i++)
{
if (i == names .Count - 1) lblName.Text += names [i].Value + ".";
else lblName.Text += names [i].Value + ", ";
}
I'll have several For loops like above which will format the value to be displayed in a label. I'm trying to make a method out of it which will do the formatting when I pass in the collection and the label, something like -
void FormatValue(List<??> items, Label label)
{
//For loop
//Format value
}
What do I pass in for the List. How do I make this generic enough so I'll be able to use it for all entity.Names
, entity.Xxx
, entity.Yyy
etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使方法本身通用并允许调用者指定格式化程序:
然后您可以像这样调用该方法:
Make the method itself generic and allow the caller to specify a formatter:
You can then call the method like: