jeudi 13 juin 2019

Reflection Issue Adding Item to List

Hi I have the following code but seem to be stuck at one point and am wondering what I might be missing. The code uses reflection to instantiate an object and configure it.

    Type intType = Type.GetType("IMyService");
    Type impType = Type.GetType("MyService");
    //Create instance of class
    var clsInstance = Activator.CreateInstance(impType);
    //Set properties have a method which does this and works fine
    SetProperty(clsInstance,"Endpoint.Name", "BasicHttpBinding_IMyService");
    SetProperty(clsInstance,"Endpoint.Address", new EndpointAddress("http://testurl"));
    //Set behaviour object ex: InspectorBehavior<T> : IEndpointBehavior
    Type behaviorType = typeof(InspectorBehavior<>).MakeGenericType(impType);
    //Create instance of behaviour passing in contructor data
    var behaveInstance = Activator.CreateInstance(behaviorType,new object[] { true});
    //Get endpoint property from the instance
    PropertyInfo endpoint = clsInstance.GetType().GetProperty("Endpoint");
    //Get value of behaviour
    var behaviourValue = endpoint.GetValue(clsInstance,null).GetType().GetProperty("EndpointBehaviors");
    //All WORKING to here and can see all objects created next line causes Exception: Object does match Target
    var m =behaviourValue.PropertyType.GetMethod("Add").Invoke(clsInstance,new object[] { behaveInstance });

The code is working apart from last line where I want to add my custom behaviour to the webservice.





Aucun commentaire:

Enregistrer un commentaire