Beispiel #1
0
        private static void SaveAssembly(AssemblyBuilderHelper assemblyBuilder, Type type)
        {
            if (_globalAssembly != null)
            {
                return;
            }

            if (_saveTypes)
            {
                try
                {
                    assemblyBuilder.Save();

                    WriteDebug("The '{0}' type saved in '{1}'.",
                               type.FullName,
                               assemblyBuilder.Path);
                }
                catch (Exception ex)
                {
                    WriteDebug("Can't save the '{0}' assembly for the '{1}' type: {2}.",
                               assemblyBuilder.Path,
                               type.FullName,
                               ex.Message);
                }
            }
        }
        public static Type CreateSerializableType(Type originalType)
        {
            var originalAsmName       = new AssemblyName(originalType.FullName + "Proxy");
            var assemblyBuilderHelper = new AssemblyBuilderHelper(originalAsmName.Name + ".dll");
            var returnType            = CreateType(originalType, assemblyBuilderHelper);

#if DEBUG
            assemblyBuilderHelper.Save();
#endif
            return(returnType);
        }
Beispiel #3
0
        public static void SaveGlobalAssembly()
        {
            if (_globalAssembly != null)
            {
                _globalAssembly.Save();

                WriteDebug("The global assembly saved in '{0}'.", _globalAssembly.Path);

                _globalAssembly        = null;
                _globalAssemblyPath    = null;
                _globalAssemblyVersion = null;
                _globalAssemblyKeyFile = null;
            }
        }
        public static Assembly CreateSerializableAssembly(Assembly originalAssembly)
        {
            var originalAsmName       = originalAssembly.GetName();
            var assemblyBuilderHelper = new AssemblyBuilderHelper(originalAsmName.Name + "Proxy.dll");
            var types = originalAssembly.GetTypes();

            foreach (var type in types)
            {
                CreateType(type, assemblyBuilderHelper);
            }

#if DEBUG
            assemblyBuilderHelper.Save();
#endif
            return(assemblyBuilderHelper);
        }
 static void SaveAssembly(AssemblyBuilderHelper assemblyBuilder, Type type)
 {
     if (!SaveTypes)
     {
         return;
     }
     try
     {
         assemblyBuilder.Save();
         WriteDebug($"The '{type.FullName}' type saved in '{assemblyBuilder.Path}'.");
     }
     catch (Exception ex)
     {
         WriteDebug($"Can't save the '{assemblyBuilder.Path}' assembly for the '{type.FullName}' type: {ex.Message}.");
     }
 }
Beispiel #6
0
        public Materializer <T> CreateMaterializer <T>()
        {
            var baseType       = typeof(Materializer <T>);
            var localType      = TypeCache.Get <T>();
            var emptyArgsArray = new Type[] {};

            var typeBuilderHelper = AssemblyBuilder.DefineType(localType.Type.Name + "Materializer", baseType);

            //constructor
            typeBuilderHelper.DefaultConstructor.Emitter.ldarg_0.call(baseType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, emptyArgsArray, null)).ret();

            //getobject impl
            var defineMethod = typeBuilderHelper.DefineMethod(baseType.GetMethod("GetObject"));

            var emitter   = defineMethod.Emitter;
            var returnVar = emitter.DeclareLocal(localType.Type);

            //create new T
            emitter
            .newobj(localType.Type.GetConstructor(emptyArgsArray))
            .stloc(returnVar);

            foreach (var propertyInfo in localType.Properties)
            {
                emitter =
                    emitter
                    .ldloc_0
                    .ldarg_0
                    .ldarg_1
                    .ldstr(propertyInfo.Name)
                    .ldloc_0
                    .callvirt(propertyInfo.GetGetMethod())
                    .call(baseType.GetMethod("GetValue").MakeGenericMethod(propertyInfo.PropertyType))
                    .callvirt(propertyInfo.GetSetMethod())
                    .nop;
            }

            emitter
            .ldloc_0
            .ret();

            var type = typeBuilderHelper.Create();

            AssemblyBuilder.Save();

            return((Materializer <T>)Activator.CreateInstance(type));
        }
