As pointed out, you can't do this and there are good reasons why you shouldn't.
The method I've implemented in the past creates a interface and an abstract base class that implements the interface. It looks something like this:
public interface IMyCompanySetting
{
XmlNode Serialize();
IMyCompanySetting Deserialize(XmlNode pattern);
string SettingName { get; }
string Key { get; }
object SettingValue { get; set; }
SettingScope Scope { get; set; }
}
public abstract class MyCompanySettingBase : IMyCompanySetting
{
public MyCompanySettingBase() {}
public MyCompanySettingBase(XmlNode pattern)
{
Deserialize(pattern);
}
#region IMyCompanySetting Members
public abstract XmlNode Serialize();
public abstract IMyCompanySetting Deserialize(XmlNode pattern);
public abstract string SettingName{ get; }
public abstract string Key { get; }
public abstract SettingScope Scope{ get; set; }
public abstract object SettingValue{ get; set; }
#endregion
public static XmlNode WrapInSettingEnvelope(XmlNode innerNode, IMyCompanySetting theSetting)
{
// Write the top of the envelope.
XmlTextWriter xtw = null;
MemoryStream theStream = OpenSettingEnvelope(theSetting, ref xtw);
// Insert the message.
xtw.WriteNode(new XmlTextReader(innerNode.OuterXml, XmlNodeType.Element, null), true);
// Close the envelope.
XmlNode retNode = CloseSettingEnvelope(xtw, theStream);
return retNode;
}
public static MemoryStream OpenSettingEnvelope(IMyCompanySetting theSetting, ref XmlTextWriter theWriter)
{
MemoryStream theStream = new MemoryStream();
theWriter = new XmlTextWriter(theStream, Encoding.ASCII);
System.Type messageType = theSetting.GetType();
string[] fullAssembly = messageType.Assembly.ToString().Split(',');
string assemblyName = fullAssembly[0].Trim();
theWriter.WriteStartElement(theSetting.SettingName);
theWriter.WriteAttributeString("type", messageType.ToString());
theWriter.WriteAttributeString("assembly", assemblyName);
theWriter.WriteAttributeString("scope", ConfigurationManager.ScopeName(theSetting.Scope));
return theStream;
}
public static XmlNode CloseSettingEnvelope(XmlTextWriter xtw, MemoryStream theStream)
{
XmlDocument retDoc = new XmlDocument();
try
{
// Close the envelope.
xtw.WriteEndElement();
xtw.Flush();
// Return the node.
string xmlString = Encoding.ASCII.GetString(theStream.ToArray());
retDoc.LoadXml(xmlString);
}
catch (XmlException)
{
string xmlString = Encoding.ASCII.GetString(theStream.ToArray());
Trace.WriteLine(xmlString);
retDoc.LoadXml(@"<error/>");
}
catch (Exception)
{
retDoc.LoadXml(@"<error/>");
}
return retDoc.DocumentElement;
}
}