你如何判断的方法使用反射时,返回一个动态类型? [英] How do you determine if a method returns a dynamic type when using reflection?

查看:114
本文介绍了你如何判断的方法使用反射时,返回一个动态类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用反射,字段,属性,索引和参数可以以确定它们具有动态类型进行检查的DynamicAttribute属性。然而,这并不对方法的工作 - 即使它们返回动态,它们没有属性和它们的返回类型是对象(也没有属性)。 ?Visual Studio中做到这一点的智能感知外部组件的方法......可以把它用反射来完成



例如,下面的代码产生这样的输出:




  dynamicField是动态
DynamicProperty是动态的
项目是动态的
DynamicMethod的是不是动态!
dynamicParameter是动态




示例代码:

 使用系统; 
使用的System.Reflection;使用System.Runtime.CompilerServices
;

命名空间ReflectDynamics
{
类节目
{
静态无效的主要()
{
VAR测试= typeof运算(测试);
CheckDynamic(test.GetField(dynamicField));
CheckDynamic(test.GetProperty(DynamicProperty));
CheckDynamic(test.GetProperty(项目));
MethodInfo的DynamicMethod的= test.GetMethod(DynamicMethod的);
CheckDynamic(DynamicMethod的);
CheckDynamic(dynamicMethod.GetParameters()[0]);
Console.ReadKey();
}

静态无效CheckDynamic(的MemberInfo的MemberInfo)
{
布尔isDynamic = memberInfo.GetCustomAttributes(typeof运算(DynamicAttribute),TRUE)。长度> 0;
Console.WriteLine(?memberInfo.Name +(isDynamic动态:是不是动态的!));
}
静态无效CheckDynamic(信息参数信息参数)
{
布尔isDynamic = parameterInfo.GetCustomAttributes(typeof运算(DynamicAttribute),TRUE)。长度> 0;
Console.WriteLine(?parameterInfo.Name +(isDynamic动态:是不是动态的!));
}
}

类测试
{
公共动态dynamicField;
公共动态DynamicProperty {{返回dynamicField; }}
公共动态此[INT指数] {{返回dynamicField; }}
公共DynamicMethod的动态(动态dynamicParameter){返回dynamicField; }
}
}


解决方案

这是因为你需要从方法检查上的 ReturnTypeCustomAttributes 属性。



修改你的 CheckDynamic 方法,像这样:

 静态无效CheckDynamic(的MemberInfo的MemberInfo)
{
布尔isDynamic = memberInfo.GetCustomAttributes(typeof运算(DynamicAttribute),TRUE)。长度> 0;

VAR的MethodInfo =(为的MemberInfo MethodInfo的);
如果(MethodInfo的!= NULL)
{
isDynamic = methodInfo.ReturnTypeCustomAttributes.GetCustomAttributes(typeof运算(DynamicAttribute),TRUE)。长度> 0;
}

Console.WriteLine(memberInfo.Name +(isDynamic动态:是不是动态!!));
}

这可能需要一些防御性编码,但它只是一个快速和肮脏的证明概念。


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.

这篇关于你如何判断的方法使用反射时,返回一个动态类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