private void GetInfo(PropertyConfig config, Type type, MemberInfo field) { string name = $"{type.FullName}.{field.Name}"; var member = XmlMember.Find(name); if (member != null) { config.Description = member.Summary; config.Caption = member.DisplayName; config.Remark = member.Remark; } var ca = field.GetAttribute <DisplayNameAttribute>(); if (ca != null) { config.Caption = ca.DisplayName; } var ct = field.GetAttribute <CategoryAttribute>(); if (ct != null) { config.Group = ct.Category; } var de = field.GetAttribute <DescriptionAttribute>(); if (de != null) { config.Description = de.Description; } }
Assembly LoadAssembly(string path, string name) { var file = Path.Combine(path, name); if (loaded.Contains(file)) { return(null); } loaded.Add(file); string path2 = file.Replace("dll", "xml"); if (File.Exists(path2)) { HelpXml.AddRange(XmlMember.LoadHelpXml(path2)); } var p2 = Path.GetDirectoryName(GetType().Assembly.Location); var asm = Assembly.LoadFile(file); IOHelper.CopyPath(path, p2, "*.dll", false, false); System.Diagnostics.Trace.WriteLine(Path.Combine(path, name)); foreach (var friend in asm.GetReferencedAssemblies()) { file = Path.Combine(path, friend.Name + ".dll"); if (File.Exists(file)) { LoadAssembly(path, friend.Name + ".dll"); } } return(asm); }
internal void WriteNull(XmlWriter writer, Type valueType, XmlMember member) { var nullValueHandling = Settings.NullValueHandling; if (member != null) { if (member.MappingType == XmlMappingType.Attribute) { return; } nullValueHandling = member.NullValueHandling ?? nullValueHandling; } if (nullValueHandling != XmlNullValueHandling.Ignore) { if (member == null) { member = Settings.GetTypeContext(valueType).Contract.Root; } writer.WriteStartElement(member.Name); if (initialState) { initialState = false; WriteNamespaces(writer); } writer.WriteAttributeString(Settings.NullAttributeName, "true"); writer.WriteEndElement(); } }
private void GetInfo(ClassifyConfig config, Type type) { var member = XmlMember.Find(type); if (member != null) { config.Caption = member.DisplayName; config.Description = member.Summary; config.Remark = member.Remark; } var ca = type.GetAttribute <DisplayNameAttribute>(); if (ca != null) { config.Caption = ca.DisplayName; } var ct = type.GetAttribute <CategoryAttribute>(); if (ct != null) { config.Classify = ct.Category; } var de = type.GetAttribute <DescriptionAttribute>(); if (de != null) { config.Description = de.Description; } }
internal XmlSerializationContext(XmlSerializerSettings settings, XmlMember member, XmlContract contract) : this(settings) { Contract = contract ?? throw new ArgumentNullException("contract"); Member = member ?? throw new ArgumentNullException("member"); initialState = false; }
public void SerializeBody(XmlWriter writer, object value, XmlMember member) { if (member == null) { throw new ArgumentNullException("member"); } SerializeBody(writer, value, member.ValueType, member); }
/// <summary> /// 查找API /// </summary> public void Discover(Type type) { logger = DependencyHelper.LoggerFactory.CreateLogger <ApiDiscover>(); Assembly = type.Assembly; XmlMember.Load(Assembly); if (type.IsSupperInterface(typeof(IApiController))) { FindApi(type); } }
/// <summary> /// 查找API /// </summary> public void Discover(Assembly assembly) { logger = DependencyHelper.LoggerFactory.CreateLogger <ApiDiscover>(); XmlMember.Load(assembly); Assembly = assembly; var types = Assembly.GetTypes().Where(p => p.IsSupperInterface(typeof(IApiController))).ToArray(); foreach (var type in types) { FindApi(type); } }
internal void WriteXml(XmlWriter writer, object value, XmlMember member, XmlTypeContext typeContext) { XmlMember lastMember = currentMember; XmlContract lastContract = currentContract; currentMember = member; currentContract = typeContext.Contract; typeContext.WriteXml(writer, value, this); currentMember = lastMember; currentContract = lastContract; }
internal void WriteXml(XmlWriter writer, object value, XmlMember member, XmlTypeContext typeContext) { var lastMember = Member; var lastContract = Contract; Member = member; Contract = typeContext.Contract; typeContext.WriteXml(writer, value, this); Member = lastMember; Contract = lastContract; }
internal object ReadXml(XmlReader reader, XmlMember member, XmlTypeContext typeContext) { XmlMember lastMember = currentMember; XmlContract lastContract = currentContract; currentMember = member; currentContract = typeContext.Contract; object value = typeContext.ReadXml(reader, this); currentMember = lastMember; currentContract = lastContract; return(value); }
internal object ReadXml(XmlReader reader, XmlMember member, XmlTypeContext typeContext) { var lastMember = Member; var lastContract = Contract; Member = member; Contract = typeContext.Contract; var value = typeContext.ReadXml(reader, this); Member = lastMember; Contract = lastContract; return(value); }
internal static XmlNameRef[] GetKnownNameRefs(XmlMember item, XmlNameTable nameTable) { if (item.KnownTypes.Count == 0) { return(null); } var nameRefs = new XmlNameRef[item.KnownTypes.Count]; for (var i = 0; i < nameRefs.Length; i++) { nameRefs[i].Reset(item.KnownTypes[i].Name, nameTable); } return(nameRefs); }
/// <summary> /// 查找API /// </summary> public static void FindApies(Assembly asm) { var discover = new ApiDiscover { Assembly = asm }; try { XmlMember.Load(asm); discover.FindApies(); } catch (Exception e2) { discover.logger.Debug(e2.ToString()); } }
internal XmlSerializationContext(XmlSerializerSettings settings, XmlMember member, XmlContract contract) : this(settings) { if (contract == null) { throw new ArgumentNullException("contract"); } if (member == null) { throw new ArgumentNullException("member"); } currentContract = contract; currentMember = member; initialState = false; }
private void SerializeBody(XmlWriter writer, object value, Type memberType, XmlMember member) { if (writer == null) { throw new ArgumentNullException("writer"); } if (value == null) { WriteNull(writer, memberType, member); } else { var typeContext = Settings.GetTypeContext(memberType); WriteXml(writer, value, member ?? typeContext.Contract.Root, typeContext); } }
internal bool TryResolveValueType(object value, ref XmlMember member, out Type valueType) { if (member.IsOpenType) { var typeHandling = member.TypeHandling ?? Settings.TypeHandling; if (typeHandling != XmlTypeHandling.None) { valueType = value.GetType(); member = member.ResolveMember(valueType); return(typeHandling == XmlTypeHandling.Always || valueType != member.ValueType); } } valueType = member.ValueType; return(false); }
private object Deserialize(XmlReader reader, Type valueType, XmlMember member) { if (reader == null) { throw new ArgumentNullException("reader"); } if (valueType == null) { throw new ArgumentNullException("valueType"); } if (initialState && reader.NodeType == XmlNodeType.None) { initialState = false; while (reader.NodeType != XmlNodeType.Element) { if (!reader.Read()) { return(null); } } } if (reader.NodeType == XmlNodeType.Element) { if (!ReadValueType(reader, ref valueType)) { reader.Skip(); return(null); } } var typeInfo = Settings.GetTypeContext(valueType); if (member == null) { member = typeInfo.Contract.Root; } return(ReadXml(reader, member, typeInfo)); }
public static void AreEqual(XmlMember expected, XmlMember actual) { if (expected == null) { Assert.IsNull(actual); return; } Assert.IsNotNull(actual); Assert.IsInstanceOf(expected.GetType(), actual); Assert.AreEqual(expected.ValueType, actual.ValueType); Assert.AreEqual(expected.Name, actual.Name); Assert.AreEqual(expected.MappingType, actual.MappingType); Assert.AreEqual(expected.DefaultValueHandling.HasValue, actual.DefaultValueHandling.HasValue); Assert.AreEqual(expected.DefaultValueHandling, actual.DefaultValueHandling); Assert.AreEqual(expected.DefaultValue, actual.DefaultValue); Assert.AreEqual(expected.TypeHandling.HasValue, actual.TypeHandling.HasValue); Assert.AreEqual(expected.TypeHandling, actual.TypeHandling); Assert.AreEqual(expected.NullValueHandling.HasValue, actual.NullValueHandling.HasValue); Assert.AreEqual(expected.NullValueHandling, actual.NullValueHandling); Assert.AreEqual(expected.KnownTypes.Count, actual.KnownTypes.Count); foreach (var expectedKnownType in expected.KnownTypes) { var actualKnownType = actual.KnownTypes.FirstOrDefault(x => x.ValueType == expectedKnownType.ValueType); AreEqual(expectedKnownType, actualKnownType); } AreEqual(expected.Item, actual.Item); if (expected is XmlProperty) { var ep = (XmlProperty)expected; var ap = (XmlProperty)actual; Assert.AreEqual(ep.PropertyInfo, ap.PropertyInfo); Assert.AreEqual(ep.PropertyName, ap.PropertyName); Assert.AreEqual(ep.IsRequired, ap.IsRequired); Assert.AreEqual(ep.Order, ap.Order); Assert.AreEqual(ep.IsCollection, ap.IsCollection); } }
internal void WriteNull(XmlWriter writer, Type valueType, XmlMember member) { // don't write to writer if building object graph if (Settings.IsBuildingObjectGraph) { return; } XmlNullValueHandling nullValueHandling = settings.NullValueHandling; if (member != null) { if (member.MappingType == XmlMappingType.Attribute) { return; } nullValueHandling = member.NullValueHandling ?? nullValueHandling; } if (nullValueHandling != XmlNullValueHandling.Ignore) { if (member == null) { member = settings.GetTypeContext(valueType).Contract.Root; } writer.WriteStartElement(member.Name); if (initialState) { initialState = false; WriteNamespaces(writer); } writer.WriteAttributeString(settings.NullAttributeName, "true"); writer.WriteEndElement(); } }
private static XmlSerializationContext CreateContext(Type valueType, XmlMember member, XmlContract contract, XmlSerializerSettings settings) { if (settings == null) { settings = new XmlSerializerSettings { OmitXmlDeclaration = true, ContractResolver = new XmlContractResolver(NamingConventions.CamelCase) }; } if (contract == null) { contract = settings.ContractResolver.ResolveContract(valueType); } if (member == null) { member = contract.Root; } return(new XmlSerializationContext(settings, member, contract)); }
public static string ToXml <TValue>(this IXmlConverter converter, TValue value, XmlMember member = null, XmlContract contract = null, XmlSerializerSettings settings = null) { return(ToXml(converter, typeof(TValue), value, member, contract, settings)); }
public object Deserialize(XmlReader reader, XmlMember member) { return(Deserialize(reader, member.ValueType, member)); }
private void Serialize(XmlWriter writer, object value, Type memberType, XmlMember member) { if (writer == null) { throw new ArgumentNullException("writer"); } if (value == null) { WriteNull(writer, memberType, member); return; } XmlTypeContext context = null; if (member == null) { context = Settings.GetTypeContext(memberType); member = context.Contract.Root; } var shouldWriteTypeName = TryResolveValueType(value, ref member, out Type valueType); if (member.DefaultValue != null) { var defaultValueHandling = member.DefaultValueHandling ?? Settings.DefaultValueHandling; if (defaultValueHandling == XmlDefaultValueHandling.Ignore && value.Equals(member.DefaultValue)) { return; } } if (context == null || context.Contract.ValueType != member.ValueType) { context = Settings.GetTypeContext(valueType); } switch (member.MappingType) { case XmlMappingType.Element: writer.WriteStartElement(member.Name); if (initialState) { initialState = false; WriteNamespaces(writer); } if (shouldWriteTypeName) { WriteTypeName(writer, valueType); } WriteXml(writer, value, member, context); writer.WriteEndElement(); break; case XmlMappingType.Attribute: writer.WriteStartAttribute(member.Name); WriteXml(writer, value, member, context); writer.WriteEndAttribute(); break; case XmlMappingType.InnerText: WriteXml(writer, value, member, context); break; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// 构造 /// </summary> static ApiDiscover() { XmlMember.Load(typeof(IMessageItem).Assembly); XmlMember.Load(typeof(ApiExecuter).Assembly); }
static XmlDoc LoadXml(XElement document) { XmlDoc returnValue = new XmlDoc(); if (document != null) { var assemblyElement = document.Element("assembly"); if (assemblyElement != null) { returnValue.Assembly = new XmlAssembly(); var nameElement = assemblyElement.Element("name"); if (nameElement != null) { returnValue.Assembly.Name = nameElement.Value; } } var members = document.Element("members"); if (members != null) { var xmlMembers = new List <XmlMember>(); var items = members.Elements(); foreach (var member in items) { XmlMember xmlMember = new XmlMember(); //Name var nameAttribute = member.Attribute("name"); if (nameAttribute != null) { xmlMember.Name = nameAttribute.Value; } //Summary var summaryElement = member.Element("summary"); if (summaryElement != null) { if (!string.IsNullOrEmpty(summaryElement.Value)) { xmlMember.Summary = summaryElement.Value.Trim(); } } var paramElements = member.Elements("param"); if (paramElements != null) { var xmlMemberParamList = new List <XmlMemberParam>(); foreach (var paramElement in paramElements) { var paramNameAttribute = paramElement.Attribute("name"); var paramValueElement = paramElement.Value; if (paramNameAttribute != null && !string.IsNullOrEmpty(paramNameAttribute.Value) && !string.IsNullOrEmpty(paramValueElement)) { var xmlMemberParam = new XmlMemberParam(); xmlMemberParam.Name = paramNameAttribute.Value; xmlMemberParam.Value = paramValueElement; xmlMemberParamList.Add(xmlMemberParam); } } if (xmlMemberParamList.Count > 0) { xmlMember.Param = xmlMemberParamList.ToArray(); } } var returns = member.Element("returns"); if (returns != null) { if (!string.IsNullOrEmpty(returns.Value)) { xmlMember.Returns = returns.Value; } } xmlMembers.Add(xmlMember); } returnValue.Members = xmlMembers.ToArray(); } var x = 0; } return(returnValue); }
public static string ToXml(this IXmlConverter converter, Type valueType, object value, XmlMember member = null, XmlContract contract = null, XmlSerializerSettings settings = null) { var builder = new StringBuilder(); var context = CreateContext(valueType, member, contract, settings); using (var output = new StringWriter(builder)) using (var writer = XmlWriter.Create(output, context.Settings.GetWriterSettings())) { writer.WriteStartElement("xml"); var isAttribute = context.Member.MappingType == XmlMappingType.Attribute; if (isAttribute) { writer.WriteStartAttribute(context.Member.Name); } converter.WriteXml(writer, value, context); if (isAttribute) { writer.WriteEndAttribute(); } writer.WriteEndElement(); } return(builder.ToString()); }
private void Serialize(XmlWriter writer, object value, Type memberType, XmlMember member) { if (writer == null) { throw new ArgumentNullException("writer"); } // ignore optional values with none value if (value is IAnyOptional optional) { if (!optional.HasValue) { if (Settings.NoneValueHandling == XmlNoneValueHandling.Ignore) { return; } // Output default values for inner types var innerType = optional.GetInnerType(); var nullableInnerType = innerType.GetUnderlyingNullableType(); if (nullableInnerType != null) { WriteNull(writer, memberType, member); return; } else if (innerType.IsClass) { WriteNull(writer, memberType, member); return; } } else if (optional.Value == null) { WriteNull(writer, memberType, member); return; } } if (value == null) { WriteNull(writer, memberType, member); return; } if (settings.EmptyCollectionHandling == XmlEmptyCollectionHandling.Ignore && value.IsEmpty() == true) { return; } XmlTypeContext context = null; if (member == null) { context = settings.GetTypeContext(memberType); member = context.Contract.Root; } bool shouldWriteTypeName = TryResolveValueType(value, ref member, out Type valueType); if (member.DefaultValue != null) { XmlDefaultValueHandling defaultValueHandling = member.DefaultValueHandling ?? settings.DefaultValueHandling; if (defaultValueHandling == XmlDefaultValueHandling.Ignore && value.Equals(member.DefaultValue)) { return; } } if (context == null || context.Contract.ValueType != member.ValueType) { context = settings.GetTypeContext(valueType); } switch (member.MappingType) { case XmlMappingType.Element: WriteElement(writer, value, member, context, valueType, shouldWriteTypeName); break; case XmlMappingType.Attribute: if (!Settings.IsBuildingObjectGraph) { writer.WriteStartAttribute(member.Name); } WriteXml(writer, value, member, context); if (!Settings.IsBuildingObjectGraph) { writer.WriteEndAttribute(); } break; case XmlMappingType.InnerText: if (!Settings.IsBuildingObjectGraph) { WriteXml(writer, value, member, context); } break; } }
internal void WriteElement(XmlWriter writer, object value, XmlMember member, XmlTypeContext context, Type valueType, bool shouldWriteTypeName) { var contractValueType = context.Contract.ValueType; if (!Settings.IsBuildingObjectGraph) { writer.WriteStartElement(member.Name); if (initialState) { initialState = false; WriteNamespaces(writer); } if (shouldWriteTypeName) { WriteTypeName(writer, valueType); } } object referencedValue = value; if (value is IAnyOptional optValue && optValue.HasValue) { contractValueType = optValue.GetInnerType(); referencedValue = optValue.Value; } var isCollection = !contractValueType.Equals(typeof(string)) && contractValueType.IsEnumerable(); var isModel = contractValueType.IsClass && contractValueType != typeof(string); if (context.Contract is XmlObjectContract && isModel && !isCollection) { long id = settings.ReferenceHandlingGenerator.GetId(referencedValue, out bool firstTime); if (Settings.IsBuildingObjectGraph) { if (firstTime) { _xmlReferenceLevelLookup.Add(id, currentLevel); currentLevel++; WriteXml(writer, value, member, context); currentLevel--; } else { if (Settings.ReferenceHandling == XmlReferenceHandling.Throw) { throw new Exception("Found reference loop. Please set ReferenceHandling setting to XmlReferenceHandling.Handle"); } if (currentLevel < _xmlReferenceLevelLookup[id]) { _xmlReferenceLevelLookup[id] = currentLevel; } } // stop processing return; } bool isExpansionElement = Settings.ReferenceExpansion == XmlReferenceExpansion.FirstAccessed ? firstTime : currentLevel == _xmlReferenceLevelLookup[id]; if (isExpansionElement) { if (Settings.ReferenceExpansion == XmlReferenceExpansion.HighestLevel) { // element should only be expanded once - prevents expansion of elements on the same level _xmlReferenceLevelLookup[id] = -1; } if (Settings.ReferenceHandling == XmlReferenceHandling.Handle) { writer.WriteAttributeString(Settings.ReferenceHandlingIdName, id.ToString()); } currentLevel++; WriteXml(writer, value, member, context); currentLevel--; } else { if (Settings.ReferenceHandling == XmlReferenceHandling.Throw) { throw new Exception("Found reference loop. Please set ReferenceHandling setting to XmlReferenceHandling.Handle"); } writer.WriteAttributeString(Settings.ReferenceHandlingReferenceName, id.ToString()); } } else { if (!Settings.IsBuildingObjectGraph || isCollection) { WriteXml(writer, value, member, context); } } if (!Settings.IsBuildingObjectGraph) { writer.WriteEndElement(); } }
public static object ParseXml(this IXmlConverter converter, Type valueType, string xmlString, XmlMember member = null, XmlContract contract = null, XmlSerializerSettings settings = null) { var context = CreateContext(valueType, member, contract, settings); using (var input = new StringReader(xmlString)) using (var reader = XmlReader.Create(input, context.Settings.GetReaderSettings())) { while (reader.NodeType != XmlNodeType.Element && reader.Read()) { } if (reader.Name != "xml") { Assert.Fail("Expected start element \"xml\"."); } var isAttribute = context.Member.MappingType == XmlMappingType.Attribute; if (isAttribute) { reader.MoveToAttribute(context.Member.Name.LocalName, context.Member.Name.NamespaceUri); } var value = converter.ReadXml(reader, context); if (isAttribute) { reader.MoveToElement(); if (reader.NodeType != XmlNodeType.Element || reader.Name != "xml") { Assert.Fail("Expected element \"xml\"."); } } else if (reader.NodeType != XmlNodeType.None) { Assert.Fail("Expected end of xml."); } return(value); } }