C# Invoke generic method with casted property as parameter

Nervis Wreck

I have a generic class with a generic method:

public class GenericClass<T> where T : class
{
   public void GenericMethod<T>(T item)
   {
      // do work here
   }
}

I also have an object with multiple properties, some of which can be other objects:

public class TestObject
{
   public ChildObject ChildObject { get; set; }
}

I am then attempting to use a generic method which will reflect through TestObject and call GenericMethod for all properties that are in and of themselves custom classes such as ChildObject (I do have a way of determining this based on inheritance, however for the sake of keeping it simple did not include that code for this example):

public void ReflectingMethod<T>(T item)
{
   var properties = item.GetType().GetProperties();

   foreach (var property in properties)
   {
      var type = property.PropertyType;

      dynamic propertyModel = property.GetValue(model, null);

      var castedObject = Convert.ChangeType(propertyModel, type);
      var genericClass = Activator.CreateInstance(typeof(GenericClass<>).MakeGenericType(type));
      var method = genericClass.GetType().GetMethod("GenericMethod", new [] { type });

      method.Invoke(castedObject, null);
   }
}

The problem is that whether I attempt to change property's type (as shown in the above example) or I pass property directly to method.Invoke, such as:

method.Invoke(propertyModel, null);

I still receive the same error:

Object does not match target type.

At RunTime method is:

GenericMethod(TestProject.ChildObject)

And castedObject is:

TestProject.ChildObject

I am confused as to why I am getting the error I am, when it would appear that the casted object is exactly of the type that the method is looking for.

EDIT 1 Included the call to GetValue that I had originally left out when posting the question.

Ivan Stoev

The call

  method.Invoke(castedObject, null);

is incorrect. It should be

  method.Invoke(genericClass, new object[] { castedObject });

Since you are trying to call an instance method, the first argument to Invoke(object, object[]) must be the this instance. For static method, pass null as first argument. Method arguments are always passed via the second object[] argument of the Invoke method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reflection: invoke a method with generic parameter

Generic method returning property based on generic parameter

How to use C# reflection to invoke an extension method with generic List parameter?

How to invoke generic method with Action<T> parameter using reflection in C#

C# Create a Generic method contains generic collection and invoke Sum()

Invoke method on generic type?

Invoke List of generic objects as parameter

Implementing a multi parameter generic method in C#

C# Passing a Generic Class As a Method Parameter

Set property to generic parameter

typeof generic and casted type

Cannot invoke c++ method from qml: "Property of object is not a function"

C# reflection, invoke method with different parameter types

How to pass Struct parameter when a Dynamic Method Invoke in C#?

Invoke javascript method from c# with dynamic parameter

Generic method with HttpMethod as a parameter

Null OR generic as parameter method

Generic enum as method parameter

Generic method with no parameter

Passing an generic parameter to the method

Generic method with multiple parameter

Type of a generic method parameter

Generic type as method parameter

Require that a generic parameter contains a property with a specific string as a name C#?

Generics C# How to pass a property of a generic object by parameter

Invoke delegate method name with parameter

Invoke parameter from another method

Overload method with generic method and parameter

Invoke a method returned from generic method with generic Action return type