我遇到了一个我从未见过的错误。我希望有人能提供帮助。错误7,参数1:无法从''转换为''
这里是我的代码:
public class MyT
{
public int ID { get; set; }
public MyT Set(string Line)
{
int x = 0;
this.ID = Convert.ToInt32(Line);
return this;
}
}
public class MyList<T> : List<T> where T : MyT, new()
{
internal T Add(T n)
{
Read();
Add(n);
return n;
}
internal MyList<T> Read()
{
Clear();
StreamReader sr = new StreamReader(@"../../Files/" + GetType().Name + ".txt");
while (!sr.EndOfStream)
Add(new T().Set(sr.ReadLine())); //<----Here is my error!
sr.Close();
return this;
}
}
public class Customer : MyT
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Item : MyT
{
public int ID { get; set; }
public string Category { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
public class MyClass
{
MyList<Customer> Customers = new MyList<Customer>();
MyList<Item> Items = new MyList<Item>();
}
在那说,该行 “添加(新T()设置(sr.ReadLine()));”我得到“Error 7,参数1:无法从'Simple_Reservation_System.MyT'转换为'T'”。有人可以帮我解决这个问题。
坦率地说,这是不是很“普通”,你为什么不只是使用列表 MYLIST里面? –
2013-05-10 07:35:41