Esempio n. 1
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();
			}
		}
Esempio n. 2
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;
        }
Esempio n. 3
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);
				}
			}
		}