【发布时间】:2013-06-24 10:55:42
【问题描述】:
public static T Process<T>(this string key)
where T:bool,string, DateTime
{
var tType = typeof(T);
if(tType == typeof(DateTime))
{
return DateTime.Parse(key.InnerProcess());
}
else if(tType == typeof(bool))
{
return bool.Parse(key.InnerProcess());
}
else if(tType == typeof(string))
{
return key.InnerProcess();
}
}
它说它不能从 bool 类型转换为 T,或 datetime 到 T.. 如何做到这一点?
innerPrecess() 给了我一个字符串。我想将其解析为给定参数的类型,然后返回。
【问题讨论】:
-
您的类型参数约束无论如何都不起作用。 T 需要是它们的all。不幸的是,这在这里是相互排斥的。
-
你的约束都不是有效的:如果你指定一个类型作为约束,它必须要么是一个接口,要么是一个未密封的类。
bool和DateTime是结构体,string是密封类。
标签: c# asp.net oop c#-4.0 c#-3.0