.net 开发 配置文件的灵活使用
配置文件的编写代码:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="spring" type="spring_test.springClass,spring_test"/>
<section name="hellowChild_1" type="spring_test.IFactory.HelloClassChild_1,spring_test"/>
<section name="hellowChild_2" type="spring_test.IFactory.HelloClassChild_2,spring_test"/>
</configSections>
<spring Name="spring_class:this is a test" ></spring>
<hellowChild_1 Name="HelloClassChild_1 : this is a HelloClassChild_1 class" NameSpace="spring_test.IFactory"></hellowChild_1>
<hellowChild_2 Name="HelloClassChild_2 : this is a HelloClassChild_2 class" NameSpace="spring_test.IFactory"></hellowChild_2>
</configuration>
类的编写代码:
public HelloClassChild_2()
{
}
public HelloClassChild_2(string Name, string NameSpace)
{
this.Name = Name;
this.NameSpace = NameSpace;
}
[System.Configuration.ConfigurationProperty("Name", IsRequired = true)]
public string Name
{
get
{
return this["Name"].ToString();
}
set
{
this["Name"] = value;
}
}
[System.Configuration.ConfigurationProperty("NameSpace", IsRequired = true)]
public string NameSpace
{
get
{
return this["NameSpace"].ToString();
}
set
{
this["NameSpace"] = value;
}
}
public string GetWriteLine(string Name,string NameSpace)
{
return string.Format("程序加载的类为:{0} \n所属的命名空间为:{1}", Name, NameSpace);
}
public void WriteLineString(string Name, string NameSpace)
{
Console.WriteLine(GetWriteLine(Name,NameSpace));
}
类的实例化代码:
static void Main(string[] args)
{
var dataClass1 = System.Configuration.ConfigurationManager.GetSection("spring");
springClass spring = dataClass1 as spring_test.springClass;
Console.WriteLine(spring.Name);
var dataClass2 = System.Configuration.ConfigurationManager.GetSection("hellowChild_1");
IFactory.IHello iFactory_1 = dataClass2 as IFactory.IHello;
iFactory_1.WriteLineString(iFactory_1.Name, iFactory_1.NameSpace);
var dataClass3 = System.Configuration.ConfigurationManager.GetSection("hellowChild_1");
IFactory.IHello iFactory_2 = dataClass2 as IFactory.IHello;
iFactory_2.WriteLineString(iFactory_2.Name, iFactory_2.NameSpace);
Console.Read();
}