aba*_*hev 29 .net c# generics operators on-the-fly
我正在为CLR脚本编写.NET On-the-Fly编译器,并希望执行方法使得泛型可接受:
object Execute()
{
return type.InvokeMember(..);
}
T Execute<T>()
{
return Execute() as T; /* doesn't work:
The type parameter 'T' cannot be used with the 'as' operator because
it does not have a class type constraint nor a 'class' constraint */
// also neither typeof(T) not T.GetType(), so on are possible
return (T) Execute(); // ok
}
Run Code Online (Sandbox Code Playgroud)
但我认为运算符as
将非常有用:如果结果类型不是T
方法将返回null
,而不是异常!有可能吗?
Dan*_*ker 59
你需要添加
where T : class
Run Code Online (Sandbox Code Playgroud)
你的方法声明,例如
T Execute<T>() where T : class
{
Run Code Online (Sandbox Code Playgroud)
顺便说一句,作为一个建议,通用包装器并没有真正增加太多价值.来电者可以写:
MyClass c = whatever.Execute() as MyClass;
Run Code Online (Sandbox Code Playgroud)
或者如果他们想要失败:
MyClass c = (MyClass)whatever.Execute();
Run Code Online (Sandbox Code Playgroud)
通用包装器方法如下所示:
MyClass c = whatever.Execute<MyClass>();
Run Code Online (Sandbox Code Playgroud)
所有三个版本必须指定完全相同的三个实体,只是按不同的顺序,所以没有更简单或更方便,但通用版本隐藏了正在发生的事情,而"原始"版本每个都清楚地表明是否会是一个投掷或一个null
.
(如果您的示例已从实际代码中简化,这可能与您无关).
Sam*_*uel 12
您不能将as
操作符用于没有限制的泛型类型.由于as
运算符使用null来表示它不是类型,因此不能在值类型上使用它.如果要使用obj as T
,T
则必须是引用类型.
T Execute<T>() where T : class
{
return Execute() as T;
}
Run Code Online (Sandbox Code Playgroud)
这段代码是as关键字的异常安全替代:
return Execute() is T value ? value : default(T)
Run Code Online (Sandbox Code Playgroud)
它使用 C# 7 引入的模式匹配功能。如果您不想将泛型参数限制为引用类型,请使用它