泛型错误
The type parameter 'T' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint
如果T是接口
这是错误的 :return System.Activator.CreateInstance(reType) as T;
正确的写法 T t1=(T)System.Activator.CreateInstance(reType) ;
错误的
public class EventIDDictionary<T> where T : MKProtocol
{
private MKHandler defaultHandler;
private Dictionary<String, Type> productModuleDictionary = new Dictionary<string, Type>();
private Dictionary<String, Type> ActionDictionary = new Dictionary<string, Type>();
public void AddDictionary(String eventId, Type type)
{
if (type == null)
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + ",type不能为null");
}
if (String.IsNullOrEmpty(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId不能为空,type=" + type.FullName);
}
if(eventId.Length==12){
if (ActionDictionary.ContainsKey(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + "type=" + type.FullName + "已经存在,现有type=" + ActionDictionary[eventId].FullName);
}
ActionDictionary.Add(eventId, type);
}
else if (eventId.Length == 6)
{
if (productModuleDictionary.ContainsKey(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + "type=" + type.FullName + "已经存在,现有type=" + productModuleDictionary[eventId].FullName);
}
productModuleDictionary.Add(eventId, type);
}
else
{
throw new ServerInitException("增加AddDictionary,eventId位数不对=" + eventId + "type=" + type.FullName);
}
}
public void DeleteDictionary(String eventId)
{
if (String.IsNullOrEmpty(eventId))
{
throw new ServerInitException("删除DeleteDictionary,eventId不能为空");
}
if (eventId.Length == 12)
{
if (ActionDictionary.ContainsKey(eventId))
{
ActionDictionary.Remove(eventId);
}
}
if (eventId.Length == 6)
{
if (productModuleDictionary.ContainsKey(eventId))
{
productModuleDictionary.Remove(eventId);
}
}
}
public virtual T GetHandler(MKEntity entity)
{
Type reType=null;
if (entity == null || String.IsNullOrEmpty(entity.EventID))
return default(T);
if (entity.EventID.Length != 12)
{
return default(T);
}
if (ActionDictionary.ContainsKey(entity.EventID))
{
reType= ActionDictionary[entity.EventID];
}
if (productModuleDictionary.ContainsKey(entity.EventID.Substring(0, 6)))
{
reType= productModuleDictionary[entity.EventID.Substring(0, 6)];
}
return System.Activator.CreateInstance(reType) as T;
}
}
这是正确的
public class EventIDDictionary<T> where T : MKProtocol
{
private MKHandler defaultHandler;
private Dictionary<String, Type> productModuleDictionary = new Dictionary<string, Type>();
private Dictionary<String, Type> ActionDictionary = new Dictionary<string, Type>();
public void AddDictionary(String eventId, Type type)
{
if (type == null)
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + ",type不能为null");
}
if (String.IsNullOrEmpty(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId不能为空,type=" + type.FullName);
}
if(eventId.Length==12){
if (ActionDictionary.ContainsKey(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + "type=" + type.FullName + "已经存在,现有type=" + ActionDictionary[eventId].FullName);
}
ActionDictionary.Add(eventId, type);
}
else if (eventId.Length == 6)
{
if (productModuleDictionary.ContainsKey(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + "type=" + type.FullName + "已经存在,现有type=" + productModuleDictionary[eventId].FullName);
}
productModuleDictionary.Add(eventId, type);
}
else
{
throw new ServerInitException("增加AddDictionary,eventId位数不对=" + eventId + "type=" + type.FullName);
}
}
public void DeleteDictionary(String eventId)
{
if (String.IsNullOrEmpty(eventId))
{
throw new ServerInitException("删除DeleteDictionary,eventId不能为空");
}
if (eventId.Length == 12)
{
if (ActionDictionary.ContainsKey(eventId))
{
ActionDictionary.Remove(eventId);
}
}
if (eventId.Length == 6)
{
if (productModuleDictionary.ContainsKey(eventId))
{
productModuleDictionary.Remove(eventId);
}
}
}
public virtual T GetHandler(MKEntity entity)
{
Type reType=null;
if (entity == null || String.IsNullOrEmpty(entity.EventID))
return default(T);
if (entity.EventID.Length != 12)
{
return default(T);
}
if (ActionDictionary.ContainsKey(entity.EventID))
{
reType= ActionDictionary[entity.EventID];
}
if (productModuleDictionary.ContainsKey(entity.EventID.Substring(0, 6)))
{
reType= productModuleDictionary[entity.EventID.Substring(0, 6)];
}
T t1=(T)System.Activator.CreateInstance(reType) ;
return t1;
}
}
The type parameter 'T' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint
如果T是接口
这是错误的 :return System.Activator.CreateInstance(reType) as T;
正确的写法 T t1=(T)System.Activator.CreateInstance(reType) ;
错误的
public class EventIDDictionary<T> where T : MKProtocol
{
private MKHandler defaultHandler;
private Dictionary<String, Type> productModuleDictionary = new Dictionary<string, Type>();
private Dictionary<String, Type> ActionDictionary = new Dictionary<string, Type>();
public void AddDictionary(String eventId, Type type)
{
if (type == null)
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + ",type不能为null");
}
if (String.IsNullOrEmpty(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId不能为空,type=" + type.FullName);
}
if(eventId.Length==12){
if (ActionDictionary.ContainsKey(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + "type=" + type.FullName + "已经存在,现有type=" + ActionDictionary[eventId].FullName);
}
ActionDictionary.Add(eventId, type);
}
else if (eventId.Length == 6)
{
if (productModuleDictionary.ContainsKey(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + "type=" + type.FullName + "已经存在,现有type=" + productModuleDictionary[eventId].FullName);
}
productModuleDictionary.Add(eventId, type);
}
else
{
throw new ServerInitException("增加AddDictionary,eventId位数不对=" + eventId + "type=" + type.FullName);
}
}
public void DeleteDictionary(String eventId)
{
if (String.IsNullOrEmpty(eventId))
{
throw new ServerInitException("删除DeleteDictionary,eventId不能为空");
}
if (eventId.Length == 12)
{
if (ActionDictionary.ContainsKey(eventId))
{
ActionDictionary.Remove(eventId);
}
}
if (eventId.Length == 6)
{
if (productModuleDictionary.ContainsKey(eventId))
{
productModuleDictionary.Remove(eventId);
}
}
}
public virtual T GetHandler(MKEntity entity)
{
Type reType=null;
if (entity == null || String.IsNullOrEmpty(entity.EventID))
return default(T);
if (entity.EventID.Length != 12)
{
return default(T);
}
if (ActionDictionary.ContainsKey(entity.EventID))
{
reType= ActionDictionary[entity.EventID];
}
if (productModuleDictionary.ContainsKey(entity.EventID.Substring(0, 6)))
{
reType= productModuleDictionary[entity.EventID.Substring(0, 6)];
}
return System.Activator.CreateInstance(reType) as T;
}
}
这是正确的
public class EventIDDictionary<T> where T : MKProtocol
{
private MKHandler defaultHandler;
private Dictionary<String, Type> productModuleDictionary = new Dictionary<string, Type>();
private Dictionary<String, Type> ActionDictionary = new Dictionary<string, Type>();
public void AddDictionary(String eventId, Type type)
{
if (type == null)
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + ",type不能为null");
}
if (String.IsNullOrEmpty(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId不能为空,type=" + type.FullName);
}
if(eventId.Length==12){
if (ActionDictionary.ContainsKey(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + "type=" + type.FullName + "已经存在,现有type=" + ActionDictionary[eventId].FullName);
}
ActionDictionary.Add(eventId, type);
}
else if (eventId.Length == 6)
{
if (productModuleDictionary.ContainsKey(eventId))
{
throw new ServerInitException("增加AddDictionary,eventId=" + eventId + "type=" + type.FullName + "已经存在,现有type=" + productModuleDictionary[eventId].FullName);
}
productModuleDictionary.Add(eventId, type);
}
else
{
throw new ServerInitException("增加AddDictionary,eventId位数不对=" + eventId + "type=" + type.FullName);
}
}
public void DeleteDictionary(String eventId)
{
if (String.IsNullOrEmpty(eventId))
{
throw new ServerInitException("删除DeleteDictionary,eventId不能为空");
}
if (eventId.Length == 12)
{
if (ActionDictionary.ContainsKey(eventId))
{
ActionDictionary.Remove(eventId);
}
}
if (eventId.Length == 6)
{
if (productModuleDictionary.ContainsKey(eventId))
{
productModuleDictionary.Remove(eventId);
}
}
}
public virtual T GetHandler(MKEntity entity)
{
Type reType=null;
if (entity == null || String.IsNullOrEmpty(entity.EventID))
return default(T);
if (entity.EventID.Length != 12)
{
return default(T);
}
if (ActionDictionary.ContainsKey(entity.EventID))
{
reType= ActionDictionary[entity.EventID];
}
if (productModuleDictionary.ContainsKey(entity.EventID.Substring(0, 6)))
{
reType= productModuleDictionary[entity.EventID.Substring(0, 6)];
}
T t1=(T)System.Activator.CreateInstance(reType) ;
return t1;
}
}