dka*_*man 8 c# dynamic .net-4.0 c#-4.0
我有一个泛型方法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 new `ExpandoObject` and populates/sets member properties
Run Code Online (Sandbox Code Playgroud)
Jar*_*Par 10
简单地说,你不能,因为没有类型动态.在类型中,如果类型出现在元数据中,则动态写为具有附加特殊属性(动态)的对象.基本上,对于大多数用途,typeof(动态)与typeof(对象)没有什么不同.
| 归档时间: |
|
| 查看次数: |
6862 次 |
| 最近记录: |