Attribute类的使用
为每个变量设置设置属性 "Description"
public class PatternOption { /// <summary> /// 方向图步长 /// </summary> [Description("PT_SitaStepDeg")] public double StepDeg { get; set; }// /// <summary> /// 垂直面的切角(phi角) /// </summary> [Description("PT_VeritalCutPlaneDeg")] public double VeritalCutPlaneDeg { get; set; } /// <summary> /// 垂直面的切角(sita角) /// </summary> [Description("PT_HorizontalCutPlaneDeg")] public double HorizontalCutPlaneDeg { get; set; } /// <summary> /// 主瓣搜索起点 /// </summary> [Description("PT_MBStartDeg")] public double MainbeamSearchStartDeg { get; set; }// /// <summary> /// 主瓣搜索终点 /// </summary> [Description("PT_MBStopDeg")] public double MainbeamSearchStopDeg { get; set; }// /// <summary> /// 上旁瓣搜索起点,相对于主瓣方向,deg /// </summary> [Description("PT_UBOffsDeg")] public double UBStartOffsetFromMainBeamDeg { get; set; } /// <summary> /// 上旁瓣搜索终点偏移量,相对于主瓣方向,def /// </summary> [Description("PT_SitaStepDeg")] public double UBEndOffsetFromMainBeamDeg { get; set; }// [Description("PT_UBOffsDeg")] public bool UBCorrectToEdge { get; set; } }
通过反射读取 Description
#region 读取属性描述的例程, PatternOption patternOption = new PatternOption(); Type type = patternOption.GetType(); FieldInfo[] fieldInfos = type.GetFields(); foreach (FieldInfo item in fieldInfos) { DescriptionAttribute[] attributes = (DescriptionAttribute[])item.GetCustomAttributes(typeof(DescriptionAttribute), false); } #endregion