How to check if a generic parameter is dynamic in

2020-07-05 05:51发布

I have a class ObjectMapper<T> . Is there any way in .NET 4.0 to tell if typeof(T) is dynamic? I want to be able to determine inside a member method whether the class was initialized as new ObjectMapper<dynamic>() vs. new ObjectMapper<SomeConcreteClass>().

标签: c# .net-4.0
2条回答
你好瞎i
2楼-- · 2020-07-05 06:05

There is no CLR type called dynamic. The C# compiler makes all dynamic values of type object and then calls custom binding code to figure out how to handle them. If dynamic was used, it will show up as Object.

查看更多
甜甜的少女心
3楼-- · 2020-07-05 06:12

You do this by checking if an instance is of type IDynamicMetaObjectProvider or you can check whether the type implements IDynamicMetaObjectProvider.

查看更多
登录 后发表回答