/// <summary> /// 获取服务信息列表 /// </summary> /// <returns></returns> public IList<ServiceInfo> GetServiceList() { if (services == null || services.Count == 0) { //获取拥有ServiceContract约束的服务 var types = container.GetServiceTypes<ServiceContractAttribute>(); services = new List<ServiceInfo>(); foreach (var type in types) { //状态服务跳过 if (type == typeof(IStatusService)) continue; var contract1 = CoreHelper.GetMemberAttribute<ServiceContractAttribute>(type); var s = new ServiceInfo { Assembly = type.Assembly.FullName, Name = type.Name, FullName = type.FullName }; if (contract1 != null) { s.ServiceName = contract1.Name; s.ServiceDescription = contract1.Description; } //读取方法 foreach (var method in CoreHelper.GetMethodsFromType(type)) { var contract2 = CoreHelper.GetMemberAttribute<OperationContractAttribute>(type); var m = new MethodInfo { Name = method.Name, FullName = method.ToString() }; if (contract2 != null) { m.MethodName = contract2.Name; m.MethodDescription = contract2.Description; } //读取参数 foreach (var p in method.GetParameters()) { var pi = new ParameterInfo { Name = p.Name, TypeName = CoreHelper.GetTypeName(p.ParameterType), TypeFullName = p.ParameterType.FullName, IsByRef = p.ParameterType.IsByRef, IsOut = p.IsOut, IsEnum = p.ParameterType.IsEnum, IsPrimitive = CheckPrimitive(p.ParameterType), SubParameters = GetSubParameters(p.ParameterType) }; if (pi.IsEnum) { pi.EnumValue = GetEnumValue(p.ParameterType); } m.Parameters.Add(pi); } s.Methods.Add(m); } services.Add(s); } } return services; }
private IList<ParameterInfo> GetSubParameters(Type type) { type = CoreHelper.GetPrimitiveType(type); if (GetTypeClass(type) && !typeof(ICollection).IsAssignableFrom(type)) { var plist = new List<ParameterInfo>(); foreach (var p in CoreHelper.GetPropertiesFromType(type)) { var pi = new ParameterInfo { Name = p.Name, TypeName = CoreHelper.GetTypeName(p.PropertyType), TypeFullName = p.PropertyType.FullName, IsByRef = p.PropertyType.IsByRef, IsOut = false, IsEnum = p.PropertyType.IsEnum, IsPrimitive = CheckPrimitive(p.PropertyType) }; if (p.PropertyType != type) pi.SubParameters = GetSubParameters(p.PropertyType); if (pi.IsEnum) { pi.EnumValue = GetEnumValue(p.PropertyType); } plist.Add(pi); } return plist; } else { return new List<ParameterInfo>(); } }
private string GetParameterText(ParameterInfo parameter, int index) { StringBuilder sb = new StringBuilder(); for (var i = 0; i < index * 4; i++) sb.Append(" "); sb.AppendLine("【" + parameter.Name + "】 => " + parameter.TypeName); if (parameter.IsEnum) { for (var i = 0; i < index * 4 + 1; i++) sb.Append(" "); sb.AppendLine("{"); foreach (var p in parameter.EnumValue) { for (var i = 0; i < (index + 1) * 4; i++) sb.Append(" "); sb.AppendLine("【" + p.Name + "】 => " + p.Value); } for (var i = 0; i < index * 4 + 1; i++) sb.Append(" "); sb.AppendLine("}"); } else if (parameter.SubParameters.Count > 0) { for (var i = 0; i < index * 4 + 1; i++) sb.Append(" "); sb.AppendLine("{"); { foreach (var p in parameter.SubParameters) { if (p.IsEnum || p.SubParameters.Count > 0) sb.Append(GetParameterText(p, index + 1)); else { for (var i = 0; i < (index + 1) * 4; i++) sb.Append(" "); sb.AppendLine("【" + p.Name + "】 => " + p.TypeName); } } } for (var i = 0; i < index * 4 + 1; i++) sb.Append(" "); sb.AppendLine("}"); } return sb.ToString(); }