How to identify members returning and or accepting the dynamic type

69 Views Asked by At

Is there any possibility to figure out whether a return or argument type of a member is dynamic? The issue is that is compiles down to System.Object.

I've observed that if a member accepting arguments has a dynamic type present in its signature, an additional argument A_0 will be added.

enter image description here

Are there any more tricks to figure out that a type is dynamic using reflection?

I'm using dnlib as my reflection library. The solution should work for all dotnet platforms; however, if it is limited to .NET Core, no biggie.

The project I'm working on is a Markdown documentation generator for C# libraries - you can find it here.

Thank you for any suggestions.

Update

Identifying types that are not part of generic type arguments is simple.

If a return type is dynamic, then the method/property getter/property setter/delegate/event adder will have an additional argument in their signature called A_0.

If an argument is dynamic, then is will be decorated with a custom attribute: System.Runtime.CompilerServices.DynamicAttribute.

However, if the dynamic type is an argument of some generic type (Dictionary<object, dynamic>), then none of the generic argument types will be decorated with the custom attribute above. If the given generic type is returned by a member, then the additional argument will be present in the members signature. Same goes for if the generic type is an argument, it will be decorated with a custom attribute.

These indicators can be reliably used only if the generic type only either has only one argument or there is no other object type argument:

  • <object, dynamic> NOT OK
  • <int, dynamic> OK
  • <dynamic> OK

Neither the additional argument nor the custom attribute indicates which generic argument is dynamic.

So far, I do not see a solution for this issue.

0

There are 0 best solutions below