2014-07-03 39 views
0

通用类属性调用WCF方法我有下面的类:与反射

Public Class WcfClient(Of T) 

     Private _Cliente As T 

     Public ReadOnly Property Client As T 
      Get 
       Return _Cliente 
      End Get 
     End Property 

T,表示WCF频道,也就是像这样创建的:

Dim channel As New ChannelFactory(Of T)(basicHttpBinding, remoteAddress) 
'Set the property 
_Cliente = channel.CreateChannel() 

现在,随着反射,我正在创建一个WcfClient的实例,并且我想执行一个位于客户端属性中的方法。

Type tipo = FindInterface(Request.GetFriendlyUrlSegments()[0]); 
Type genType = typeof (WcfClient<>).MakeGenericType(tipo); 
var client = Activator.CreateInstance(genType);  
var clientProp = client.GetType(). 
       GetProperties().Where(p => p.Name == "Client").FirstOrDefault(); 

if (clientProp != null) 
{ 

    var method =clientProp.PropertyType 
        .GetMethod(Request.GetFriendlyUrlSegments()[1]); 
    ProcesoBase procesoBase = new ProcesoBase(); 

    foreach (var prop in typeof (ProcesoBase).GetProperties()) 
    { 
      //Here we have some code to fill ProcesoBase properties 

     } 

    } 

    var result = method.Invoke(clientProp, new object[] { procesoBase }); 

当调用method.Invoke我得到Object does not match target type异常

我从clientProp变量获取MethodInfo的类,所以我不明白这是怎么发生的。

难道这是因为生成的Channel类构建为proxy_TransparentProxy类吗?

回答

0

好吧,我解决它通过让使用的GetValue客户属性而不是使用的GetProperties:

PropertyInfo p = client.GetType().GetProperty("Client"); 
    MethodInfo m = p.PropertyType.GetMethod(Request.GetFriendlyUrlSegments()[1]); 
    object clientProp = p.GetValue(client);