private static void CheckAttributes(UpgradeConfig config, IList <CustomAttributeData> attributes) { foreach (CustomAttributeData cad in attributes) { var info = new AttributeInfo { Name = cad.AttributeType.GetTypeName(), }; config.CustomAttributes.Add(cad.AttributeType.FullName, info); foreach (CustomAttributeTypedArgument cata in cad.ConstructorArguments) { info.Constructors.AddRange(GetValues(cata)); } if (cad.NamedArguments != null) { foreach (CustomAttributeNamedArgument cana in cad.NamedArguments) { info.Values.Add(cana.MemberInfo.Name, GetValues(cana.TypedValue)); } } } if (config.CustomAttributes.Count == 0) { config.CustomAttributes = null; } }
//https://msdn.microsoft.com/zh-cn/library/system.reflection.customattributedata.namedarguments(v=vs.110).aspx private void CheckAttributes(UpgradeConfig config, MemberInfo member) { var descript = member.GetCustomAttribute <DescriptionAttribute>(); if (descript != null) { config.Description = descript.Description; } var category = member.GetCustomAttribute <CategoryAttribute>(); if (category != null) { config.Category = category.Category; } var displayName = member.GetCustomAttribute <DisplayNameAttribute>(); if (displayName != null) { config.Caption = displayName.DisplayName; } var json = member.GetCustomAttribute <JsonPropertyAttribute>(); if (json != null) { config.IsJsonAttribute = true; config.JsonName = !string.IsNullOrWhiteSpace(json.PropertyName) ? json.PropertyName : config.Name; } config.IsDataAttribute = member.GetCustomAttribute(typeof(DataMemberAttribute)) != null; CheckAttributes(config, member.GetCustomAttributesData()); }
private void GetInfo(UpgradeConfig config, Type type, MemberInfo field) { var member = _helpXml.FirstOrDefault(p => p.Name == type.FullName + "." + field.Name); if (member != null) { config.Description = member.Summary; config.Caption = member.DisplayName; config.Description = member.Remark ?? member.Summary; } GetExtendValue(field, config); CheckAttributes(config, field); }
private static void GetExtendValue(object obj, UpgradeConfig paraInfo) { foreach (var pro in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (!pro.CanRead || pro.Name == "DeclaringMethod" || pro.Name == "GenericParameterAttributes" || pro.Name == "Name" || pro.Name == "GenericParameterPosition" || pro.Name == "FieldHandle" || pro.Name == "MetadataToken" || pro.Name == "MemberType" || pro.Name == "TypeHandle" || pro.Name == "AssemblyQualifiedName" || pro.Name == "GUID") { continue; } try { var value = pro.GetValue(obj); if (value == null) { continue; } if (value is string) { paraInfo.Values.Add(pro.Name, (string)value); } else if (value is bool) { if ((bool)value) { paraInfo.Values.Add(pro.Name, "true"); } } else if (value.GetType().IsValueType) { paraInfo.Values.Add(pro.Name, value.ToString()); } } catch (Exception ex) { Console.WriteLine(ex); } } if (paraInfo.Values.Count == 0) { paraInfo.Values = null; } }