1

我想知道是否可以确定方法中的参数是否是this object me基于的参数类型ParameterInfo?我知道你可以是 ifIsOut等等IsRef

谢谢。

4

3 回答 3

4

您不会在它们扩展的类中通过反射找到扩展方法。但是,如果您正在查看定义扩展方法的静态类,您可以查看方法信息本身来判断它是扩展方法。由于编译器在扩展方法上添加了 ExtensionMethod 属性:

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

然后你知道第一个参数将是“this”。

于 2012-11-10T00:44:56.390 回答
0

这似乎有效;目前仅适用于无参数和单参数方法,尽管/有点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++; }
    }
}
于 2012-11-10T00:46:01.643 回答
0
namespace System.Reflection
{
    public static class MethodInfoExtensions
    {
        public static bool IsExtensionMethod(this MethodInfo method)
        {
            return method.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false);
        }
    }
}

似乎无法找到一种方法来查看具体ParameterInfo是否是this object me一种参数,但这适用于整个方法,而且据我测试过,我似乎可以假设第一个参数是this object me范围。

于 2012-11-10T01:02:16.140 回答