使用反射时如何确定方法是否返回动态类型?

发布于 2024-12-20 08:07:16 字数 1832 浏览 3 评论 0原文

使用反射时,可以检查字段、属性、索引器和参数的 DynamicAttribute 属性,以确定它们是否具有动态类型。但是,这不适用于方法 - 即使它们返回“动态”,但它们没有属性,并且返回类型是“对象”(并且也没有属性)。 Visual Studio 为外部程序集中的方法执行此操作...可以通过反射来完成吗?

例如,下面的代码会产生以下输出:

dynamicField 是动态的
DynamicProperty 是动态的
项目是动态的
DynamicMethod 不是动态的!!
动态参数是动态的

示例代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2024-12-27 08:07:16

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

像这样修改您的 CheckDynamic 方法:

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文