Exemplo n.º 1
0
        public static object CreateInstanceObject(Type typeObj)
        {
            if (typeObj == null)
            {
                return(null);
            }
            try
            {
                InstantiateObjectHandler instantiateObjectHandler = null;
                lstInitHandler.TryGetValue(typeObj.FullName, out instantiateObjectHandler);
                if (instantiateObjectHandler == null)
                {
                    instantiateObjectHandler = ABCDynamicMethodCompiler.CreateInstantiateObjectHandler(typeObj);
                    lstInitHandler.Add(typeObj.FullName, instantiateObjectHandler);
                }

                return(instantiateObjectHandler());
            }
            catch (System.Exception ex)
            {
                try
                {
                    return(typeObj.InvokeMember("", BindingFlags.CreateInstance, null, null, null));
                }
                catch (System.Exception ex2)
                {
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        public static InstantiateObjectHandler CreateInstantiateObjectHandler(Type type)
        {
            lock (AppInfo.EntityDictionary)
            {
                if (AppInfo.EntityDictionary.ContainsKey(type))
                {
                    return(AppInfo.EntityDictionary[type]);
                }
                else
                {
                    ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);

                    if (constructorInfo == null)
                    {
                        throw new ApplicationException(string.Format("The type {0} must declare an empty constructor (the constructor may be private, internal, protected, protected internal, or public).", type));
                    }

                    DynamicMethod dynamicMethod = new DynamicMethod("InstantiateObject", MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, typeof(object), null, type, true);
                    ILGenerator   generator     = dynamicMethod.GetILGenerator();
                    generator.Emit(OpCodes.Newobj, constructorInfo);
                    generator.Emit(OpCodes.Ret);

                    InstantiateObjectHandler instantiateObjectHandler = (InstantiateObjectHandler)dynamicMethod.CreateDelegate(typeof(InstantiateObjectHandler));
                    AppInfo.EntityDictionary.Add(type, instantiateObjectHandler);
                    return(instantiateObjectHandler);
                }
            }
        }
Exemplo n.º 3
0
            public static object CreateInstance(Type type)
            {
                InstantiateObjectHandler instantiateObjectHandler =
                    DynamicMethodCompiler.CreateInstantiateObjectHandler(type);

                return(instantiateObjectHandler());
            }