Sto lavorando con il codice che istanzia dinamicamente l'oggetto SoapHttpClientProtocol
(classe proxy) e utilizza questo oggetto per effettuare una chiamata al servizio Web WS-Basic I. Ecco la versione semplificata del mio codice:Utilizzare l'espressione Lambda compilata anziché Activator.CreateInstance per inizializzare l'oggetto SoapHttpClientProtocol
public override object Call(Type callingObject,
string method, object[] methodParams, string URL)
{
MethodInfo requestMethod = callingObject.GetMethod(method);
//creates an instance of SoapHttpClientProtocol
object instance = Activator.CreateInstance(callingObject);
//sets the URL for the object that was just created
instance.GetType().InvokeMember("Url",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null,
instance,
new object[1] {URL});
return requestMethod.Invoke(instance, methodParams);
}
Ho notato che in alcuni casi Activator.CreateInstance()
chiamata può assumere notevole quantità di tempo, quindi sto cercando di ottimizzare il codice by using a lambda expression:
public override object Call(Type callingObject,
string method, object[] methodParams, string URL)
{
MethodInfo requestMethod = callingObject.GetMethod(method);
//creates an instance of SoapHttpClientProtocol using compiled Lambda Expression
ConstructorInfo constructorInfo = callingObject.GetConstructor(new Type[0]);
object instance = Expression.Lambda(Expression.New(constructorInfo)).Compile();
//sets the URL for the object that was just created
instance.GetType().InvokeMember("Url",
BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null,
instance,
new object[1] {URL});
//calls the web service
return requestMethod.Invoke(instance, methodParams);
}
Sfortunatamente, questo codice non crea un oggetto del tipo callingObject
(invece restituisce un oggetto delegato Func<T>
) e quindi quando tenta di impostare lo Url
nella riga successiva genera un'eccezione:
System.MissingMethodException: Tentativo di accedere a un membro mancante.
Mi manca qualcosa nel mio codice?
Grazie!
Link è morto a proposito – NStuke