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
? I know you can is if IsOut
and IsRef
etc.
Thanks.
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
? I know you can is if IsOut
and IsRef
etc.
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 :
bool isExtension=methodInfo.IsDefined(typeof(ExtensionAttribute),true);
Then you know the first parameter will be "this".
This seems to work; currently only works for no parameter and single parameter methods though / bit of a 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.