概述 為什麼要用到深拷貝呢?比如我們建了某個類Person,並且實例化出一個對象,然後,突然需要把這個對象複製一遍,並且複製出來的對象要跟之前的一模一樣,來看下我們一般會怎麼做。 方法一(利用反射實現) public static T DeepCopy<T>(T obj) { //如果是字元串或值類 ...
經常能在.Net 項目中看到App.Config/Web.Config , 一直沒有瞭解過.Net 自帶的對配置文件的讀寫操作,常規的操作類在 System.Configuration.dll 中 ,比較重要的類為ConfigurationManager
底部有全部代碼+單元測試
AppSetting 及 ConnectionString 的讀取
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="MyGroup" type="AppConfig.MyGroup,Appconfig">
<section name="MySection" type="AppConfig.MySection,Appconfig" />
</sectionGroup>
<section name="MySection" type="AppConfig.MySection,Appconfig" />
</configSections>
<appSettings>
<add key="A" value="a"/>
<add key="B" value="b"/>
<add key="C" value="c"/>
</appSettings>
<connectionStrings>
<add connectionString="123" name="1"/>
<add connectionString="456" name="2"/>
</connectionStrings>
<MySection Code="asdas">
<Member Id="dasd"/>
<Members>
<add Id="1"/>
<add Id="2"/>
<add Id="3"/>
</Members>
</MySection>
<MyGroup>
<MySection Code="asdas1">
<Member Id="dasd1"/>
<Members>
<add Id="12"/>
<add Id="22"/>
</Members>
</MySection>
</MyGroup>
</configuration>
指定AppSetting 的 讀取
public string GetAppSettingValue(string appSettingName)
{
return ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(item => item.Equals(appSettingName)) != null ? ConfigurationManager.AppSettings[appSettingName] : null;
}
指定ConnecString的獲取
public string GetConnectString(string name)
{
return ConfigurationManager.ConnectionStrings[name]?.ConnectionString;
}
自定義內容的讀取
- ConfigurationSection
- ConfigurationElement
- ConfigurationSectionGroup
- ConfigurationElementCollection
ConfigurationSection
表示配置文件中的節。
// 自定義一個Section
internal class MySection : ConfigurationSection
{
[ConfigurationProperty(nameof(Code))]
public string Code
{
get => base[nameof(Code)].ToString();
}
}
需要在屬性上使用ConfigurationPropertyAttribute
//調用方法
public string GetMySectionCode()
{
return (ConfigurationManager.GetSection("MySection") as MySection)?.Code;
}
ConfigurationElement
表示Section的成員
//根據Config 配置屬性
public class MyElement : ConfigurationElement
{
[ConfigurationProperty(nameof(Id))]
public string Id
{
get => this[nameof(Id)].ToString();
}
}
//同時在MySection 中進行擴展
internal class MySection : ConfigurationSection
{
...
[ConfigurationProperty(nameof(Member))]
public MyElement Member
{
get => base[nameof(Member)] as MyElement;
}
...
}
//調用方法
public string GetMySectionMember()
{
return (ConfigurationManager.GetSection("MySection") as MySection)?.Member?.Id;
}
ConfigurationElementCollection
表示Element的集合
//定義一個MyElement 的集合
[ConfigurationCollection(typeof(MyElement))]
internal class MyElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new MyElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as MyElement).Id;
}
}
//在Section 中進行填充
internal class MySection : ConfigurationSection
{
[ConfigurationProperty(nameof(Members)), ConfigurationCollection(typeof(MyElement))]
public MyElementCollection Members
{
get => base[nameof(Members)] as MyElementCollection;
}
}
//調用
public int GetMySectionMembers()
{
return (int)(ConfigurationManager.GetSection("MySection") as MySection)?.Members?.Count;
}
ConfigurationSectionGroup
在Section 外面在套一個類
public class MyGroup : ConfigurationSectionGroup
{
[ConfigurationProperty(nameof(MySection))]
public MySection MySection { get => this.Sections[nameof(MySection)] as MySection; }
}
// 兩種調用方式都可以
public int GetMySectionMembersInGroup()
{
return (int)(ConfigurationManager.GetSection("MyGroup/MySection") as MySection)?.Members?.Count;
}
public int GetMySectionMembersInGroupByOpenConfig()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var group = (MyGroup)config.GetSectionGroup("MyGroup");
return group.MySection.Members.Count;
}
處理不是預設名字的Config 文件
ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = @".\App1.config" };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap,ConfigurationUserLevel.None);
這邊載入方式與上面的不同 後續一致