başlatmak için Activator.CreateInstance yerine Derlenmiş Lambda ifadesini kullan Dinamik olarak SoapHttpClientProtocol
nesnesini (proxy sınıfı) başlatılan kodla çalışıyorum ve WS-Basic I Web Hizmeti'ne bir çağrı yapmak için bu nesneyi kullanıyorum. İşte benim kod basitleştirilmiş bir versiyonudur:SoapHttpClientProtocol nesnesini
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);
}
Bazı durumlarda Activator.CreateInstance()
çağrı zaman önemli miktarda alabilir fark ettik, bu yüzden kod by using a lambda expression optimize etmek çalışıyorum:
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);
}
:
Maalesef bu kodu (bunun yerine bir Func<T>
temsilci nesnesi döndürür) ve bu nedenle sonraki satırda Url
ayarlamaya çalıştığında bir istisna atar callingObject
türde bir nesne yaratmaz
System.MissingMethodException: Eksik bir üyesini erişmeye çalışıldı.
Am benim kodunda şey eksik?
Teşekkürler!
Bağlantı yolu – NStuke