简体   繁体   English

ParameterInfo是一个扩展方法参数?

[英]ParameterInfo is a extension method parameter?

I was wondering if it was possible to determine if a parameter in a method was a this object me type of parameter based off of ParameterInfo ? 我在想,如果有可能,以确定是否在方法的参数是this object me基于关闭的参数类型ParameterInfo I know you can is if IsOut and IsRef etc. 我知道你可以是IsOutIsRef等。

Thanks. 谢谢。

You won't find extension methods by reflection in the classes they extend. 在扩展的类中,您不会通过反射找到扩展方法。 However, if you're looking at the static class in which the extension method is defined, you can look at the method info itself to tell that is an extension method. 但是,如果您正在查看定义了扩展方法的静态类,您可以查看方法信息本身以告知这是一个扩展方法。 Since the compiler adds the ExtensionMethod attribute on extension methods : 由于编译器在扩展方法上添加了ExtensionMethod属性:

bool isExtension=methodInfo.IsDefined(typeof(ExtensionAttribute),true);

Then you know the first parameter will be "this". 然后你就知道第一个参数是“this”。

This seems to work; 这似乎有效; currently only works for no parameter and single parameter methods though / bit of a hack: 目前只适用于没有参数和单参数方法但/ hack:

using System;
using System.Reflection;
using System.Collections.Generic;

namespace StackOverflow.Demos
{
    class Program
    {
        public static void Main(string[] args) 
        {
            object o = new object();
            Console.WriteLine(ExtensionDetectorT<string,object>.IsExtensionMethod(o.ToString));
            Console.WriteLine(ExtensionDetectorT<string, object>.IsExtensionMethod(o.ToString2));
            Console.WriteLine(ExtensionDetectorT<bool, object>.IsExtensionMethod(o.Equals));
            Console.WriteLine(ExtensionDetectorT<bool, object>.IsExtensionMethod(o.Equals2));
            Console.WriteLine(ExtensionDetectorVoid<object>.IsExtensionMethod(o.DoNothing));
            Console.WriteLine(ExtensionDetectorVoid<int>.IsExtensionMethod(o.DoNothing2));
            Console.WriteLine("Done");
            Console.ReadKey();
        }
    }

    public static class ExtensionDetectorT<TReturn,TParam1>
    {
        public delegate TReturn ExtensionMethodDelegateT();
        public delegate TReturn ExtensionMethodDelegateT2(TParam1 ignoreMe);

        public static bool IsExtensionMethod(ExtensionMethodDelegateT emd)
        {
            return !(new List<MethodInfo>(emd.Target.GetType().GetMethods())).Exists(delegate(MethodInfo mi) { return mi.Name.Equals(emd.Method.Name); });
        }
        public static bool IsExtensionMethod(ExtensionMethodDelegateT2 emd)
        {
            return !(new List<MethodInfo>(emd.Target.GetType().GetMethods())).Exists(delegate(MethodInfo mi) { return mi.Name.Equals(emd.Method.Name); });
        }
    }
    public static class ExtensionDetectorVoid<TParam1>
    {
        public delegate void ExtensionMethodDelegateVoid();
        public delegate void ExtensionMethodDelegateVoid2(TParam1 ignoreMe);
        public static bool IsExtensionMethod(ExtensionMethodDelegateVoid emd)
        {
            return !(new List<MethodInfo>(emd.Target.GetType().GetMethods())).Exists(delegate(MethodInfo mi) { return mi.Name.Equals(emd.Method.Name); });
        }
        public static bool IsExtensionMethod(ExtensionMethodDelegateVoid2 emd)
        {
            return !(new List<MethodInfo>(emd.Target.GetType().GetMethods())).Exists(delegate(MethodInfo mi) { return mi.Name.Equals(emd.Method.Name); });
        }
    }

    public static class ObjectExtensions
    {
        public static string ToString2(this object o) { return o.ToString(); }
        public static bool Equals2(this object o, object o2) { return o.Equals(o2); }
        public static void DoNothing(this object o) { }
        public static void DoNothing2(this object o, int i) { i++; }
    }
}
namespace System.Reflection
{
    public static class MethodInfoExtensions
    {
        public static bool IsExtensionMethod(this MethodInfo method)
        {
            return method.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false);
        }
    }
}

Can't seem to figure out a way to look into the specific ParameterInfo to see if it's a this object me kind of parameter, but this works for the whole method, and as far as I've tested it would seem I can assume the first parameter is a this object me parameter. 似乎无法找到一种方法来查看特定的ParameterInfo以查看它是否是一个this object me的参数,但这适用于整个方法,并且据我测试它似乎我可以假设第一个参数是this object me参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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