我有如下功能.
private IList<ContactList> getEmptyRow()
{
var _ContactList = new List<ContactList>();
_ContactList.Add(new ContactList()
{
Email = string.Empty,
Name = string.Empty
});
return _ContactList;
}
Run Code Online (Sandbox Code Playgroud)
我想把我的班级ContactList改为
private IList<T> getEmptyRow() { ..... T.Add(new T()....
Run Code Online (Sandbox Code Playgroud)
这可能吗 ?如何 ?
每个建议都将不胜感激.
也许这样的事情?
public interface IPerson
{
string Email {get;set;}
string Name {get;set;}
}
public class SomeClass
{
private IList<T> getEmptyRow<T>() where T : IPerson, new()
{
var t = new List<T>();
t.Add(new T()
{
Email = string.Empty,
Name = string.Empty
});
return t;
}
}
Run Code Online (Sandbox Code Playgroud)
您需要一个接口或基类,以便您可以设置公共属性Email,Name并且您需要new()约束,以便可以使用默认构造函数.
| 归档时间: |
|
| 查看次数: |
1049 次 |
| 最近记录: |