I just need to know if there's a smart way to detect if a particular Type method has overloads or not.
At the moment I'm iterating through methods and searching for DeclaringType+Name ambiguity...but I think it's a bit lame :)
I just need to know if there's a smart way to detect if a particular Type method has overloads or not.
At the moment I'm iterating through methods and searching for DeclaringType+Name ambiguity...but I think it's a bit lame :)
The overload is what happens when you have two methods with the same name but different signatures.
So if we can the method is override by check method name whether more than one.
I would create an extension method IsOverloads to make it.
use the IsOverloads method need to provide at least two parameter
Type. Using reflection to gets method information from the class, then check the method name greater than one means the method is overload.
public static class Ext {
public static bool IsOverloads(this Type type,string methodName)
{
return IsOverloads(type, methodName, BindingFlags.Public | BindingFlags.Instance);
}
public static bool IsOverloads(this Type type,
string methodName,
BindingFlags flags)
{
var info = type.GetMethods(flags);
return info.Where(o1 => o1.Name == methodName).Count() > 1;
}
}
Here is a simple
public class Program
{
public void Test() { }
public void Test(int a) { }
public void TestNo(int a) { }
public static void Main()
{
Console.WriteLine(typeof(Program).IsOverloads("Test")); //True
Console.WriteLine(typeof(Program).IsOverloads("TestNo")); //false
}
}