static bool IsCanSetCache(object listResult) { //return false; if (listResult == null) { return(false); } int i = 0; foreach (object o in listResult as IEnumerable) { if (i == 0)// && _AutoCache.CacheType != CacheType.LocalCache { List <PropertyInfo> pis = ReflectTool.GetPropertyList(o.GetType()); foreach (PropertyInfo item in pis) { if (DataType.GetGroup(DataType.GetSqlType(item.PropertyType)) == DataGroupType.Object)//只存档基础类型 { return(false); } } } i++; if (i > 100)// 大于N条的不缓存。List<T> 的缓存,在存和取间都要转2次( JsonString=>MDataTable=>List<T>),所以限制条数少一些,性能调节上相对合适。 { return(false); } } return(true); }
private bool GetInvokeParas(MethodInfo method, out object[] paras) { paras = null; #region 增加处理参数支持 ParameterInfo[] piList = method.GetParameters(); object[] validateList = method.GetCustomAttributes(typeof(ValidateAttribute), true); if (piList != null && piList.Length > 0) { paras = new object[piList.Length]; for (int i = 0; i < piList.Length; i++) { ParameterInfo pi = piList[i]; Type t = pi.ParameterType; string value = Query <string>(pi.Name, null); if (value == null) { if (t.IsValueType && t.IsGenericType && t.FullName.StartsWith("System.Nullable")) { continue; } if (ReflectTool.GetSystemType(ref t) != SysType.Base)//基础值类型 { value = GetJson(); } } //检测是否允许为空,是否满足正则格式。 if (!ValidateParas(validateList, pi.Name, value)) { return(false); } try { paras[i] = QueryTool.ChangeType(value, t);//类型转换(基础或实体) } catch (Exception err) { string typeName = t.Name; if (typeName.StartsWith("Nullable")) { typeName = Nullable.GetUnderlyingType(t).Name; } string outMsg = string.Format("[{0} {1} = {2}] [Error : {3}]", typeName, pi.Name, value, err.Message); Write(outMsg, false); return(false); } } } #endregion return(true); }
static bool GetExitsModule(string name) { Type[] types = ReflectTool.GetTypes(); for (int i = 0; i < types.Length; i++) { if (typeof(CsharpProtocolInterface).IsAssignableFrom(types[i]) && GetModuleName(types[i], false) == name ) { return(true); } } return(false); }
static string GetModuleByName(string name) { Type[] types = ReflectTool.GetTypes(); for (int i = 0; i < types.Length; i++) { if (typeof(CsharpProtocolInterface).IsAssignableFrom(types[i]) && GetModuleName(types[i], false) == name ) { return(types[i].FullName); } } throw new Exception("GetModuleByName Fail ! " + name); }
static Type GetAimStructType(string name) { Type[] types = ReflectTool.GetTypes(); for (int i = 0; i < types.Length; i++) { if (typeof(IProtocolStructInterface).IsAssignableFrom(types[i]) && types[i].Name.ToLower() == name.ToLower() ) { return(types[i]); } } return(null); }
internal static string GetTableName(Type t, out string conn) { conn = string.Empty; string[] items = t.FullName.Split('.'); if (items.Length > 1) { conn = items[items.Length - 2] + "Conn"; if (string.IsNullOrEmpty(AppConfig.GetConn(conn))) { conn = null; } items = null; } string tName = t.Name; object[] names = ReflectTool.GetAttributes(t, typeof(TableNameAttribute)); if (names != null && names.Length > 0) { foreach (object item in names) { if (item is TableNameAttribute) { tName = ((TableNameAttribute)item).TableName; break; } } } if (!string.IsNullOrEmpty(tName)) { t = null; if (tName.EndsWith(AppConfig.EntitySuffix)) { tName = tName.Substring(0, tName.Length - AppConfig.EntitySuffix.Length); } } string fixName; conn = CrossDB.GetConn(tName, out fixName, conn); if (!string.IsNullOrEmpty(fixName)) { tName = fixName; } return(tName); }
private object[] GetInvokeParas(MethodInfo method) { object[] paras = null; #region 增加处理参数支持 ParameterInfo[] piList = method.GetParameters(); if (piList != null && piList.Length > 0) { paras = new object[piList.Length]; for (int i = 0; i < piList.Length; i++) { ParameterInfo pi = piList[i]; Type t = pi.ParameterType; string value = Query <string>(pi.Name, null); if (value == null) { if (t.IsValueType && t.IsGenericType && t.FullName.StartsWith("System.Nullable")) { continue; } if (ReflectTool.GetSystemType(ref t) != SysType.Base)//基础值类型 { value = GetJson(); } } try { paras[i] = QueryTool.ChangeType(value, t);//类型转换(基础或实体) } catch (Exception err) { string typeName = t.Name; if (typeName.StartsWith("Nullable")) { typeName = Nullable.GetUnderlyingType(t).Name; } string outMsg = string.Format("[{0} {1} = {2}] [Error : {3}]", typeName, pi.Name, value, err.Message); Write(outMsg, false); // context.Response.Write(outMsg); context.Response.End(); break; } } } #endregion return(paras); }
public PluginGroup Use(Type importType) { if (!importers.ContainsKey(importType)) { importers.Add(importType, null); var pluginImporter = ReflectTool.Instantiate(importType) as IPlugin; //引用导入插件的依赖插件 foreach (var dependency in pluginImporter.Dependencies) { this.Use(dependency); } importers[importType] = pluginImporter; importerList.Add(pluginImporter); } return(this); }
static void GenerateList() { msgList.Clear(); Type[] types = ReflectTool.GetTypes(); for (int i = 0; i < types.Length; i++) { if (typeof(IProtocolMessageInterface).IsAssignableFrom(types[i]) && types[i] != typeof(IProtocolMessageInterface) && types[i] != typeof(CsharpProtocolInterface) && !types[i].IsAbstract ) { msgList.Add(types[i]); } if (typeof(IProtocolMessageInterface).IsAssignableFrom(types[i]) && types[i].IsAbstract ) { ModuleList.Add(types[i]); } } //进行排序 msgList.Sort(sort); StructList.Clear(); for (int i = 0; i < types.Length; i++) { if (typeof(IProtocolStructInterface).IsAssignableFrom(types[i]) && types[i] != typeof(IProtocolStructInterface) ) { StructList.Add(types[i]); } } }
public static void Generate() { Type[] allTypes = ReflectTool.GetChildTypes(typeof(ComponentBase)); List <Type> userTypes = new List <Type>(); foreach (var item in allTypes) { if (item == typeof(RecordComponent <>)) { continue; } if (item == typeof(SingletonComponent)) { continue; } if (item == typeof(MomentComponentBase)) { continue; } if (item == typeof(MomentSingletonComponent)) { continue; } if (item == typeof(ViewComponent)) { continue; } userTypes.Add(item); } string code = CreateCode(0, userTypes.ToArray()); string savePath = ReflectTool.ProjectPath + "/Generate/ComponentType.cs"; FileTool.WriteStringByFile(savePath, code); Debug.Log(savePath); Debug.Log(code); }
/// <summary> /// 序列化 /// </summary> /// <param name="value">值</param> /// <param name="type">返回序列化类型</param> /// <param name="compressionThreshold">指定超过长度时启用压缩功能</param> /// <returns></returns> public static byte[] Serialize(object value, out SerializedType type, uint compressionThreshold) { byte[] bytes; if (value is byte[]) { bytes = (byte[])value; type = SerializedType.ByteArray; if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedByteArray; } } else if (value is string) { bytes = Encoding.UTF8.GetBytes((string)value); type = SerializedType.String; if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedString; } } else if (value is DateTime) { bytes = BitConverter.GetBytes(((DateTime)value).Ticks); type = SerializedType.Datetime; } else if (value is bool) { bytes = new byte[] { (byte)((bool)value ? 1 : 0) }; type = SerializedType.Bool; } else if (value is byte) { bytes = new byte[] { (byte)value }; type = SerializedType.Byte; } else if (value is short) { bytes = BitConverter.GetBytes((short)value); type = SerializedType.Short; } else if (value is ushort) { bytes = BitConverter.GetBytes((ushort)value); type = SerializedType.UShort; } else if (value is int) { bytes = BitConverter.GetBytes((int)value); type = SerializedType.Int; } else if (value is uint) { bytes = BitConverter.GetBytes((uint)value); type = SerializedType.UInt; } else if (value is long) { bytes = BitConverter.GetBytes((long)value); type = SerializedType.Long; } else if (value is ulong) { bytes = BitConverter.GetBytes((ulong)value); type = SerializedType.ULong; } else if (value is float) { bytes = BitConverter.GetBytes((float)value); type = SerializedType.Float; } else if (value is double) { bytes = BitConverter.GetBytes((double)value); type = SerializedType.Double; } else { type = SerializedType.ObjectJson; Type t = value.GetType(); switch (ReflectTool.GetSystemType(ref t)) { case SysType.Base: case SysType.Enum: case SysType.Custom: if (t.GetCustomAttributes(typeof(SerializableAttribute), false).Length > 0) { type = SerializedType.Object; } break; } if (type == SerializedType.Object) { //可序列化 List<object> 如果 object 不支持序列化,会挂。 using (MemoryStream ms = new MemoryStream()) { new BinaryFormatter().Serialize(ms, value); bytes = ms.ToArray(); if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedObject; } } } else { string json = JsonHelper.ToJson(value); bytes = Encoding.UTF8.GetBytes(json); if (bytes.Length > compressionThreshold) { bytes = compress(bytes); type = SerializedType.CompressedObjectJson; } } } return(bytes); }
private bool GetInvokeParas(MethodInfo method, out object[] paras) { paras = null; #region 增加处理参数支持 ParameterInfo[] piList = method.GetParameters(); object[] validateList = method.GetCustomAttributes(typeof(RequireAttribute), true); if (piList != null && piList.Length > 0) { paras = new object[piList.Length]; for (int i = 0; i < piList.Length; i++) { ParameterInfo pi = piList[i]; Type t = pi.ParameterType; object value = Query <object>(pi.Name, null); if (value == null) { if (t.IsValueType && t.IsGenericType && t.FullName.StartsWith("System.Nullable")) { continue; } if (t.Name == "HttpPostedFile") { if (Request.Files != null && Request.Files.Count == 1) { value = Request.Files[0]; } } else if (piList.Length == 1 && ReflectTool.GetSystemType(ref t) != SysType.Base)//基础值类型 { value = GetJson(); } } //检测是否允许为空,是否满足正则格式。 if (!ValidateParas(validateList, pi.Name, Convert.ToString(value))) { return(false); } try { //特殊值处理 if (t.Name == "HttpPostedFile" && value is string && Convert.ToString(value) == DocSettings.DocDefaultImg.ToLower()) { string path = DocSettings.DefaultImg; if (!string.IsNullOrEmpty(path)) { paras[i] = HttpPostedFileExtend.Create(path); } } else { paras[i] = QueryTool.ChangeType(value, t);//类型转换(基础或实体) } } catch (Exception err) { string typeName = t.Name; if (typeName.StartsWith("Nullable")) { typeName = Nullable.GetUnderlyingType(t).Name; } string outMsg = string.Format("[{0} {1} = {2}] [Error : {3}]", typeName, pi.Name, value, err.Message); Write(outMsg, false); return(false); } } } //对未验证过的参数,再进行一次验证。 foreach (object item in validateList) { RequireAttribute valid = item as RequireAttribute; if (!valid.isValidated) { if (valid.paraName.IndexOf(',') > -1) { foreach (string name in valid.paraName.Split(',')) { if (string.IsNullOrEmpty(Query <string>(name))) { Write(string.Format(valid.emptyTip, name), false); return(false); } } } else if (!ValidateParas(validateList, valid.paraName, Query <string>(valid.paraName))) { return(false); } } } validateList = null; #endregion return(true); }
static string GeneratePrototalList() { string content = ""; int index = 1; List <Type> ModuleList = new List <Type>(); Type[] types = ReflectTool.GetTypes(); for (int i = 0; i < types.Length; i++) { if (typeof(IProtocolMessageInterface).IsAssignableFrom(types[i]) && types[i] != typeof(IProtocolMessageInterface) && types[i] != typeof(CsharpProtocolInterface) && types[i].IsAbstract && GetIsModule(types[i]) ) { ModuleList.Add(types[i]); } } ModuleList.Sort(sort); //无模块消息 { int count = 0; List <string> nameList = new List <string>(); for (int i = 0; i < msgList.Count; i++) { if (GetModuleName(msgList[i], true) == null) { string nameTmp = GenerateProtocolName(msgList[i]); if (!nameList.Contains(nameTmp)) { nameList.Add(nameTmp); } } } for (int i = 0; i < nameList.Count; i++) { content += i + "," + nameList[i] + "\n"; count++; if (count >= c_modMaxMessageCount) { throw new Exception("无模块下的消息超过了 " + c_modMaxMessageCount + " !"); } } } for (int i = 0; i < ModuleList.Count; i++) { int count = 0; List <string> nameList = new List <string>(); if (i != 0) { content += "\n"; } //生成模块头 object[] objs = ModuleList[i].GetCustomAttributes(typeof(ModuleAttribute), true); if (objs.Length == 0) { throw new Exception(ModuleList[i] + " 必须要有 Module 特性 !"); } ModuleAttribute mod = (ModuleAttribute)objs[0]; index = mod.MessageCode; content += index + "," + mod.ModuleName + "\n"; index = index * c_modMaxMessageCount + 1; for (int j = 0; j < msgList.Count; j++) { if (msgList[j].IsSubclassOf(ModuleList[i])) { string nameTmp = GenerateProtocolName(msgList[j]); if (!nameList.Contains(nameTmp)) { nameList.Add(nameTmp); } } } for (int j = 0; j < nameList.Count; j++) { content += index++ + "," + nameList[j] + "\n"; count++; if (count >= c_modMaxMessageCount) { throw new Exception(mod.ModuleName + "模块下的消息超过了 " + c_modMaxMessageCount + " !"); } } } return(content); }