泛型错误 The type parameter 'T' cannot be used with the 'as' operator

泛型错误
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;


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值