/// <summary> /// 反序列化操作 /// </summary> public object Deserialize() { if (null == XmlDoc.DocumentElement) { throw new InvalidDataException(string.Format("{0}, 没有加载XML内容", MethodBase.GetCurrentMethod().Name)); } // 验证XML内容是否被成功加载 //XmlRootAttribute attr = SerializeObjectType.GetCustomAttributes(true)?.First(ca => "XmlRootAttribute" == ca.GetType().Name) as XmlRootAttribute; var customAttrs = SerializeObjectType.GetCustomAttributes(true); XmlRootAttribute attr = (null != customAttrs) ? customAttrs.First(ca => "XmlRootAttribute" == ca.GetType().Name) as XmlRootAttribute : null; if (null == attr) { throw new XmlException(string.Format("{0}, 待反序列化的数据类型({1})不包含可序列化的XmlRoot属性", MethodBase.GetCurrentMethod().Name, SerializeObjectType)); } if (NodeNameMatch(XmlDoc.DocumentElement, attr.ElementName)) { try { _serializeObj = SerializeObjectType.Assembly.CreateInstance(SerializeObjectType.FullName); // 获取序列化实体的成员变量和属性 List <MemberInfo> miList = new List <MemberInfo>(1); miList.AddRange(SerializeObj.GetType().GetFields()); miList.AddRange(SerializeObj.GetType().GetProperties()); // 解析所有子节点 ParseNodes(XmlDoc.DocumentElement.ChildNodes, ref _serializeObj, miList); } catch (Exception ex) { MyLog.Instance.Error(ex, "在{0}中,反序列化{1}实体异常。", MethodBase.GetCurrentMethod().Name, SerializeObjectType); } } else { throw new XmlException(string.Format("{0}, 待反序列化的数据类型({1})与XML内容不匹配", MethodBase.GetCurrentMethod().Name, SerializeObjectType)); } // if (NodeNameMatch(XmlDoc.DocumentElement, attr.ElementName)) else return(SerializeObj); }
public static object ConvertType(Type toType, object value) { if (value == null) { return(null); } SerializeObjectType targetPropertyType = GetTypeCodeOfObject(toType); if (targetPropertyType == SerializeObjectType.Boolean) { if (bool.TryParse(value.ToString(), out bool result)) { return(result); } return(default(bool)); } else if (targetPropertyType == SerializeObjectType.BooleanNullable) { if (bool.TryParse(value.ToString(), out bool result)) { return((bool?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.Byte) { if (byte.TryParse(value.ToString(), out byte result)) { return(result); } return(default(byte)); } else if (targetPropertyType == SerializeObjectType.ByteNullable) { if (byte.TryParse(value.ToString(), out byte result)) { return((byte?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.Char) { if (char.TryParse(value.ToString(), out char result)) { return(result); } return(default(char)); } else if (targetPropertyType == SerializeObjectType.CharNullable) { if (char.TryParse(value.ToString(), out char result)) { return((char?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.DateTime) { if (DateTime.TryParse(value.ToString(), out DateTime result)) { return(result); } return(default(DateTime)); } else if (targetPropertyType == SerializeObjectType.DateTimeNullable) { if (DateTime.TryParse(value.ToString(), out DateTime result)) { return((DateTime?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.DateTimeOffset) { if (DateTimeOffset.TryParse(value.ToString(), out DateTimeOffset result)) { return(result); } return(default(DateTimeOffset)); } else if (targetPropertyType == SerializeObjectType.DateTimeOffsetNullable) { if (DateTimeOffset.TryParse(value.ToString(), out DateTimeOffset result)) { return((DateTimeOffset?)result); } return(null); //return (DateTimeOffset?)new DateTimeOffset(Convert.ToDateTime(value)); } else if (targetPropertyType == SerializeObjectType.Decimal) { if (decimal.TryParse(value.ToString(), out decimal result)) { return(result); } return(default(decimal)); } else if (targetPropertyType == SerializeObjectType.DecimalNullable) { if (decimal.TryParse(value.ToString(), out decimal result)) { return((decimal?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.Double) { if (double.TryParse(value.ToString(), out double result)) { return(result); } return(default(double)); } else if (targetPropertyType == SerializeObjectType.DoubleNullable) { if (double.TryParse(value.ToString(), out double result)) { return((double?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.Enum) { if (value == null) { Array values = Enum.GetValues(toType); if (values.Length > 0) { return(Enum.GetValues(toType).GetValue(0)); } else { return(null); } } return(Enum.Parse(toType, value.ToString(), true)); } else if (targetPropertyType == SerializeObjectType.EnumNullable) { try { if (value.ToString() == "null") { return(null); } Type nullableType = Nullable.GetUnderlyingType(toType); return(Enum.Parse(nullableType, value.ToString())); } catch (Exception ex) { AutoLogger.LogError(ex, $"cannot convert value {value} type of {value.GetType()} to EnumNullable!"); return(null); } } else if (targetPropertyType == SerializeObjectType.Guid) { #if (NET35) return(new Guid(value.ToString())); #else if (Guid.TryParse(value.ToString(), out Guid result)) { return(result); } return(Guid.Empty); #endif } else if (targetPropertyType == SerializeObjectType.Int16) { if (short.TryParse(value.ToString(), out short result)) { return(result); } return(default(short)); } else if (targetPropertyType == SerializeObjectType.Int16Nullable) { if (short.TryParse(value.ToString(), out short result)) { return(result); } return(null); } else if (targetPropertyType == SerializeObjectType.Int32) { if (int.TryParse(value.ToString(), out int result)) { return(result); } return(default(int)); } else if (targetPropertyType == SerializeObjectType.Int32Nullable) { if (int.TryParse(value.ToString(), out int result)) { return((int?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.Int64) { if (long.TryParse(value.ToString(), out long result)) { return(result); } return(default(long)); } else if (targetPropertyType == SerializeObjectType.Int64Nullable) { if (long.TryParse(value.ToString(), out long result)) { return((long?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.UInt16) { if (ushort.TryParse(value.ToString(), out ushort result)) { return(result); } return(default(ushort)); } else if (targetPropertyType == SerializeObjectType.UInt16Nullable) { if (ushort.TryParse(value.ToString(), out ushort result)) { return((ushort?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.UInt32) { if (uint.TryParse(value.ToString(), out uint result)) { return(result); } return(default(uint)); } else if (targetPropertyType == SerializeObjectType.UInt32Nullable) { if (uint.TryParse(value.ToString(), out uint result)) { return((uint?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.UInt64) { if (ulong.TryParse(value.ToString(), out ulong result)) { return(result); } return(default(ulong)); } else if (targetPropertyType == SerializeObjectType.UInt64Nullable) { if (ulong.TryParse(value.ToString(), out ulong result)) { return((ulong?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.SByte) { if (sbyte.TryParse(value.ToString(), out sbyte result)) { return(result); } return(default(sbyte)); } else if (targetPropertyType == SerializeObjectType.SByteNullable) { if (sbyte.TryParse(value.ToString(), out sbyte result)) { return((sbyte?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.Single) { if (float.TryParse(value.ToString(), out float result)) { return(result); } return(default(float)); } else if (targetPropertyType == SerializeObjectType.FloatNullable) { if (float.TryParse(value.ToString(), out float result)) { return((float?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.String) { if (value.ToString() == "null") { return(null); } return(value.ToString()); } else if (targetPropertyType == SerializeObjectType.TimeSpan) { if (TimeSpan.TryParse(value.ToString(), out TimeSpan result)) { return(result); } return(TimeSpan.MinValue); } else if (targetPropertyType == SerializeObjectType.TimeSpanNullable) { if (TimeSpan.TryParse(value.ToString(), out TimeSpan result)) { return((TimeSpan?)result); } return(null); } else if (targetPropertyType == SerializeObjectType.Uri) { if (Uri.TryCreate(value.ToString(), UriKind.RelativeOrAbsolute, out Uri result)) { return(result); } return(null); } else { return(null); } }
private string TypeToJsonString(Type type) { List <Type> createdInstance = new List <Type>(); return(ServerSerializationHelper.SerializeObject(CreateInstances(type, createdInstance), null, NullValueHandling.Include)); object CreateInstances(Type newType, List <Type> items) { if (items.Contains(newType)) { return(DataExchangeConverter.GetDefault(newType)); } items.Add(newType); object result = null; SerializeObjectType typeCode = SerializeHelper.GetTypeCodeOfObject(newType); if (typeCode == SerializeObjectType.Object) { #if (NETSTANDARD1_6 || NETCOREAPP1_1) if (!newType.GetTypeInfo().IsInterface) #else if (!newType.IsInterface) #endif { try { result = Activator.CreateInstance(newType); foreach (PropertyInfo item in newType.GetProperties()) { item.SetValue(result, CreateInstances(item.PropertyType, items), null); } } catch (Exception) { } } else if (newType.GetGenericTypeDefinition() == typeof(ICollection <>) || newType.GetGenericTypeDefinition() == typeof(IList <>)) { try { Type gType = newType.GetListOfGenericArguments().FirstOrDefault(); Type listType = typeof(List <>).MakeGenericType(gType); result = Activator.CreateInstance(listType); } catch { } } } else { result = DataExchangeConverter.GetDefault(newType); } #if (NETSTANDARD1_6 || NETCOREAPP1_1) if (newType.GetTypeInfo().IsGenericType&& result != null) #else if (newType.IsGenericType && result != null) #endif { if (newType.GetGenericTypeDefinition() == typeof(List <>) || newType.GetGenericTypeDefinition() == typeof(ICollection <>) || newType.GetGenericTypeDefinition() == typeof(IList <>)) { Type gType = newType.GetListOfGenericArguments().FirstOrDefault(); if (gType != null) { try { object gResult = Activator.CreateInstance(gType); foreach (PropertyInfo item in gType.GetProperties()) { item.SetValue(gResult, CreateInstances(item.PropertyType, items), null); } result.GetType().GetMethod("Add").Invoke(result, new object[] { gResult }); //col.Add(gResult); } catch { } } } } return(result); } }
internal ProviderDetailsInfo SendServiceDetail(string hostUrl, ServerBase serverBase) { //try //{ if (!hostUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { hostUrl += "http://"; } if (Uri.TryCreate(hostUrl, UriKind.Absolute, out Uri uri)) { hostUrl = uri.Host + ":" + uri.Port; } using (XmlCommentLoader xmlCommentLoader = new XmlCommentLoader()) { List <Type> modelTypes = new List <Type>(); int id = 1; ProviderDetailsInfo result = new ProviderDetailsInfo() { Id = id }; foreach (KeyValuePair <string, Type> service in serverBase.RegisteredServiceTypes.Where(x => x.Value.IsServerService())) { id++; ServiceDetailsInfo serviceDetail = new ServiceDetailsInfo() { ServiceName = service.Key, FullNameSpace = service.Value.FullName, NameSpace = service.Value.Name, Id = id }; result.Services.Add(serviceDetail); List <Type> types = new List <Type>(); types.Add(service.Value); foreach (Type item in CSCodeInjection.GetListOfTypes(service.Value)) { if (item.GetCustomAttributes <ServiceContractAttribute>(false).Length > 0 && !types.Contains(item)) { types.Add(item); types.AddRange(CSCodeInjection.GetListOfTypes(service.Value).Where(x => !types.Contains(x))); } } foreach (Type serviceType in types) { if (serviceType == typeof(object)) { continue; } List <MethodInfo> methods = serviceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).Where(x => !(x.IsSpecialName && (x.Name.StartsWith("set_") || x.Name.StartsWith("get_"))) && x.DeclaringType != typeof(object)).ToList(); if (methods.Count == 0) { continue; } CommentOfClassInfo comment = xmlCommentLoader.GetComment(serviceType); id++; ServiceDetailsInterface interfaceInfo = new ServiceDetailsInterface() { NameSpace = serviceType.Name, FullNameSpace = serviceType.FullName, Comment = comment?.Summery, Id = id }; serviceDetail.Services.Add(interfaceInfo); List <ServiceDetailsMethod> serviceMethods = new List <ServiceDetailsMethod>(); foreach (MethodInfo method in methods) { SerializeObjectType pType = SerializeHelper.GetTypeCodeOfObject(method.ReturnType); if (pType == SerializeObjectType.Enum) { AddEnumAndNewModels(ref id, method.ReturnType, result, SerializeObjectType.Enum, xmlCommentLoader); } CommentOfMethodInfo methodComment = comment == null ? null : (from x in comment.Methods where x.Name == method.Name && x.Parameters.Count == method.GetParameters().Length select x).FirstOrDefault(); string exceptions = ""; if (methodComment?.Exceptions != null && methodComment?.Exceptions.Count > 0) { foreach (CommentOfExceptionInfo ex in methodComment.Exceptions) { try { if (ex.RefrenceType.LastIndexOf('.') != -1) { string baseNameOfEnum = ex.RefrenceType.Substring(0, ex.RefrenceType.LastIndexOf('.')); Type type = GetEnumType(baseNameOfEnum); #if (NETSTANDARD1_6 || NETCOREAPP1_1) if (type != null && type.GetTypeInfo().IsEnum) #else if (type != null && type.IsEnum) #endif { object value = Enum.Parse(type, ex.RefrenceType.Substring(ex.RefrenceType.LastIndexOf('.') + 1, ex.RefrenceType.Length - ex.RefrenceType.LastIndexOf('.') - 1)); int exNumber = (int)value; exceptions += ex.RefrenceType + $" ({exNumber}) : " + ex.Comment + TextHelper.NewLine; continue; } } } catch { } exceptions += ex.RefrenceType + ":" + ex.Comment + TextHelper.NewLine; } } id++; ServiceDetailsMethod info = new ServiceDetailsMethod() { MethodName = method.Name, #if (!NET35) Requests = new System.Collections.ObjectModel.ObservableCollection <ServiceDetailsRequestInfo>() { new ServiceDetailsRequestInfo() { Name = "Default", Parameters = new List <ServiceDetailsParameterInfo>(), IsSelected = true } }, #endif ReturnType = method.ReturnType.GetFriendlyName(), Comment = methodComment?.Summery, ReturnComment = methodComment?.Returns, ExceptionsComment = exceptions, Id = id }; RuntimeTypeHelper.GetListOfUsedTypes(method.ReturnType, ref modelTypes); foreach (System.Reflection.ParameterInfo paramInfo in method.GetParameters()) { pType = SerializeHelper.GetTypeCodeOfObject(paramInfo.ParameterType); if (pType == SerializeObjectType.Enum) { AddEnumAndNewModels(ref id, paramInfo.ParameterType, result, SerializeObjectType.Enum, xmlCommentLoader); } string parameterComment = ""; if (methodComment != null) { parameterComment = (from x in methodComment.Parameters where x.Name == paramInfo.Name select x.Comment).FirstOrDefault(); } id++; ServiceDetailsParameterInfo p = new ServiceDetailsParameterInfo() { Name = paramInfo.Name, Type = paramInfo.ParameterType.GetFriendlyName(), FullTypeName = paramInfo.ParameterType.FullName, Comment = parameterComment, Id = id }; #if (!NET35) info.Requests.First().Parameters.Add(p); #endif RuntimeTypeHelper.GetListOfUsedTypes(paramInfo.ParameterType, ref modelTypes); } serviceMethods.Add(info); } interfaceInfo.Methods.AddRange(serviceMethods); } } foreach (KeyValuePair <string, Type> service in serverBase.RegisteredServiceTypes.Where(x => x.Value.IsClientService())) { id++; CallbackServiceDetailsInfo serviceDetail = new CallbackServiceDetailsInfo() { ServiceName = service.Key, FullNameSpace = service.Value.FullName, NameSpace = service.Value.Name, Id = id }; result.Callbacks.Add(serviceDetail); List <Type> types = new List <Type>(); types.Add(service.Value); foreach (Type item in CSCodeInjection.GetListOfTypes(service.Value)) { if (item.GetCustomAttributes <ServiceContractAttribute>(false).Length > 0 && !types.Contains(item)) { types.Add(item); types.AddRange(CSCodeInjection.GetListOfTypes(service.Value).Where(x => !types.Contains(x))); } } List <MethodInfo> methods = service.Value.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).Where(x => !(x.IsSpecialName && (x.Name.StartsWith("set_") || x.Name.StartsWith("get_"))) && x.DeclaringType != typeof(object)).ToList(); if (methods.Count == 0) { continue; } CommentOfClassInfo comment = xmlCommentLoader.GetComment(service.Value); List <ServiceDetailsMethod> serviceMethods = new List <ServiceDetailsMethod>(); foreach (MethodInfo method in methods) { SerializeObjectType pType = SerializeHelper.GetTypeCodeOfObject(method.ReturnType); if (pType == SerializeObjectType.Enum) { AddEnumAndNewModels(ref id, method.ReturnType, result, SerializeObjectType.Enum, xmlCommentLoader); } CommentOfMethodInfo methodComment = comment == null ? null : (from x in comment.Methods where x.Name == method.Name && x.Parameters.Count == method.GetParameters().Length select x).FirstOrDefault(); string exceptions = ""; if (methodComment?.Exceptions != null && methodComment?.Exceptions.Count > 0) { foreach (CommentOfExceptionInfo ex in methodComment.Exceptions) { try { if (ex.RefrenceType.LastIndexOf('.') != -1) { string baseNameOfEnum = ex.RefrenceType.Substring(0, ex.RefrenceType.LastIndexOf('.')); Type type = GetEnumType(baseNameOfEnum); #if (NETSTANDARD1_6 || NETCOREAPP1_1) if (type != null && type.GetTypeInfo().IsEnum) #else if (type != null && type.IsEnum) #endif { object value = Enum.Parse(type, ex.RefrenceType.Substring(ex.RefrenceType.LastIndexOf('.') + 1, ex.RefrenceType.Length - ex.RefrenceType.LastIndexOf('.') - 1)); int exNumber = (int)value; exceptions += ex.RefrenceType + $" ({exNumber}) : " + ex.Comment + TextHelper.NewLine; continue; } } } catch { } exceptions += ex.RefrenceType + ":" + ex.Comment + TextHelper.NewLine; } } id++; ServiceDetailsMethod info = new ServiceDetailsMethod() { MethodName = method.Name, #if (!NET35) Requests = new System.Collections.ObjectModel.ObservableCollection <ServiceDetailsRequestInfo>() { new ServiceDetailsRequestInfo() { Name = "Default", Parameters = new List <ServiceDetailsParameterInfo>(), IsSelected = true } }, #endif ReturnType = method.ReturnType.GetFriendlyName(), Comment = methodComment?.Summery, ReturnComment = methodComment?.Returns, ExceptionsComment = exceptions, Id = id }; RuntimeTypeHelper.GetListOfUsedTypes(method.ReturnType, ref modelTypes); foreach (System.Reflection.ParameterInfo paramInfo in method.GetParameters()) { pType = SerializeHelper.GetTypeCodeOfObject(paramInfo.ParameterType); if (pType == SerializeObjectType.Enum) { AddEnumAndNewModels(ref id, paramInfo.ParameterType, result, SerializeObjectType.Enum, xmlCommentLoader); } string parameterComment = ""; if (methodComment != null) { parameterComment = (from x in methodComment.Parameters where x.Name == paramInfo.Name select x.Comment).FirstOrDefault(); } id++; ServiceDetailsParameterInfo p = new ServiceDetailsParameterInfo() { Name = paramInfo.Name, Type = paramInfo.ParameterType.GetFriendlyName(), FullTypeName = paramInfo.ParameterType.FullName, Comment = parameterComment, Id = id }; #if (!NET35) info.Requests.First().Parameters.Add(p); #endif RuntimeTypeHelper.GetListOfUsedTypes(paramInfo.ParameterType, ref modelTypes); } serviceMethods.Add(info); } serviceDetail.Methods.AddRange(serviceMethods); } foreach (KeyValuePair <string, Type> httpServiceType in serverBase.RegisteredServiceTypes.Where(x => x.Value.IsHttpService())) { id++; HttpControllerDetailsInfo controller = new HttpControllerDetailsInfo() { Id = id, Url = httpServiceType.Value.GetCustomAttributes <ServiceContractAttribute>(true).Length > 0 ? httpServiceType.Value.GetCustomAttributes <ServiceContractAttribute>(true)[0].Name : httpServiceType.Key, }; id++; result.WebApiDetailsInfo.Id = id; result.WebApiDetailsInfo.HttpControllers.Add(controller); List <MethodInfo> methods = httpServiceType.Value.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).Where(x => !(x.IsSpecialName && (x.Name.StartsWith("set_") || x.Name.StartsWith("get_"))) && x.DeclaringType != typeof(object)).ToList(); if (methods.Count == 0) { continue; } CommentOfClassInfo comment = xmlCommentLoader.GetComment(httpServiceType.Value); List <ServiceDetailsMethod> serviceMethods = new List <ServiceDetailsMethod>(); foreach (MethodInfo method in methods) { SerializeObjectType pType = SerializeHelper.GetTypeCodeOfObject(method.ReturnType); if (pType == SerializeObjectType.Enum) { AddEnumAndNewModels(ref id, method.ReturnType, result, SerializeObjectType.Enum, xmlCommentLoader); } CommentOfMethodInfo methodComment = comment == null ? null : (from x in comment.Methods where x.Name == method.Name && x.Parameters.Count == method.GetParameters().Length select x).FirstOrDefault(); string exceptions = ""; if (methodComment?.Exceptions != null && methodComment?.Exceptions.Count > 0) { foreach (CommentOfExceptionInfo ex in methodComment.Exceptions) { try { if (ex.RefrenceType.LastIndexOf('.') != -1) { string baseNameOfEnum = ex.RefrenceType.Substring(0, ex.RefrenceType.LastIndexOf('.')); Type type = GetEnumType(baseNameOfEnum); #if (NETSTANDARD1_6 || NETCOREAPP1_1) if (type != null && type.GetTypeInfo().IsEnum) #else if (type != null && type.IsEnum) #endif { object value = Enum.Parse(type, ex.RefrenceType.Substring(ex.RefrenceType.LastIndexOf('.') + 1, ex.RefrenceType.Length - ex.RefrenceType.LastIndexOf('.') - 1)); int exNumber = (int)value; exceptions += ex.RefrenceType + $" ({exNumber}) : " + ex.Comment + TextHelper.NewLine; continue; } } } catch { } exceptions += ex.RefrenceType + ":" + ex.Comment + TextHelper.NewLine; } } id++; ServiceDetailsMethod info = new ServiceDetailsMethod() { Id = id, MethodName = method.Name, #if (!NET35) Requests = new System.Collections.ObjectModel.ObservableCollection <ServiceDetailsRequestInfo>() { new ServiceDetailsRequestInfo() { Name = "Default", Parameters = new List <ServiceDetailsParameterInfo>(), IsSelected = true } }, #endif ReturnType = method.ReturnType.GetFriendlyName(), Comment = methodComment?.Summery, ReturnComment = methodComment?.Returns, ExceptionsComment = exceptions, TestExample = hostUrl + "/" + controller.Url + "/" + method.Name }; RuntimeTypeHelper.GetListOfUsedTypes(method.ReturnType, ref modelTypes); string testExampleParams = ""; foreach (System.Reflection.ParameterInfo paramInfo in method.GetParameters()) { pType = SerializeHelper.GetTypeCodeOfObject(paramInfo.ParameterType); if (pType == SerializeObjectType.Enum) { AddEnumAndNewModels(ref id, paramInfo.ParameterType, result, SerializeObjectType.Enum, xmlCommentLoader); } string parameterComment = ""; if (methodComment != null) { parameterComment = (from x in methodComment.Parameters where x.Name == paramInfo.Name select x.Comment).FirstOrDefault(); } id++; ServiceDetailsParameterInfo p = new ServiceDetailsParameterInfo() { Id = id, Name = paramInfo.Name, Type = paramInfo.ParameterType.Name, FullTypeName = paramInfo.ParameterType.FullName, Comment = parameterComment }; #if (!NET35) info.Requests.First().Parameters.Add(p); #endif if (string.IsNullOrEmpty(testExampleParams)) { testExampleParams += "?"; } else { testExampleParams += "&"; } testExampleParams += paramInfo.Name + "=" + DataExchangeConverter.GetDefault(paramInfo.ParameterType) ?? "null"; RuntimeTypeHelper.GetListOfUsedTypes(paramInfo.ParameterType, ref modelTypes); } info.TestExample += testExampleParams; serviceMethods.Add(info); } controller.Methods = serviceMethods; } foreach (Type type in modelTypes) { try { SerializeObjectType pType = SerializeHelper.GetTypeCodeOfObject(type); AddEnumAndNewModels(ref id, type, result, pType, xmlCommentLoader); // var mode = SerializeHelper.GetTypeCodeOfObject(type); // if (mode == SerializeObjectType.Object) // { // if (type.Name.Contains("`") || type == typeof(CustomAttributeTypedArgument) || type == typeof(CustomAttributeNamedArgument) || //#if (NETSTANDARD1_6 || NETCOREAPP1_1) // type.GetTypeInfo().BaseType == typeof(Attribute)) //#else // type.BaseType == typeof(Attribute)) //#endif // continue; // var instance = Activator.CreateInstance(type); // string jsonResult = JsonConvert.SerializeObject(instance, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include }); // var refactorResult = (JObject)JsonConvert.DeserializeObject(jsonResult); // foreach (var item in refactorResult.Properties()) // { // var find = type.GetProperties().FirstOrDefault(x => x.Name == item.Name); // refactorResult[item.Name] = find.PropertyType.FullName; // } // jsonResult = JsonConvert.SerializeObject(refactorResult, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include }); // if (jsonResult == "{}" || jsonResult == "[]") // continue; // var comment = xmlCommentLoader.GetComment(type); // id++; // result.ProjectDomainDetailsInfo.Id = id; // id++; // result.ProjectDomainDetailsInfo.Models.Add(new ModelDetailsInfo() // { // Id = id, // Comment = comment?.Summery, // Name = type.Name, // FullNameSpace = type.FullName, // ObjectType = mode, // JsonTemplate = jsonResult // }); // foreach (var property in type.GetProperties()) // { // var pType = SerializeHelper.GetTypeCodeOfObject(property.PropertyType); // if (pType == SerializeObjectType.Enum) // { // AddEnumAndNewModels(ref id, property.PropertyType, result, SerializeObjectType.Enum, xmlCommentLoader); // } // } // } } catch (Exception ex) { serverBase.AutoLogger.LogError(ex, "Model Type Add error: " + ex.ToString()); } } return(result); //string json = ServerSerializationHelper.SerializeObject(result, serverBase); //List<byte> bytes = new List<byte> // { // (byte)DataType.GetServiceDetails, // (byte)CompressMode.None // }; //byte[] jsonBytes = Encoding.UTF8.GetBytes(json); //byte[] dataLen = BitConverter.GetBytes(jsonBytes.Length); //bytes.AddRange(dataLen); //bytes.AddRange(jsonBytes); //await client.StreamHelper.WriteToStreamAsync(client.ClientStream, bytes.ToArray()); } //} //catch (Exception ex) //{ // try // { // string json = ServerSerializationHelper.SerializeObject(new Exception(ex.ToString()), serverBase); // List<byte> bytes = new List<byte> // { // (byte)DataType.GetServiceDetails, // (byte)CompressMode.None // }; // byte[] jsonBytes = Encoding.UTF8.GetBytes(json); // byte[] dataLen = BitConverter.GetBytes(jsonBytes.Length); // bytes.AddRange(dataLen); // bytes.AddRange(jsonBytes); // await client.StreamHelper.WriteToStreamAsync(client.ClientStream, bytes.ToArray()); // } // catch (Exception) // { // } // serverBase.AutoLogger.LogError(ex, $"{client.IPAddress} {client.ClientId} ServerBase CallMethod"); //} //finally //{ // skippedTypes.Clear(); //} void AddEnumAndNewModels(ref int id, Type type, ProviderDetailsInfo result, SerializeObjectType objType, XmlCommentLoader xmlCommentLoader) { if (result.ProjectDomainDetailsInfo.Models.Any(x => x.FullNameSpace == type.FullName) || skippedTypes.Contains(type)) { return; } id++; result.ProjectDomainDetailsInfo.Id = id; id++; if (objType == SerializeObjectType.Enum) { List <string> items = new List <string>(); foreach (Enum obj in Enum.GetValues(type)) { int x = Convert.ToInt32(obj); // x is the integer value of enum items.Add(obj.ToString() + " = " + x); } result.ProjectDomainDetailsInfo.Models.Add(new ModelDetailsInfo() { Id = id, Name = type.Name, FullNameSpace = type.FullName, ObjectType = objType, JsonTemplate = JsonConvert.SerializeObject(items, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include }) }); } else { try { if (type.Name.Contains("`") || type == typeof(CustomAttributeTypedArgument) || type == typeof(CustomAttributeNamedArgument) || #if (NETSTANDARD1_6 || NETCOREAPP1_1) type.GetTypeInfo().BaseType == typeof(Attribute) || type.GetTypeInfo().BaseType == null) #else type.BaseType == typeof(Attribute) || type.BaseType == null) #endif { skippedTypes.Add(type); return; } object instance = Activator.CreateInstance(type); string jsonResult = JsonConvert.SerializeObject(instance, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include }); JObject refactorResult = (JObject)JsonConvert.DeserializeObject(jsonResult); foreach (JProperty item in refactorResult.Properties()) { PropertyInfo find = type.GetProperties().FirstOrDefault(x => x.Name == item.Name); refactorResult[item.Name] = find.PropertyType.GetFriendlyName(); } jsonResult = JsonConvert.SerializeObject(refactorResult, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include }); if (jsonResult == "{}" || jsonResult == "[]") { skippedTypes.Add(type); return; } CommentOfClassInfo comment = xmlCommentLoader.GetComment(type); id++; result.ProjectDomainDetailsInfo.Id = id; id++; result.ProjectDomainDetailsInfo.Models.Add(new ModelDetailsInfo() { Id = id, Comment = comment?.Summery, Name = type.Name, FullNameSpace = type.FullName, ObjectType = objType, JsonTemplate = jsonResult }); } catch { skippedTypes.Add(type); } } foreach (Type item in type.GetListOfGenericArguments()) { SerializeObjectType pType = SerializeHelper.GetTypeCodeOfObject(item); AddEnumAndNewModels(ref id, item, result, pType, xmlCommentLoader); } foreach (Type item in type.GetListOfInterfaces()) { SerializeObjectType pType = SerializeHelper.GetTypeCodeOfObject(item); AddEnumAndNewModels(ref id, item, result, pType, xmlCommentLoader); } foreach (Type item in type.GetListOfNestedTypes()) { SerializeObjectType pType = SerializeHelper.GetTypeCodeOfObject(item); AddEnumAndNewModels(ref id, item, result, pType, xmlCommentLoader); } foreach (Type item in type.GetListOfBaseTypes()) { SerializeObjectType pType = SerializeHelper.GetTypeCodeOfObject(item); AddEnumAndNewModels(ref id, item, result, pType, xmlCommentLoader); } foreach (PropertyInfo property in type.GetProperties()) { SerializeObjectType pType = SerializeHelper.GetTypeCodeOfObject(property.PropertyType); AddEnumAndNewModels(ref id, property.PropertyType, result, pType, xmlCommentLoader); } } }