mercredi 31 août 2016

Reflection how to pass values to parameters based on getparameters and cast return object

i have class library with class

    namespace ReflectionCALlingproject
{
    public class Class1
    {
        public House GetResultsByReflection(int HouseNumb,int pincode)
        {
            House house = new House ();
            House.currentDay=DateTime.Now;
            return house;
        }

    }
}

i have to call this class dynamically at runtime and need to get the currentDay.

 public string test()
        {
     int HouseNumb= 31231123;
     int pincode= 124124124;
    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\ReflectionCALlingproject.dll");         
               Assembly assembly = Assembly.LoadFile(path);
               Type type = assembly.GetType("ReflectionCALlingproject.Class1");
                if (type != null)
                {
                    MethodInfo methodInfo = type.GetMethod("GetResultsByReflection");
                    if (methodInfo != null)
                    {
                    ParameterInfo[] parameters = methodInfo.GetParameters();
                   object classInstance = Activator.CreateInstance(type, null);

        object[] parametersArray = new object[] { HouseNumb, pincode};   //need to pass this values based on the parameterinfo.       
                        result = methodInfo.Invoke(classInstance, parametersArray );


                    }
                }
                return result.ToString();
//result is House object how can i get the property from it.
}

i have to pass the parameters based on the parameterinfo.

if am changing the calling method parameters it should work properly.

how can i pass values based on the parameterinfo.

how can i cast the result to the specified house class?





Aucun commentaire:

Enregistrer un commentaire