/// <summary> /// 根据类型查找指定的创建对象的代理 /// </summary> /// <param name="type">类型</param> /// <returns></returns> public static CreateInstanceHandler GetCreateInstanceHandlerWithOutCache(Type type) { CreateInstanceHandler create = null; create = FastInvoke.GetInstanceCreator(type); return(create); }
/// <summary> /// 创建带参数的对象实例 /// </summary> /// <param name="objtype"></param> /// <param name="key"></param> /// <param name="ptypes"></param> private static void CreateHandler(Type objtype, string key, Type[] ptypes) { lock (typeof(ProxyFactory)) { if (!m_Handlers.ContainsKey(key)) { DynamicMethod method = new DynamicMethod(key, typeof(object), new Type[] { typeof(object[]) }, typeof(ProxyFactory).Module); ILGenerator iLGenerator = method.GetILGenerator(); ConstructorInfo constructor = objtype.GetConstructor(ptypes); iLGenerator.Emit(OpCodes.Nop); for (int i = 0; i < ptypes.Length; i++) { iLGenerator.Emit(OpCodes.Ldarg_0); iLGenerator.Emit(OpCodes.Ldc_I4, i); iLGenerator.Emit(OpCodes.Ldelem_Ref); if (ptypes[i].IsValueType) { iLGenerator.Emit(OpCodes.Unbox_Any, ptypes[i]); } else { iLGenerator.Emit(OpCodes.Castclass, ptypes[i]); } } iLGenerator.Emit(OpCodes.Newobj, constructor); iLGenerator.Emit(OpCodes.Ret); CreateInstanceHandler handler = (CreateInstanceHandler)method.CreateDelegate(typeof(CreateInstanceHandler)); m_Handlers.Add(key, handler); } } }
/// <summary> /// Get an instance of T /// </summary> /// <param name="onCreateInstance"></param> /// <returns></returns> public static T GetInstance(CreateInstanceHandler <T> onCreateInstance) { if (_Instance == null) { lock (LockKey) { if (_Instance == null) { try { if (onCreateInstance == null) { _Instance = Activator.CreateInstance <T>(); } else { _Instance = onCreateInstance(); } } catch { _Instance = default(T); } } } } return(_Instance); }
/// <summary> /// Get an instance of T and set to instance /// </summary> /// <param name="instance"></param> /// <param name="onCreateInstance"></param> /// <returns></returns> public static T GetInstance(object lockKey, ref T instance, CreateInstanceHandler <T> onCreateInstance) { if (instance == null) { if (lockKey == null) { lockKey = LockKey; } lock (lockKey) { if (instance == null) { try { if (onCreateInstance == null) { instance = Activator.CreateInstance <T>(); } else { instance = onCreateInstance(); } } catch { instance = default(T); } } } } return(instance); }
/// <summary> /// 类的信息 /// </summary> /// <param name="classType">类类型</param> /// <param name="createInstanceHandler">实例化类的句柄</param> /// <param name="propertyInfoHandles">属性集合</param> /// <param name="fieldInfoHandles">字段集合</param> internal ClassInfoHandle(Type classType, CreateInstanceHandler createInstanceHandler, Dictionary <string, PropertyInfoHandle> propertyInfoHandles, Dictionary <string, FieldInfoHandle> fieldInfoHandles) { this._classType = classType; this._createInstanceHandler = createInstanceHandler; this._propertyInfoHandles = new ClassPropertyInfoCollection(propertyInfoHandles); this._fieldInfoHandles = new ClassFieldInfoCollection(fieldInfoHandles); }
/// <summary> /// 类的信息 /// </summary> /// <param name="entityType">实体类型</param> /// <param name="createInstanceHandler">实例化类的句柄</param> /// <param name="propertyInfoHandles">属性集合</param> /// <param name="tableName">对应的表名</param> /// <param name="baseListInfo">此对象的查询缓存集合句柄</param> /// <param name="connectionKey">连接字符串的键</param> internal EntityInfoHandle(Type entityType, CreateInstanceHandler createInstanceHandler, TableAttribute tableInfo, DBInfo db) { this._entityType = entityType; this._createInstanceHandler = createInstanceHandler; this._tableInfo = tableInfo; //this.connectionKey = connectionKey; this._dbInfo = db; }
static void CreateHandler(Type objtype, string key, Type[] ptypes) { lock (typeof(CreateObjectFactory)) { if (!mHandlers.ContainsKey(key)) { DynamicMethod dm = new DynamicMethod(key, typeof(object), new Type[] { typeof(object[]) }, typeof(CreateObjectFactory).Module); ILGenerator il = dm.GetILGenerator(); ConstructorInfo cons = objtype.GetConstructor(ptypes); if (cons == null) { throw new MissingMethodException("The constructor for the corresponding parameter was not found"); } il.Emit(OpCodes.Nop); for (int i = 0; i < ptypes.Length; i++) { il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Ldelem_Ref); if (ptypes[i].IsValueType) { il.Emit(OpCodes.Unbox_Any, ptypes[i]); } else { il.Emit(OpCodes.Castclass, ptypes[i]); } } il.Emit(OpCodes.Newobj, cons); il.Emit(OpCodes.Ret); CreateInstanceHandler ci = (CreateInstanceHandler)dm.CreateDelegate(typeof(CreateInstanceHandler)); mHandlers.Add(key, ci); } } }
/// <summary> /// 获取缓存数据 /// </summary> /// <typeparam name="T">返回的数据类型</typeparam> /// <param name="keyword">缓存关键字</param> /// <param name="handler">生成缓存数据的委托</param> /// <returns></returns> public T GetInstance <T>(string keyword, CreateInstanceHandler <T> handler, int timeout) where T : class { keyword = preCacheKey + keyword; if (!cacheKeyList.Contains(keyword)) { cacheKeyList.Add(keyword); } return(We7Utils.GetCacheInstance <T>(keyword, handler, timeout)); }
/// <summary> /// Get an instance of T for current thread /// </summary> /// <param name="appendedkey"></param> /// <param name="onCreateInstance"></param> /// <returns></returns> public static T GetThreadInstance(string appendedkey, CreateInstanceHandler <T> onCreateInstance) { LocalDataStoreSlot slot = Thread.GetNamedDataSlot("__ThreadInstanceTable"); Dictionary <string, object> instances = Thread.GetData(slot) as Dictionary <string, object>; if (instances == null) { lock (LockKey) { instances = Thread.GetData(slot) as Dictionary <string, object>; if (instances == null) { instances = new Dictionary <string, object>(); Thread.SetData(slot, instances); } } } string key = string.Format("{0}:{1}", typeof(T).AssemblyQualifiedName, appendedkey); object obj; if (instances.TryGetValue(key, out obj)) { return((T)obj); } else { lock (LockKey) { if (instances.TryGetValue(key, out obj)) { return((T)obj); } else { T t; try { t = (onCreateInstance == null ? Activator.CreateInstance <T>() : onCreateInstance()); } catch { t = default(T); } instances[key] = t; return(t); } } } }
/// <summary> /// 生成实体类 /// </summary> /// <param name="type"></param> /// <returns></returns> public static CreateInstanceHandler GetInstanceCreator(Type type) { // generates a dynamic method to generate a FastCreateInstanceHandler delegate DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, type, new Type[0], typeof(FastInvoke).Module); ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); // generates code to create a new object of the specified type using the default constructor ilGenerator.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes)); // returns the value to the caller ilGenerator.Emit(OpCodes.Ret); // converts the DynamicMethod to a FastCreateInstanceHandler delegate to create the object CreateInstanceHandler creator = (CreateInstanceHandler)dynamicMethod.CreateDelegate(typeof(CreateInstanceHandler)); return(creator); }
/// <summary> /// 根据类型查找指定的创建对象的代理 /// </summary> /// <param name="type">类型</param> /// <returns></returns> public static CreateInstanceHandler GetCreateInstanceHandler(Type type) { CreateInstanceHandler create = null; string key = type.FullName; if (!_invokerInstance.TryGetValue(key, out create)) { using (Lock objLock = new Lock(_invokerInstance)) { create = GetCreateInstanceHandlerWithOutCache(type); _invokerInstance.Add(key, create); } } return(create); }
/// <summary> /// 初始化类型的属性信息 /// </summary> /// <param name="type">类型</param> /// <returns>如果已经初始化过侧返回false</returns> private static void InitClassPropertyInfos(Type type) { string fullName = type.FullName; //实例化本类型的句柄 CreateInstanceHandler createrHandel = FastValueGetSet.GetCreateInstanceHandlerWithOutCache(type); Dictionary <string, PropertyInfoHandle> dicPropertys = new Dictionary <string, PropertyInfoHandle>(); Dictionary <string, FieldInfoHandle> dicField = new Dictionary <string, FieldInfoHandle>(); //属性信息句柄 PropertyInfo[] destproper = type.GetProperties(FastValueGetSet.AllBindingFlags); FieldInfo[] allField = type.GetFields(FastValueGetSet.AllBindingFlags); //int index = 0; ///读取属性别名 foreach (PropertyInfo pinf in destproper) { ///通过属性来反射 string proName = pinf.Name; FastPropertyHandler getHandle = FastValueGetSet.GetGetMethodInfo(proName, type); FastPropertyHandler setHandle = FastValueGetSet.GetSetMethodInfo(proName, type); if (getHandle != null || setHandle != null) { PropertyInfoHandle classProperty = new PropertyInfoHandle(type, getHandle, setHandle, pinf.PropertyType, pinf.Name); dicPropertys.Add(pinf.Name, classProperty); } } ///读取属性别名 foreach (FieldInfo fInf in allField) { string proName = fInf.Name; GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(fInf); SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(fInf); if (getHandle != null || setHandle != null) { FieldInfoHandle fieldInfo = new FieldInfoHandle(type, getHandle, setHandle, fInf.FieldType, fInf.Name, fInf); dicField.Add(fInf.Name, fieldInfo); } } ClassInfoHandle classInfo = new ClassInfoHandle(type, createrHandel, dicPropertys, dicField); dicClass.Add(fullName, classInfo); }
/// <summary> /// 获取缓存数据 /// </summary> /// <typeparam name="T">返回的数据类型</typeparam> /// <param name="keyword">缓存关键字</param> /// <param name="handler">生成缓存数据的委托</param> /// <returns></returns> public T GetInstance <T>(string keyword, CreateInstanceHandler <T> handler) where T : class { keyword = preCacheKey + keyword; if (!cacheKeyList.Contains(keyword)) { cacheKeyList.Add(keyword); } if (defaultTimeout == -1) { return(handler != null?handler() : default(T)); } else if (defaultTimeout == 0) { return(We7Utils.GetCacheInstance <T>(keyword, handler)); } else { return(We7Utils.GetCacheInstance <T>(keyword, handler, defaultTimeout)); } }
/// <summary> /// 生成实体类 /// </summary> /// <param name="type"></param> /// <returns></returns> public static object CreateObject(Type type) { CreateInstanceHandler create = GetCreateInstanceHandler(type); return(create.Invoke()); }
/// <summary> /// 初始化类型的属性信息 /// </summary> /// <param name="type">类型</param> /// <returns>如果已经初始化过侧返回false</returns> private static EntityInfoHandle InitEntityPropertyInfos(Type type, Dictionary <string, EntityConfigInfo> dicConfigs) { if (type == null) { return(null); } string fullName = type.FullName; TableAttribute tableAtt = new TableAttribute(); CreateInstanceHandler createrHandle = null; //实例化本类型的句柄 if (!type.IsGenericType) { createrHandle = FastValueGetSet.GetCreateInstanceHandlerWithOutCache(type); } Dictionary <string, EntityPropertyInfo> dicPropertys = new Dictionary <string, EntityPropertyInfo>(); Dictionary <string, EntityMappingInfo> dicMapping = new Dictionary <string, EntityMappingInfo>(); Dictionary <string, EntityParam> dicParamsInfo = new Dictionary <string, EntityParam>(); Dictionary <string, TableRelationAttribute> dicRelationInfo = new Dictionary <string, TableRelationAttribute>(); FillEntityInfos(dicParamsInfo, dicRelationInfo, type, tableAtt, dicConfigs); DBInfo db = DataAccessLoader.GetDBInfo(tableAtt.BelongDB); IDBAdapter idb = db.CurrentDbAdapter; EntityInfoHandle classInfo = new EntityInfoHandle(type, createrHandle, tableAtt, db); Dictionary <string, bool> dicNotFoundParam = new Dictionary <string, bool>(); Dictionary <string, bool> dicNotFoundRelation = new Dictionary <string, bool>(); FillNotFoundField(dicParamsInfo, dicRelationInfo, dicNotFoundParam, dicNotFoundRelation); //属性信息句柄 List <FieldInfoHandle> lstFields = FieldInfoHandle.GetFieldInfos(type, FastValueGetSet.AllBindingFlags, true); DataBaseOperate oper = db.DefaultOperate; ///读取属性别名 foreach (FieldInfoHandle finf in lstFields) { ///通过属性来反射 EntityParam ep = null; if (dicParamsInfo.TryGetValue(finf.FieldName, out ep)) { //if (tableAtt.IsParamNameUpper) //{ // ep.ParamName = ep.ParamName.ToUpper(); //} string proName = ep.PropertyName; //GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(finf); //SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(finf); if (finf.HasGetHandle || finf.HasSetHandle) { PropertyInfo pinfo = type.GetProperty(ep.PropertyName, FastValueGetSet.AllBindingFlags); EntityPropertyInfo entityProperty = new EntityPropertyInfo( classInfo, finf.GetHandle, finf.SetHandle, ep, finf.FieldType, finf.FieldName, finf.BelongFieldInfo, pinfo); dicPropertys.Add(proName, entityProperty); dicNotFoundParam.Remove(finf.FieldName); } } else { TableRelationAttribute tableMappingAtt = null; if (dicRelationInfo.TryGetValue(finf.FieldName, out tableMappingAtt)) { Type targetType = DefaultType.GetRealValueType(finf.FieldType); tableMappingAtt.SetEntity(type, targetType); //GetFieldValueHandle getHandle = FastFieldGetSet.GetGetValueHandle(finf); //SetFieldValueHandle setHandle = FastFieldGetSet.GetSetValueHandle(finf); PropertyInfo pinfo = type.GetProperty(tableMappingAtt.PropertyName, FastValueGetSet.AllBindingFlags); EntityMappingInfo entityMappingInfo = new EntityMappingInfo( type, finf.GetHandle, finf.SetHandle, tableMappingAtt, finf.FieldName, finf.FieldType, finf.BelongFieldInfo, pinfo); dicMapping.Add(tableMappingAtt.PropertyName, entityMappingInfo); dicNotFoundRelation.Remove(finf.FieldName); } } } if (dicNotFoundParam.Count > 0 || dicNotFoundRelation.Count > 0) { StringBuilder message = new StringBuilder(); foreach (KeyValuePair <string, bool> kvp in dicNotFoundParam) { message.Append(kvp.Key + "、"); } foreach (KeyValuePair <string, bool> kvp in dicNotFoundRelation) { message.Append(kvp.Key + "、"); } if (message.Length > 0) { message.Remove(message.Length - 1, 1); } message.Insert(0, "类:" + type.FullName + " 找不到字段"); throw new MissingFieldException(message.ToString()); } classInfo.SetInfoHandles(dicPropertys, dicMapping); FillAttributeInfo(type, classInfo); _dicClass[fullName] = classInfo; return(classInfo); }
public TextNode() : base() { childCreateInstance += new CreateInstanceHandler(TextNodeCreateInstance); }
public ActionNodeData() : base() { childCreateInstance -= TextNodeCreateInstance; childCreateInstance += new CreateInstanceHandler(ActionNodeDataCreateInstance); }
/// <summary> /// 初始化代理类 /// </summary> internal void InitProxyType(EntityProxyBuilder proxyBuilder) { _proxyType = proxyBuilder.CreateProxyType(_entityType); _createProxyInstanceHandler = FastInvoke.GetInstanceCreator(_proxyType); }
public SceneData(string name, bool showInMenu, CreateInstanceHandler CreateInstanceEvent) { this.name = name; this.showInMenu = showInMenu; this.CreateInstanceEvent = CreateInstanceEvent; }