谁能解释一下为什么调用下面的“DoTest1”方法是一个问题?
至于为什么我需要将传入的 GridCore 对象仍然转换为泛型类型 T 即使我指定 T 派生自 GridCore 与我的 where T : GridCore?
谢谢
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
MyTest<MyAlbum> mytest = new MyTest<MyAlbum>();
mytest.DoTest1(new MyAlbum());
mytest.DoTest2(new MyAlbum());
}
}
public class GridCore { }
public class MyAlbum : GridCore
{
public string Title { get; set; }
}
public class MyTest<T> where T : GridCore
{
private List<T> _list = new List<T>();
public void DoTest1(GridCore ma)
{
//_list.Add(ma); <-- why doesn't this work?
_list.Add((T)ma);
}
public void DoTest2(T ma)
{
_list.Add(ma);
}
}