.NET Framework Developer Center > .NET Framework Forums > .NET Base Class Library > Object does not match target type in PropertyInfo

Answered Object does not match target type in PropertyInfo

  • Monday, March 22, 2010 2:41 AM
     
     
    I created some simple codes (see below) trying to get the callback instance from a WCF proxy.  From debugger, I can see I get the pi object with valuable information.  I thought I can get the  IDuplexContextChannel object with these codes below.  But it throws me the exception - "Object does not match target type" in the line of pi.GetValue(proxy, null).  Is "proxy" not the right parameter passed in?  I though it is because pi is derived from proxy.  If no, what should I need to pass in?


    Thanks in advance for help!


    Here are the codes --

     

                    Type proxy = GetProxyType(contractType);

                    PropertyInfo pi = proxy.GetProperty("InnerDuplexChannel");

     

                    if (pi.GetIndexParameters().Length == 0) {

                        IDuplexContextChannel dcc = (IDuplexContextChannel)pi.GetValue(proxy, null);

                        InstanceContext ic = new InstanceContext(dcc.CallbackInstance);

                    }

Answers

  • Monday, March 22, 2010 7:30 AM
     
     Answered
    I think that you should pass an instance of proxy, not its type to GetValue.
    Miha Markic [MVP C#] http://blog.rthand.com
    • Marked As Answer by DongL Tuesday, March 23, 2010 2:36 AM
    •  

All Replies

  • Monday, March 22, 2010 7:30 AM
     
     Answered
    I think that you should pass an instance of proxy, not its type to GetValue.
    Miha Markic [MVP C#] http://blog.rthand.com
    • Marked As Answer by DongL Tuesday, March 23, 2010 2:36 AM
    •  
  • Monday, March 22, 2010 9:19 PM
     
     

    Thanks Miha!

    I have modified the line of code - " IDuplexContextChannel dcc = (IDuplexContextChannel)pi.GetValue(proxy, null);"

     to " dcc = (IDuplexContextChannel)pi.GetValue(Activator.CreateInstance(proxy), null);"

    Now I have differnt error "[System.MissingMethodException] = {"No parameterless constructor defined for this object."}".  Its stack report is -

       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
       at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)

    It is true I don't have a no parameterless constructor.  All the constructors I have are -

    "[System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel.InstanceContext)}
    base {System.Reflection.MemberInfo} = {Void .ctor(System.ServiceModel.InstanceContext)}

    [System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel.InstanceContext, System.String)}
    base {System.Reflection.MethodBase} = {Void .ctor(System.ServiceModel.InstanceContext, System.String)}

    [System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel.InstanceContext, System.String, System.String)}
    base {System.Reflection.MethodBase} = {Void .ctor(System.ServiceModel.InstanceContext, System.String, System.String)}


    [System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel.InstanceContext, System.String, System.ServiceModel.EndpointAddress)}
    base {System.Reflection.MethodBase} = {Void .ctor(System.ServiceModel.InstanceContext, System.String, System.ServiceModel.EndpointAddress)}

    [System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel.InstanceContext, System.ServiceModel.Channels.Binding, System.ServiceModel.EndpointAddress)}
    base {System.Reflection.MethodBase} = {Void .ctor(System.ServiceModel.InstanceContext, System.ServiceModel.Channels.Binding, System.ServiceModel.EndpointAddress)}

    From the stack's tope message "at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)", I gue ss that I may be able to replace that line of code by setting the boolean value to true. 

    dcc = (IDuplexContextChannel)pi.GetValue(Activator.CreateInstance(proxy, true, true, true, RuntimeMethodHandle, true), null);

    But I don't know how to set the RuntimeMethodHandle from the Type object proxy.  Is it possible to get the RuntimeMethodHandle from there?

    Thanks all!

     


     

     

  • Tuesday, March 23, 2010 2:36 AM
     
     
    Thanks guys.  I guess I got it.  Passing an empty RuntimeMethodHandle may be fine.