How do you determine if a method returns a dynamic type when using reflection?

本小妞迷上赌 提交于 2020-01-03 19:33:10

问题


When using reflection, fields, properties, indexers, and parameters can be examined for the DynamicAttribute attribute in order to determine that they have a dynamic type. However, this doesn't work for Methods - even though they return 'dynamic', they have NO attributes and their return type is 'object' (and also has no attributes). Visual Studio does this for intellisense for methods in external assemblies... can it be done with reflection?

For example, the code below produces this output:

dynamicField is dynamic
DynamicProperty is dynamic
Item is dynamic
DynamicMethod is NOT dynamic!!
dynamicParameter is dynamic

Example code:

using System;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace ReflectDynamics
{
    class Program
    {
        static void Main()
        {
            var test = typeof(Test);
            CheckDynamic(test.GetField("dynamicField"));
            CheckDynamic(test.GetProperty("DynamicProperty"));
            CheckDynamic(test.GetProperty("Item"));
            MethodInfo dynamicMethod = test.GetMethod("DynamicMethod");
            CheckDynamic(dynamicMethod);
            CheckDynamic(dynamicMethod.GetParameters()[0]);
            Console.ReadKey();
        }

        static void CheckDynamic(MemberInfo memberInfo)
        {
            bool isDynamic = memberInfo.GetCustomAttributes(typeof(DynamicAttribute), true).Length > 0;
            Console.WriteLine(memberInfo.Name + (isDynamic ? " is dynamic" : " is NOT dynamic!!"));
        }
        static void CheckDynamic(ParameterInfo parameterInfo)
        {
            bool isDynamic = parameterInfo.GetCustomAttributes(typeof(DynamicAttribute), true).Length > 0;
            Console.WriteLine(parameterInfo.Name + (isDynamic ? " is dynamic" : " is NOT dynamic!!"));
        }
    }

    class Test
    {
        public dynamic dynamicField;
        public dynamic DynamicProperty { get { return dynamicField; } }
        public dynamic this[int index] { get { return dynamicField; } }
        public dynamic DynamicMethod(dynamic dynamicParameter) { return dynamicField; }
    }
}

回答1:


This is because you need to check the Attributes on the ReturnTypeCustomAttributes from the method.

Modify your CheckDynamic method like so:

static void CheckDynamic(MemberInfo memberInfo)
{
    bool isDynamic = memberInfo.GetCustomAttributes(typeof(DynamicAttribute), true).Length > 0;

    var methodInfo = (memberInfo as MethodInfo);
    if (methodInfo != null)
    {
        isDynamic = methodInfo.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(DynamicAttribute), true).Length > 0;
    }

    Console.WriteLine(memberInfo.Name + (isDynamic ? " is dynamic" : " is NOT dynamic!!"));
}

This probably needs some defensive coding, but it's just a quick and dirty proof of concept.



来源:https://stackoverflow.com/questions/8439786/how-do-you-determine-if-a-method-returns-a-dynamic-type-when-using-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!