& çağrılacak yöntemin belirtilemediği bir genel işlev oluşturmaya çalışıyorum; gibiC# veya vb Kod bloğunu tekrar deneme için jenerik işlev n sayısı
şey:
//3 stands for maximum number of times GetCustomerbyId should be called if it fails on first attempt.
var result = RetryCall(GetCustomerbyId(id),3);
İkincisi döndürme türü o çağırıyor fonksiyonu göre otomatik olarak ayarlanmalıdır. Örneğin, aşağıdaki işlevlerden ikisinden de sonuç alabilmem gerekir, biri & diğer Müşteri öğesi döndürür.
public static string GetCustomerFullNamebyId(int id){
return dataContext.Customers.Where(c => c.Id.Equals(id)).SingleOrDefault().FullName;
}
public static Customer GetCustomerbyId(int id){
return dataContext.Customers.Find(id);
}
Bu mümkün mü?
public T Retry<T>(Func<T> getter, int count)
{
for (int i = 0; i < (count - 1); i++)
{
try
{
return getter();
}
catch (Exception e)
{
// Log e
}
}
return getter();
}
const int retryCount = 3;
Customer customer = Retry(() => GetCustomerByID(id), retryCount);
string customerFullName = Retry(() => GetCustomerFullNamebyId(id), retryCount);
soru ilk n girişimleri sırasında istisna durumunda ne yapılacağını etmektir: Aşağıdaki yapabilirsiniz
'GetCustomerbyId (id)' çağrılırken bir hata nasıl görünür? Bir istisna? Bir "boş" dizesi? Bir 'null' nesnesi? – Enigmativity