.net 开发 配置文件的灵活使用
2013-06-16 14:19
190 查看
配置文件的编写代码:
<?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();
}
<?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();
}
相关文章推荐
- .net 开发 配置文件的灵活使用
- .Net Core MVC 网站开发(Ninesky) 2.3、项目架构调整(续)-使用配置文件动态注入
- [dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置
- eclipse + JBoss 5 + EJB3开发指南(5):使用配置文件发布Session Be
- 使用VI编辑器开发PHP程序——配置文件
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- 使用C# .net开发微信公众号之服务器URL配置
- 无配置文件下使用log4net
- Android下使用TinyXml读取xml配置文件(Cocos2d-x游戏开发)
- 使用VS Code开发Angular 2应用程序所需配置文件的解析
- ASP.NET网站开发中的配置文件
- asp.net mvc开发中,web.config文件如何配置数据库链接
- ASP.NET Core 开发-Logging 使用NLog 写日志文件
- RDIFramework.NET — 基于.NET的快速信息化系统开发框架- 5.4平台日志、异常管理、生成自动升级配置文件模块
- JavaHost:使用虚拟DNS省掉开发环境配置hosts文件
- android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用
- 彻底摆脱配置文件 八(使用maven-zkcu-plugin灵活控制zkconfigutil)
- 使用Web.Config Transformation配置灵活的配置文件
- 使用VS Code开发.Net Core 2.0 MVC Web应用程序教程之三(配置文件读取)
- 使用.NET Framework的配置文件app.config