SoapHttpClientProtocol
개체 (프록시 클래스)를 동적으로 인스턴스화하고이 개체를 사용하여 WS-Basic I Web Service를 호출하는 코드로 작업하고 있습니다. 여기에 내 코드의 단순화 된 버전입니다 :Activator.CreateInstance 대신 컴파일 된 람다 식을 사용하여 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);
}
나는 경우에 Activator.CreateInstance()
통화 시간의 상당한 시간이 걸릴 수 있습니다 것으로 나타났습니다, 그래서 나는 코드 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);
}
을
불행하게도이 코드는 callingObject
유형의 객체를 만들지 않고 대신 Func<T>
대리 객체를 반환하므로 다음 줄에 Url
을 설정하려고 시도하면 예외가 throw됩니다.
System.MissingMethodException : 누락 된 멤버에 액세스하려고 시도했습니다.
코드에 뭔가가 누락 되었습니까?
감사합니다.
링크가 길어 졌음 – NStuke