Beispiel #7
0
        public void RegisterService(object service, bool isOverride)
        {
            var originalAsmName       = new AssemblyName("NTSock" + service.GetType().FullName);
            var assemblyBuilderHelper = new AssemblyBuilderHelper(originalAsmName.Name + ".dll");

            servicesLock.EnterWriteLock();
            try
            {
                var serviceType = service.GetType();
                var methods     = serviceType.GetMethods();
                if (methods != null)
                {
                    for (var i = 0; i < methods.Length; i++)
                    {
                        var method = methods[i];
                        if (method.GetCustomAttributes(typeof(ServiceMethodAttribute), false).Length == 0)
                        {
                            continue;
                        }
                        var executor = ExecutorFactory.CreateExecutor(service, method, i, assemblyBuilderHelper);
                        if (methodMaps.ContainsKey(executor.ExecutorKey))
                        {
                            if (!isOverride)
                            {
                                throw new ArgumentException("Cannot override an existing service.");
                            }
                            methodMaps.Remove(executor.ExecutorKey);
                        }
                        methodMaps.Add(executor.ExecutorKey, executor);
                    }
                    ExecutorFactory.CreateProxy(service, assemblyBuilderHelper);
                }
#if DEBUG
                assemblyBuilderHelper.Save();
#endif
            }
            finally
            {
                servicesLock.ExitWriteLock();
            }
        }
Beispiel #8
0
        public void Register(Assembly assembly)
        {
            var types            = assembly.GetTypes();
            var asmBuilderHelper = new AssemblyBuilderHelper("Db" + assembly.GetName().Name + ".dll");
            var dict             = new Dictionary <ClassMetaData, bool>();

            foreach (var type in types)
            {
                var result = ClassMetaDataManager.Instace.GetClassMetaData(type);

                if (result != null && !dict.ContainsKey(result))
                {
                    dict.Add(result, true);
                    tableExecutor.CreateTable(result);
                }
            }
            tableExecutor.CreateRelation(dict.Keys);
#if DEBUG
            asmBuilderHelper.Save();
#endif
        }
Beispiel #9
0
        public static IExecutor CreateExecutor(object service, MethodInfo method)
        {
            if (service == null)
            {
                throw new ArgumentNullException("Cannot create a null executor");
            }
            var type     = service.GetType();
            var typeName = type.Name + method.Name;
            var assemblyBuilderHelper = new AssemblyBuilderHelper(typeName + "_Executor" + ".dll");
            var typeBuilderHelper     = assemblyBuilderHelper.DefineType(typeName, typeof(object),
                                                                         typeof(IExecutor));
            var serviceFieldBuilder = typeBuilderHelper.DefineField(type.Name, type, FieldAttributes.Public);
            var methods             = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
            var emit       = typeBuilderHelper.DefineMethod(typeof(IExecutor).GetMethod("Execute")).Emitter;
            var returnType = method.ReturnType;
            var paramInfos = method.GetParameters();

            emit
            .ldarg_0
            .ldfld(serviceFieldBuilder);
            for (var j = 0; j < paramInfos.Length; j++)
            {
                var paramInfo = paramInfos[j];
                var paramType = paramInfo.ParameterType;
                emit = emit
                       .ldarg_1
                       .ldc_i4(j)
                       .ldelem_ref;
                if (paramType.IsValueType)
                {
                    emit
                    .unbox_any(paramType);
                }
                else if (paramType.IsClass || paramType.IsInterface)
                {
                    emit.castclass(paramType);
                }
            }
            emit.call(method);
            if (returnType != typeof(void))
            {
                emit
                .boxIfValueType(returnType)
                .ret();
            }
            else
            {
                emit
                .ldnull
                .ret();
            }
            var executorType = typeBuilderHelper.Create();
            var executor     = (IExecutor)TypeAccessor.CreateInstance(executorType);
            var serviceField = executorType.GetField(type.Name);

            serviceField.SetValue(executor, service);
#if DEBUG
            assemblyBuilderHelper.Save();
#endif
            return(executor);
        }