क्या कोई जेनेरिक में इस व्यवहार को समझा सकता है?सी # जेनरिक फ़ंक्शन
मैं एक पक्ष टिप्पणी पर सी #
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";
}
}
में एक सामान्य समारोह है, मैं स्विच मामला है जब मैं एक "नियंत्रण पाठ बॉक्स है" जाँच के प्रकार के कर रहा हूँ का उपयोग कर सकते हैं?
संपादित करें:
त्रुटि संदेश जोड़ने के लिए क्षमा करें क्षमा करें!
ये रहा:
Error 3 Cannot convert type 'T' to 'TextBox'
संपादित करें:
हम जेनरिक के बारे में बात कर रहे हैं, मैं एक और सवाल है। ,
विधि एक और सामान्य प्रकार
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;
}
जब आप किसी त्रुटि का अर्थ पूछ रहे हैं, तो यह वास्तव में मददगार है कि त्रुटि क्या है। बस पुन: पेश करने की कोशिश कर रहा है ... –
त्रुटि क्या है? –
@ जोन स्कीट: अरे ... मुझे नहीं पता था कि आप इस मामले में थे ... –