Code-saving Configuration File Maintenance (propertygrid + expandableobjectconverter)

Source: Internet
Author: User

Namespace webservicestudio
{
Using system;
Using system. componentmodel;
Using system. IO;
Using system. reflection;
Using system. xml. serialization;

[Typeconverter (typeof (expandableobjectconverter)] // provides a type converter that converts extensible objects to other forms of representation.

Public class configuration
{
Private customhandler [] dataeditors;
Private invokeproperties invokesettings = new invokeproperties ();
Private Static configuration masterconfig = NULL;
Private customhandler [] proxyproperties;
Private customhandler [] typeconverters;
Private uiproperties uisettings;
Private wsdlproperties wsdlsettings;

Public configuration copy ()
{
Memorystream stream = new memorystream ();
Xmlserializer serializer = new xmlserializer (typeof (configuration ));
Serializer. serialize (Stream) stream, masterconfig );
Stream. Position = 0l;
Return (serializer. deserialize (Stream) as configuration );
}

Private Static string getconfigfilename ()
{
Return (assembly. getexecutingassembly (). Location + ". Options ");
}

Internal string [] getproxybasetypes ()
{
Customhandler [] proxyproperties = This. proxyproperties;
String [] strarray = new string [(proxyproperties! = NULL )? (Proxyproperties. Length + 1): 1];
Strarray [0] = "";
For (INT I = 1; I <strarray. length; I ++)
{
Strarray [I] = proxyproperties [I-1]. typename;
}
Return strarray;
}

Public static void loadmasterconfig ()
{
Try
{
Filestream stream = file. openread (getconfigfilename ());
Streamreader textreader = new streamreader (Stream );
Xmlserializer serializer = new xmlserializer (typeof (configuration ));
Masterconfig = serializer. deserialize (textreader) as configuration; // reverse instantiation, C # The best Function
Stream. Flush ();
Stream. Close ();
}
Catch
{
}
If (masterconfig = NULL)
{
Masterconfig = new configuration ();
}
If (masterconfig. dataeditors = NULL)
{
Masterconfig. dataeditors = new customhandler [0];
}
If (masterconfig. typeconverters = NULL)
{
Masterconfig. typeconverters = new customhandler [0];
}
If (masterconfig. wsdlsettings = NULL)
{
Masterconfig. wsdlsettings = new wsdlproperties ();
}
If (masterconfig. uisettings = NULL)
{
Masterconfig. uisettings = new uiproperties ();
}
}

Public static void savemasterconfig ()
{
Filestream stream = file. openwrite (getconfigfilename ());
Streamwriter writer = new streamwriter (Stream );
New xmlserializer (typeof (configuration). serialize (textwriter) writer, masterconfig); // save it to a file
Stream. setlength (stream. position );
Stream. Flush (); // refresh the cache to truly write data to the disk
Stream. Close ();
}

[Browsable (false)]
Public customhandler [] dataeditors
{
Get
{
Return this. dataeditors;
}
Set
{
This. dataeditors = value;
}
}

[Browsable (false)]
Public invokeproperties invokesettings
{
Get
{
Return this. invokesettings;
}
Set
{
This. invokesettings = value;
}
}

Internal static configuration masterconfig
{
Get
{
If (masterconfig = NULL)
{
Loadmasterconfig ();
}
Return masterconfig;
}
Set
{
Masterconfig = value;
Savemasterconfig ();
}
}

[Browsable (false)]
Public customhandler [] proxyproperties
{
Get
{
Return this. proxyproperties;
}
Set
{
This. proxyproperties = value;
}
}

[Browsable (false)]
Public customhandler [] typeconverters
{
Get
{
Return this. typeconverters;
}
Set
{
This. typeconverters = value;
}
}

Public uiproperties uisettings
{
Get
{
Return this. uisettings;
}
Set
{
This. uisettings = value;
}
}

Public wsdlproperties wsdlsettings
{
Get
{
Return this. wsdlsettings;
}
Set
{
This. wsdlsettings = value;
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.