我有以下课程:
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 :(得分:0)
好的,我通过使用GetValue而不是使用GetProperties来获取客户端属性来解决它:
PropertyInfo p = client.GetType().GetProperty("Client");
MethodInfo m = p.PropertyType.GetMethod(Request.GetFriendlyUrlSegments()[1]);
object clientProp = p.GetValue(client);