示例#1
0
        public void ExposeInterface <TInterface>()
            where TInterface : class
        {
            var methods = new MethodDictionary();

            methods.AddInterface <TInterface>();

            _methodDictionaries.Add(methods.GetServiceHash(), methods);
            _exposedInterfaces.Add(typeof(TInterface));
        }
示例#2
0
        public RemoteService(IRemoteProcedureCaller remoteProcedureCaller, IRemoteProcedureSerializer remoteProcedureSerializer, MethodDictionary methodDictionary)
        {
            _remoteProcedureSerializer = remoteProcedureSerializer;
            _methodDictionary          = methodDictionary;

            _remoteSessionInformation = new RemoteSessionInformation
            {
                InstanceId = InstanceId,
                ScopeId    = Guid.Empty,
                ActionId   = Guid.Empty
            };

            _remoteProcedureCaller = remoteProcedureCaller;
        }
        static StaticInterfaceDescriptor()
        {
            MethodDictionary = new MethodDictionary();
            MethodDictionary.AddInterface <TInterface>();

            var iface     = typeof(TInterface);
            var ifaceName = iface.Name;
            var implName  = ifaceName.Substring(1);
            var baseType  = typeof(RemoteService <TInterface>);

            // Create the hosting assembly
            var assemblyName    = new AssemblyName("Implementation." + iface.Name);
            var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

            // Create module
            var moduleBuilder = assemblyBuilder.DefineDynamicModule("Main");

            // Create the implementation
            var typeBuilder = moduleBuilder
                              .DefineType("Remote" + implName + "Service", 0
                                          | TypeAttributes.Public
                                          | TypeAttributes.Class
                                          | TypeAttributes.AutoLayout
                                          | TypeAttributes.Sealed
                                          , baseType
                                          , new Type[] { iface });

            // Create the constructor
            var constructorBuilder = typeBuilder.DefineConstructor(
                MethodAttributes.Public | MethodAttributes.HideBySig,
                CallingConventions.Standard,
                new Type[] { typeof(IRemoteProcedureCaller), typeof(IRemoteProcedureSerializer), typeof(MethodDictionary) }
                );

            constructorBuilder.DefineParameter(1, ParameterAttributes.None, "remoteProcedureCaller");
            constructorBuilder.DefineParameter(2, ParameterAttributes.None, "remoteProcedureSerializer");
            constructorBuilder.DefineParameter(3, ParameterAttributes.None, "remoteProcedureDescriptor");

            var baseConstructor = baseType.GetConstructor(
                BindingFlags.Public | BindingFlags.Instance, null,
                new Type[] { typeof(IRemoteProcedureCaller), typeof(IRemoteProcedureSerializer), typeof(MethodDictionary) }, null
                );

            // Tell our constructor to call the RemoteService<TInterface> constructor
            var constructor = constructorBuilder.GetILGenerator();

            constructor.Emit(OpCodes.Ldarg_0);
            constructor.Emit(OpCodes.Ldarg_1);
            constructor.Emit(OpCodes.Ldarg_2);
            constructor.Emit(OpCodes.Ldarg_3);
            constructor.Emit(OpCodes.Call, baseConstructor);
            constructor.Emit(OpCodes.Nop);
            constructor.Emit(OpCodes.Nop);
            constructor.Emit(OpCodes.Ret);


            var methods = iface.GetAllMethods(BindingFlags.Public | BindingFlags.Instance);

            // Implement the functions of the interface
            foreach (var method in methods)
            {
                AddMethodCall(typeBuilder, method);
            }

            CachedType = typeBuilder.CreateType();
        }