Advertisements
Home > Misc, Uncategorized > Reflection with method overloading C#

Reflection with method overloading C#

I have a class with 3 overloaded methods and when try to invoke method’s using Reflection, I was gettingAvoiding an ambiguous match exception” exception.

Below is the way to invoke overload methods using Reflection, which solved my exception.

Let’s take a class with 3 overload methods.

Class Structure

namespace MyNamespace{

public class CallMe{

public string MyName(){

return “You don’t have name”;

}

public string MyName(string firstName){

return “Your name is ” + firstName;

}

public string MyName(string firstName, string lastName){

return “Your name is ” + firstName + lastName;

}

}

Here is the way to invoke methods using Reflection

C# Code to call the Generic Method

// Load .dll

Assembly assembly = Assembly.LoadFile(“{Physical path of }MyNamespace.dll”);

// Set the Class type as “Namespace.Classname”

Type classType = assembly.GetType(“ReflectionClass.CallMe”);

// One of my methods expects 1 string hence creating MethodInfo object with 1 Type[] parameter

MethodInfo methodWithOneParameter = classType.GetMethod(“MyName”, new Type[] { typeof(string) });

// One of my methods expect 2 string parameters hence creating MethodInfo object with 2 Type[] parameters.

MethodInfo methodWithTwoParameter = classType.GetMethod(“MyName”, new Type[] { typeof(string), typeof(string) });

// To invoke overload with no parameters, provide an empty Type array to GetMethod’s second parameter

MethodInfo methodWithNoParameter = classType.GetMethod(“MyName”, new Type[0]);

// Invoke Methods

var resultMethodWithOneParameter = InvokeMethod(classType, methodWithOneParameter, new string[] { “Rajeev” });

var resultMethodWithTwoParameter = InvokeMethod(classType, methodWithTwoParameter, new string[] { “Rajeev”, “Pentyala” });

var resultMethodWithNoParameter = InvokeMethod(classType, methodWithNoParameter, null);

//  Display Results

Console.WriteLine(“ResultMethodWithOneParameter – ” + resultMethodWithOneParameter.ToString());

Console.WriteLine(“ResultMethodWithTwoParameter – ” + resultMethodWithTwoParameter.ToString());

Console.WriteLine(“ResultMethodWithNoParameter – ” + resultMethodWithNoParameter.ToString());

 C# Generic Method to Invoke methods

       // Generic method Invokes methods and return Result as Object

public static object InvokeMethod(Type classType, MethodInfo methodInfo, object[] parametersArray){

object result = null;

if (classType != null) {

if (methodInfo != null) {

ParameterInfo[] parameters = methodInfo.GetParameters();

object classInstance = Activator.CreateInstance(classType, null);

if (parameters.Length == 0) {

//This works fine

result = methodInfo.Invoke(classInstance, null);

}

else {

//The invoke does NOT work it throws “Object does not match target type”

result = methodInfo.Invoke(classInstance, parametersArray);

}

}

}

return result;

}

We get the response as below

Reflection Overload Result

Reflection Overload Result

🙂

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: