相关疑难解决方法(0)

如何测试typeof(动态)?

我有一个泛型方法TResult Foo<TSource, TResult>(IEnumerable<TSource> source),如果TResult声明dynamic我想执行一个不同的代码路径而不是其他类型的声明.

对于常规类型,您可以执行以下操作:

if (typeof(TResult) == typeof(int))
    return ExpressionFactory.CreateExpandoFunction<TSource, TResult>();
Run Code Online (Sandbox Code Playgroud)

但是if (typeof(TResult) == typeof(dynamic))没有编译.

无论如何,在使用声明调用方法时,在运行时进行此类确定:

dyanmic x = Foo<int, dynamic>(list);
Run Code Online (Sandbox Code Playgroud)

由于动态本身不是一种类型,我应该测试什么?IDynamicMetaObjectProvider

编辑 这是System.Linq.Expression评估程序的SQL文本的一部分.分支if的特定愿望TResult是动态的是某些伪逻辑看起来像这样:

if (type is struct)
   create selector that initializes each element to result values
else if (type is class)
   create selector that initialize each element to new instance and set member properties
else if (type is dynamic)
   create selector that initializes each element to …
Run Code Online (Sandbox Code Playgroud)

c# dynamic .net-4.0 c#-4.0

8
推荐指数
1
解决办法
6862
查看次数

标签 统计

.net-4.0 ×1

c# ×1

c#-4.0 ×1

dynamic ×1