Birisi bu davranışı Generics'te açıklayabilir mi? C# Genel işlevler
ben denetlemenin bir "Denetim olan TextBox" türü yapıyorum switch case kullanabilir, Yan not C#protected virtual void LoadFieldDataEditor <T> (ref T control, string strFieldName) where T : Control
{
//T can be different types of controls inheriting from System.Web.UI.Control
if (control is TextBox)
{
//This line gives an error
//((TextBox)control).Text = "test";
//This line works!
(control as TextBox).Text = "Test";
}
}
bir jenerik fonksiyonu var?
DÜZENLEME:
Maalesef hata mesajı eklemek unuttum! İşte
gitmek:
Error 3 Cannot convert type 'T' to 'TextBox'
DÜZENLEME:
biz jenerik söz ederken, bir sorum daha var. Oldukça zor olan yöntem başka genel tür
protected virtual void LoadFieldDataEditor <T1, T2> (T1 control, T2 objData, string strFieldName) where T1 : Control where T2 : BaseDataType
{
//I will need to access field1.
//I don't know at compile time if this would be SomeType1 or
//SomeType2 but all of them inherit from BaseDataType.
//Is this possible using generics?
}
public abstract class BaseDataType {}
public class SomeType1 : BaseDataType
{
string field1;
string field2;
}
Bir hatanın ne anlama geldiğini sorduğunuzda, hatanın ne olduğunu söylemek gerçekten yararlıdır. Sadece şimdi yeniden üretmeye çalışıyorum ... –
Hata nedir? –
@Jon Skeet: Lanet ... Durumda olduğunun farkında değildim ... –