I want to restrict the creation of an object using the default constructor. Because I have a view as shown below:
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;
This is normal. But when I make BaseClass for Serializable, it will throw this error: Unhandled Exception: System.InvalidOperationException: ConsoleApplication1.BaseC lass cannot be serialized because it does not have a parameterless constructor.
Now my project is crashing because I need to have Username , Password parameters, but the default constructor destroys my construction ....
What should I do?
uzay95
source share