Tại sao biên dịch sau đây?Generics and Casting
public IList<T> Deserialize<T>(string xml)
{
if (typeof(T) == typeof(bool))
return (IList<T>)DeserializeBools(xml);
return null;
}
private static IList<bool> DeserializeBool(string xml) { ... do stuff ... }
Nhưng điều này không
public MyClass<T> GetFromDb<T>(string id)
{
if (typeof(T) == typeof(bool))
return (MyClass<T>)GetBoolValue(id); <-- compiler error here
return null;
}
private static MyClass<bool> GetBoolValue(string id) { ... do stuff ... }
Lỗi biên dịch là gì? –
Lỗi nói gì? –
Không thể truyền biểu thức loại 'MyClass' để nhập 'MyClass ' –
Magpie