根据传入的路径来解析XML文件赋值给动态常量,能很好的扩展应用程序的常量值
三、枚举
枚举是用户定义的整数类型,在声明一个枚举时,要指定该枚举的实例可以包含的一组可以接受的值,枚举具有如下的优势:
1.枚举可以使代码更易于维护,有助于确定给变量指定合法的,期望的值
2.枚举使代码更清晰,允许用描述性的名称来表示整数,而不是含义模糊、变化多端的数
3.枚举也是代码更易于输入
在实际应用中通常在枚举上面加上Description需要显示的枚举特性值,在页面显示的时候通常显示的也是枚举的特性值,所以有必要写一个获取枚举特性值的通用方法
class Program
{
static void Main(string[] args)
{
//ReadOnlyModel readOnlyCompany = new ReadOnlyModel(@"D:GitHubProjectC#AdvancedProgrammingVariableDefineVariableDefinebinDebugTest-Parking-SN.xml"); //C:Program Files(x86)FPOnline
//foreach (var company in readOnlyCompany.ListCompany)
//{
// Console.WriteLine("company name is {0} company email is {1}", company.CompanyName, company.CompanyEmail);
//}
//Console.WriteLine("company name is {0} company email is {1}", ConstClass.CompanyName, ConstClass.CompanyEmail);
MemberLevel superMember = MemberLevel.SuperMember;
Console.WriteLine(superMember.GetDescriptionEnum());
Console.ReadKey();
}
}
public enum MemberLevel
{
[Description("超级会员")]
SuperMember=1,
[Description("一般会员")]
Member=2,
[Description("普通用户")]
GeneralUser=3
}
public static class EnumExtension
{
public static string GetDescriptionEnum(this Enum enumValue)
{
DescriptionAttribute attr = null;
var enumType = enumValue.GetType();
string name = Enum.GetName(enumType, enumValue);
if (name != null)
{
FieldInfo fieldInfo = enumType.GetField(name);
if (fieldInfo != null)
attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
}
if (attr != null && !string.IsNullOrEmpty(attr.Description))
return attr.Description;
else return string.Empty;
}
}
参考博客:枚举知多少
四、C#的预处理指令
使用预处理指令可以禁止编译器编译与额外功能相关的代码,以控制不同版本拥有的功能,如企业版和基本版本
#define(给定名称的符号)和#undef(删除名称的符号)一般与#if、#elif、#else、#endif结合起来使用如

没有找到预定义的Debug就不会执行 #if和#endif代码块里面的语句,这也称为条件编译。










