简体   繁体   中英

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

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; }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM