Я работаю с кодом, который динамически создает экземпляр SoapHttpClientProtocol
объекта (прокси-класса )и использует этот объект для вызова WS -Basic I Web Service. Вот упрощенная версия моего кода:
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);
}
Я заметил, что в некоторых случаях вызов Activator.CreateInstance()
может занимать значительное время, поэтому я пытаюсь оптимизировать код с помощью лямбда-выражения:
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);
}
К сожалению, этот код не создает объект типа callingObject
(, вместо этого он возвращает объект делегата Func
), и поэтому, когда он пытается установить Url
в следующей строке, он выдает исключение:
System.MissingMethodException: Attempted to access a missing member.
Я что-то упустил в своем коде?
Спасибо!