मैं डिफ़ॉल्ट कन्स्ट्रक्टर का उपयोग कर ऑब्जेक्ट बनाने को प्रतिबंधित करना चाहता हूं। क्योंकि मेरे पास नीचे की तरह एक desing है:डिफ़ॉल्ट कन्स्ट्रक्टर के बिना डिज़ाइन
class Program
{
static void Main(string[] args)
{
BaseClass bc = new BaseClass("","");
XmlSerializer xml = new XmlSerializer(typeof(BaseClass));
StreamWriter sw = new StreamWriter(File.Create("c:\\test.txt"));
xml.Serialize(sw,bc);
sw.Flush();
sw.Close();
}
}
[Serializable]
public class BaseClass
{
public string UserName, Password;
// I don't want to create default constructor because of Authentication
public BaseClass(string _UserName, string _Password)
{
UserName = _UserName;
Password = _Password;
f_Authenticate();
}
private void f_Authenticate() { }
}
public class DerivedClass:BaseClass
{
public DerivedClass(string _UserName, string _Password) : base(_UserName, _Password)
{
}
}
यह ठीक है। लेकिन serializable जब मैं BaseClass यह इस त्रुटि उत्पन्न करेंगे: Unhandled Exception: System.InvalidOperationException: ConsoleApplication1.BaseC lass cannot be serialized because it does not have a parameterless constructor.
अब मेरी डिजाइन क्योंकि मैं Username
, Password
मानकों की आवश्यकता है लेकिन डिफ़ॉल्ट निर्माता मेरे डिजाइन को बर्बाद कर रहा है टूट जाता है ....
क्या क्या मुझे करना चाहिए?
दरअसल मैं हर संभावनाओं को सीखना चाहता हूं। अगर मैं इसका उपयोग नहीं कर सकता/सकती हूं तो आप मुझे क्या पेशकश करेंगे? – uzay95
http://stackoverflow.com/questions/267724/why-xml-serializable-class-need-a-parameterless-constructor – Matten