public object GetInstance(ServiceChannel channel, InvokeMethodDelegate invokeMethod, ServiceUser user)
        {
            if (channel.Provider.Mode == ServiceMode.Single)
            {
                return(GetInstance(channel, invokeMethod));
            }
            if (UserInstances[user].ContainsKey(channel.Provider))
            {
                return(UserInstances[user][channel.Provider]);
            }
            Type type;

            try
            {
                type = Builder.GetInstanceType(channel.Provider.Instance, channel.Provider.Contract);
            }
            catch
            {
                return(null);
            }
            object instance = CreateInstance(channel, invokeMethod, type);

            if (channel.Provider.Mode == ServiceMode.PreClient)
            {
                UserInstances[user].Add(channel.Provider, instance);
            }
            return(instance);
        }
        /// <summary>
        /// Builds method delegates
        /// </summary>
        protected virtual void SetupMethod()
        {
            lock (this)
            {
                if (ActivationFunc == null)
                {
                    AssignActivationFunc();

                    BindParametersDelegate =
                        Services.ParameterBinderDelegateBuilder.CreateParameterBindingMethod(Configuration, out var parametersType);

                    if (Configuration.InvokeInformation.MethodInvokeDelegate == null)
                    {
                        InvokeMethodDelegate =
                            Services.MethodInvokerCreationService.BuildMethodInvoker <TReturn>(Configuration, parametersType);
                    }
                    else
                    {
                        InvokeMethodDelegate =
                            (InvokeMethodDelegate <TReturn>)(object) Configuration.InvokeInformation.MethodInvokeDelegate;
                    }

                    ResponseDelegate = Services.ResponseDelegateCreator.CreateResponseDelegate(Configuration);
                }
            }
        }
        public object GetInstance(ServiceChannel channel, InvokeMethodDelegate invokeMethod)
        {
            if (GlobaInstances.ContainsKey(channel.Provider))
            {
                return(GlobaInstances[channel.Provider]);
            }
            Type type;

            try
            {
                type = Builder.GetInstanceType(channel.Provider.Instance, channel.Provider.Contract);
            }
            catch
            {
                return(null);
            }
            object instance = CreateInstance(channel, invokeMethod, type);

            if (instance == null)
            {
                return(null);
            }
            GlobaInstances.Add(channel.Provider, instance);
            return(instance);
        }
Beispiel #4
0
        private static void CreateDynamicAssembly()
        {
            new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Assert();
            Type[]        types      = new Type[] { typeof(Type), typeof(Type), typeof(string) };
            MethodInfo    methodInfo = typeof(Delegate).GetMethod("CreateDelegate", types);
            DynamicMethod method     = new DynamicMethod("CreateDelegate", typeof(Delegate), types);

            method.DefineParameter(1, ParameterAttributes.In, "delegateType");
            method.DefineParameter(2, ParameterAttributes.In, "targetType");
            method.DefineParameter(3, ParameterAttributes.In, "methodName");
            ILGenerator iLGenerator = method.GetILGenerator(5);

            iLGenerator.Emit(OpCodes.Ldarg_0);
            iLGenerator.Emit(OpCodes.Ldarg_1);
            iLGenerator.Emit(OpCodes.Ldarg_2);
            iLGenerator.EmitCall(OpCodes.Call, methodInfo, null);
            iLGenerator.Emit(OpCodes.Ret);
            s_CreateDelegate1 = (CreateDelegate1Delegate)method.CreateDelegate(typeof(CreateDelegate1Delegate));
            types             = new Type[] { typeof(Type), typeof(object), typeof(string) };
            methodInfo        = typeof(Delegate).GetMethod("CreateDelegate", types);
            method            = new DynamicMethod("CreateDelegate", typeof(Delegate), types);
            method.DefineParameter(1, ParameterAttributes.In, "delegateType");
            method.DefineParameter(2, ParameterAttributes.In, "target");
            method.DefineParameter(3, ParameterAttributes.In, "methodName");
            iLGenerator = method.GetILGenerator(5);
            iLGenerator.Emit(OpCodes.Ldarg_0);
            iLGenerator.Emit(OpCodes.Ldarg_1);
            iLGenerator.Emit(OpCodes.Ldarg_2);
            iLGenerator.EmitCall(OpCodes.Call, methodInfo, null);
            iLGenerator.Emit(OpCodes.Ret);
            s_CreateDelegate2 = (CreateDelegate2Delegate)method.CreateDelegate(typeof(CreateDelegate2Delegate));
            types             = new Type[] { typeof(Type), typeof(object[]) };
            methodInfo        = typeof(Activator).GetMethod("CreateInstance", types);
            method            = new DynamicMethod("CreateInstance", typeof(object), types);
            method.DefineParameter(1, ParameterAttributes.In, "type");
            method.DefineParameter(2, ParameterAttributes.In, "arguments");
            iLGenerator = method.GetILGenerator(4);
            iLGenerator.Emit(OpCodes.Ldarg_0);
            iLGenerator.Emit(OpCodes.Ldarg_1);
            iLGenerator.EmitCall(OpCodes.Call, methodInfo, null);
            iLGenerator.Emit(OpCodes.Ret);
            s_CreateInstance = (CreateInstanceDelegate)method.CreateDelegate(typeof(CreateInstanceDelegate));
            types            = new Type[] { typeof(object), typeof(object[]) };
            Type[] parameterTypes = new Type[] { typeof(MethodInfo), typeof(object), typeof(object[]) };
            methodInfo = typeof(MethodInfo).GetMethod("Invoke", types);
            method     = new DynamicMethod("InvokeMethod", typeof(object), parameterTypes);
            method.DefineParameter(1, ParameterAttributes.In, "method");
            method.DefineParameter(2, ParameterAttributes.In, "instance");
            method.DefineParameter(3, ParameterAttributes.In, "args");
            iLGenerator = method.GetILGenerator(5);
            iLGenerator.Emit(OpCodes.Ldarg_0);
            iLGenerator.Emit(OpCodes.Ldarg_1);
            iLGenerator.Emit(OpCodes.Ldarg_2);
            iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
            iLGenerator.Emit(OpCodes.Ret);
            s_InvokeMethod = (InvokeMethodDelegate)method.CreateDelegate(typeof(InvokeMethodDelegate));
        }
Beispiel #5
0
        public CallbacksImpl(ICallbacks callbacks)
        {
            _callbacks = callbacks;

            _isTypeValidDelegate = IsTypeValid;
            GCHandle.Alloc(_isTypeValidDelegate);

            _releaseNetReferenceDelegate = ReleaseNetReference;
            GCHandle.Alloc(_releaseNetReferenceDelegate);

            _releaseNetDelegateGCHandleDelegate = ReleaseNetDelegateGCHandle;
            GCHandle.Alloc(_releaseNetDelegateGCHandleDelegate);

            _createLazyTypeInfoDelegate = CreateLazyTypeInfo;
            GCHandle.Alloc(_createLazyTypeInfoDelegate);

            _loadTypeInfoDelegate = LoadTypeInfo;
            GCHandle.Alloc(_loadTypeInfoDelegate);

            _instantiateTypeDelgate = InstantiateType;
            GCHandle.Alloc(_instantiateTypeDelgate);

            _callComponentCompletedDelegate = CallComponentCompleted;
            GCHandle.Alloc(_callComponentCompletedDelegate);

            _callObjectDestroyedDelegate = CallObjectDestroyed;
            GCHandle.Alloc(_callObjectDestroyedDelegate);

            _readPropertyDelegate = ReadProperty;
            GCHandle.Alloc(_readPropertyDelegate);

            _writePropertyDelegate = WriteProperty;
            GCHandle.Alloc(_writePropertyDelegate);

            _invokeMethodDelegate = InvokeMethod;
            GCHandle.Alloc(_invokeMethodDelegate);

            _gcCollectDelegate = GCCollect;
            GCHandle.Alloc(_gcCollectDelegate);

            _raiseNetSignalsDelegate = RaiseNetSignals;
            GCHandle.Alloc(_raiseNetSignalsDelegate);

            _awaitTaskDelegate = AwaitTask;
            GCHandle.Alloc(_awaitTaskDelegate);

            _serializeDelegate = Serialize;
            GCHandle.Alloc(_serializeDelegate);

            _invokeDelegateDelegate = InvokeDelegate;
            GCHandle.Alloc(_invokeDelegateDelegate);
        }
Beispiel #6
0
 public static object InvokeMethod(Form form, string methodName, params object[] parms)
 {
     if (form.InvokeRequired)
     {
         Delegate delg = new InvokeMethodDelegate(InvokeMethod);
         var      ret  = form.Invoke(delg, form, methodName, parms);
         are.WaitOne();
         return(ret);
     }
     else
     {
         var type       = form.GetType();
         var methodInfo = type.GetMethod(methodName, Reflector.Flags);
         var ret        = methodInfo.Invoke(form, parms);
         are.Set();
         return(ret);
     }
 }
        private object CreateInstance(ServiceChannel channel, InvokeMethodDelegate invokeMethod, Type type)
        {
            InstanceProxy proxy = new InstanceProxy(channel, channel.Provider.ClientOperations.ToList(), invokeMethod);

            if (Unity.IsRegistered(type))
            {
                return(Unity.GetInstance(type));
            }
            foreach (var constructor in type.GetConstructors())
            {
                var      parameters       = constructor.GetParameters();
                object[] parameterArray   = new object[parameters.Length];
                bool     parameterSuccess = true;
                parameterArray[0] = proxy;
                for (int i = 1; i < parameters.Length; i++)
                {
                    object obj = Unity.GetInstance(parameters[i].ParameterType);
                    if (obj == null)
                    {
                        parameterSuccess = false;
                        break;
                    }
                    parameterArray[i] = obj;
                }
                if (!parameterSuccess)
                {
                    continue;
                }
                try
                {
                    return(Activator.CreateInstance(type, parameterArray));
                }
                catch
                {
                }
            }
            return(null);
        }
        private static void CreateDynamicAssembly()
        {
            // 1. Assert permissions demanded by the DynamicMethod ctor.
            new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Assert(); // BlessedAssert

            // 2. Create the transparent methods, each wrapping a call to a reflection method,
            //    and cache a delegate to each method.
            Type[]        parameterTypes;        // signature of the reflection method
            Type[]        wrappedParameterTypes; // signature of the wrapping method (when different)
            MethodInfo    mi;                    // descriptor for the reflection method
            DynamicMethod method;                // wrapping method
            ILGenerator   il;                    // wrapping method's generator

            // 2a. Delegate.CreateDelegate( Type, Type, String )
            parameterTypes = new Type[] { typeof(Type), typeof(Type), typeof(String) };
            mi             = typeof(Delegate).GetMethod("CreateDelegate", parameterTypes);

            method = new DynamicMethod("CreateDelegate", typeof(Delegate), parameterTypes);
            method.DefineParameter(1, ParameterAttributes.In, "delegateType");
            method.DefineParameter(2, ParameterAttributes.In, "targetType");
            method.DefineParameter(3, ParameterAttributes.In, "methodName");

            il = method.GetILGenerator(5);
            il.Emit(OpCodes.Ldarg_0);               // push delegateType
            il.Emit(OpCodes.Ldarg_1);               // push targetType
            il.Emit(OpCodes.Ldarg_2);               // push methodName
            il.EmitCall(OpCodes.Call, mi, null);    // call Delegate.CreateDelegate
            il.Emit(OpCodes.Ret);                   // return the result

            s_CreateDelegate1 = (CreateDelegate1Delegate)method.CreateDelegate(typeof(CreateDelegate1Delegate));

            // 2b. Delegate.CreateDelegate( Type, Object, String )
            parameterTypes = new Type[] { typeof(Type), typeof(Object), typeof(String) };
            mi             = typeof(Delegate).GetMethod("CreateDelegate", parameterTypes);

            method = new DynamicMethod("CreateDelegate", typeof(Delegate), parameterTypes);
            method.DefineParameter(1, ParameterAttributes.In, "delegateType");
            method.DefineParameter(2, ParameterAttributes.In, "target");
            method.DefineParameter(3, ParameterAttributes.In, "methodName");

            il = method.GetILGenerator(5);
            il.Emit(OpCodes.Ldarg_0);               // push delegateType
            il.Emit(OpCodes.Ldarg_1);               // push target
            il.Emit(OpCodes.Ldarg_2);               // push methodName
            il.EmitCall(OpCodes.Call, mi, null);    // call Delegate.CreateDelegate
            il.Emit(OpCodes.Ret);                   // return the result

            s_CreateDelegate2 = (CreateDelegate2Delegate)method.CreateDelegate(typeof(CreateDelegate2Delegate));

            // 2c. Activator.CreateInstance( Type, Object[] )
            parameterTypes = new Type[] { typeof(Type), typeof(Object[]) };
            mi             = typeof(Activator).GetMethod("CreateInstance", parameterTypes);

            method = new DynamicMethod("CreateInstance", typeof(Object), parameterTypes);
            method.DefineParameter(1, ParameterAttributes.In, "type");
            method.DefineParameter(2, ParameterAttributes.In, "arguments");

            il = method.GetILGenerator(4);
            il.Emit(OpCodes.Ldarg_0);               // push type
            il.Emit(OpCodes.Ldarg_1);               // push arguments
            il.EmitCall(OpCodes.Call, mi, null);    // call Activator.CreateInstance
            il.Emit(OpCodes.Ret);                   // return the result

            s_CreateInstance = (CreateInstanceDelegate)method.CreateDelegate(typeof(CreateInstanceDelegate));

            // 2d. MethodInfo.Invoke(object, args)
            parameterTypes        = new Type[] { typeof(Object), typeof(Object[]) };
            wrappedParameterTypes = new Type[] { typeof(MethodInfo), typeof(Object), typeof(Object[]) };
            mi = typeof(MethodInfo).GetMethod("Invoke", parameterTypes);

            method = new DynamicMethod("InvokeMethod", typeof(Object), wrappedParameterTypes);
            method.DefineParameter(1, ParameterAttributes.In, "method");
            method.DefineParameter(2, ParameterAttributes.In, "instance");
            method.DefineParameter(3, ParameterAttributes.In, "args");

            il = method.GetILGenerator(5);
            il.Emit(OpCodes.Ldarg_0);                // push method
            il.Emit(OpCodes.Ldarg_1);                // push instance
            il.Emit(OpCodes.Ldarg_2);                // push args
            il.EmitCall(OpCodes.Callvirt, mi, null); // call method.Invoke
            il.Emit(OpCodes.Ret);                    // return the result

            s_InvokeMethod = (InvokeMethodDelegate)method.CreateDelegate(typeof(InvokeMethodDelegate));
        }
Beispiel #9
0
        public static object InvokeMethod(Control ctrl, string methodName, params object[] args)
        {
            if (ctrl.InvokeRequired)
            {
                InvokeMethodDelegate del = new InvokeMethodDelegate(CrossThreadUI.InvokeMethod);
                if (CrossThreadUI.ExecSync)
                {
                    return(ctrl.Invoke(del, ctrl, methodName, args));
                }
                else
                {
                    ctrl.BeginInvoke(del, ctrl, methodName, args);
                }
            }
            else
            {
                Type ctrlType = ctrl.GetType();

                // Determine the type of each passed argument in order to try and
                //   determine the unique signature for the requested method.
                Type[] paramTypes = new Type[args.Length];
                for (int i = 0; i < paramTypes.Length; i++)
                {
                    paramTypes[i] = args[i].GetType();
                }

                // Now it's time to get a reference to the method.
                MethodInfo mi = ctrlType.GetMethod(methodName, paramTypes);
                if (mi != null)
                {
                    #region DEPRECIATED :: Checking Method Parameters
                    // We don't really need to do this, since we're using the
                    //   provided method arguments as a signature when we
                    //   search for the method.

                    //// Check to make sure the proper number of parameters were passed
                    ////   and that all the types match.
                    //ParameterInfo[] p = mi.GetParameters();
                    //if (p.Length > 0)
                    //{
                    //    // If the 'args' value is null or the lengths don't match,
                    //    //   throw an exception.
                    //    if (args == null || (args.Length != p.Length))
                    //        throw new ArgumentException("Wrong number of arguments for method '" + methodName + "'.", "args");

                    //    // Check to make sure all the parameters are of the correct type.
                    //    for (int i = 0; i < p.Length; i++)
                    //    {
                    //        Type argType = args[i].GetType();
                    //        if (argType.FullName != p[i].ParameterType.FullName && !p[i].ParameterType.IsSubclassOf(argType))
                    //            throw new ArgumentException(string.Format("Given argument is of the wrong type for parameter '{0}'. Expected '{1}' but recieved '{2}' for parameter position {3}.", p[i].Name, p[i].ParameterType.FullName, argType.FullName, p[i].Position), "args");
                    //    }
                    //}
                    #endregion

                    // If we passed all the validation, then call the invoke for the
                    //   MemberInfo object.
                    return(mi.Invoke(ctrl, args));
                }
                else
                {
                    throw new ArgumentException("Specified control does not expose a '" + methodName + "' method with the provided parameter types.", "methodName");
                }
            }
            return(null);
        }
 internal InstanceProxy(ServiceChannel channel, List <MethodInfo> methods, InvokeMethodDelegate invokeMethod)
 {
     Channel      = channel;
     Methods      = methods;
     InvokeMethod = invokeMethod;
 }
 public StreamServiceReceiver(T serviceImplementor, Stream stream)
 {
     Stream = stream;
     invoke = ProxyFactory.CreateReceiverProxy(serviceImplementor, out instance, out commandsMap);
 }
 // --- Private Static Methods ---
 private static void InitStaticFields()
 {
     _methodInvokeDelegate = new InvokeDelegateDelegate(InvokeDelegate);
     _methodInvokeMethod   = new InvokeMethodDelegate(InvokeMethod);
 }
        /// <summary>
        /// Invokes a method member on a given Control instance with proper type-safety.
        /// </summary>
        /// <param name="ctrl">The System.Windows.Forms.Control object on which the method will be invoked.</param>
        /// <param name="methodName">The name of the method that will be invoked.</param>
        /// <param name="args">Any arguments the method requires.  Array objects must be properly typed to match the method's signature.</param>
        /// <returns>An object whose type will match the specified method's signature or null if the method has no return value.</returns>
        public static object InvokeMethod(Control ctrl, string methodName, params object[] args)
        {
            if (ctrl.InvokeRequired)
            {
                InvokeMethodDelegate del = new InvokeMethodDelegate(CrossThreadUI.InvokeMethod);
                if (CrossThreadUI.ExecSync)
                    return ctrl.Invoke(del, ctrl, methodName, args);
                else
                    ctrl.BeginInvoke(del, ctrl, methodName, args);
            }
            else
            {
                Type ctrlType = ctrl.GetType();

                // Determine the type of each passed argument in order to try and
                //   determine the unique signature for the requested method.
                Type[] paramTypes = new Type[args.Length];
                for (int i = 0; i < paramTypes.Length; i++)
                    paramTypes[i] = args[i].GetType();

                // Now it's time to get a reference to the method.
                MethodInfo mi = ctrlType.GetMethod(methodName, paramTypes);
                if (mi != null)
                {
                    #region DEPRECIATED :: Checking Method Parameters
                    // We don't really need to do this, since we're using the
                    //   provided method arguments as a signature when we
                    //   search for the method.

                    //// Check to make sure the proper number of parameters were passed
                    ////   and that all the types match.
                    //ParameterInfo[] p = mi.GetParameters();
                    //if (p.Length > 0)
                    //{
                    //    // If the 'args' value is null or the lengths don't match,
                    //    //   throw an exception.
                    //    if (args == null || (args.Length != p.Length))
                    //        throw new ArgumentException("Wrong number of arguments for method '" + methodName + "'.", "args");

                    //    // Check to make sure all the parameters are of the correct type.
                    //    for (int i = 0; i < p.Length; i++)
                    //    {
                    //        Type argType = args[i].GetType();
                    //        if (argType.FullName != p[i].ParameterType.FullName && !p[i].ParameterType.IsSubclassOf(argType))
                    //            throw new ArgumentException(string.Format("Given argument is of the wrong type for parameter '{0}'. Expected '{1}' but recieved '{2}' for parameter position {3}.", p[i].Name, p[i].ParameterType.FullName, argType.FullName, p[i].Position), "args");
                    //    }
                    //}
                    #endregion
                    
                    // If we passed all the validation, then call the invoke for the
                    //   MemberInfo object.
                    return mi.Invoke(ctrl, args);
                }
                else
                    throw new ArgumentException("Specified control does not expose a '" + methodName + "' method with the provided parameter types.", "methodName");
            }
            return null;
        }
 private static void CreateDynamicAssembly()
 {
     new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Assert();
     Type[] types = new Type[] { typeof(Type), typeof(Type), typeof(string) };
     MethodInfo methodInfo = typeof(Delegate).GetMethod("CreateDelegate", types);
     DynamicMethod method = new DynamicMethod("CreateDelegate", typeof(Delegate), types);
     method.DefineParameter(1, ParameterAttributes.In, "delegateType");
     method.DefineParameter(2, ParameterAttributes.In, "targetType");
     method.DefineParameter(3, ParameterAttributes.In, "methodName");
     ILGenerator iLGenerator = method.GetILGenerator(5);
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Ldarg_1);
     iLGenerator.Emit(OpCodes.Ldarg_2);
     iLGenerator.EmitCall(OpCodes.Call, methodInfo, null);
     iLGenerator.Emit(OpCodes.Ret);
     s_CreateDelegate1 = (CreateDelegate1Delegate) method.CreateDelegate(typeof(CreateDelegate1Delegate));
     types = new Type[] { typeof(Type), typeof(object), typeof(string) };
     methodInfo = typeof(Delegate).GetMethod("CreateDelegate", types);
     method = new DynamicMethod("CreateDelegate", typeof(Delegate), types);
     method.DefineParameter(1, ParameterAttributes.In, "delegateType");
     method.DefineParameter(2, ParameterAttributes.In, "target");
     method.DefineParameter(3, ParameterAttributes.In, "methodName");
     iLGenerator = method.GetILGenerator(5);
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Ldarg_1);
     iLGenerator.Emit(OpCodes.Ldarg_2);
     iLGenerator.EmitCall(OpCodes.Call, methodInfo, null);
     iLGenerator.Emit(OpCodes.Ret);
     s_CreateDelegate2 = (CreateDelegate2Delegate) method.CreateDelegate(typeof(CreateDelegate2Delegate));
     types = new Type[] { typeof(Type), typeof(object[]) };
     methodInfo = typeof(Activator).GetMethod("CreateInstance", types);
     method = new DynamicMethod("CreateInstance", typeof(object), types);
     method.DefineParameter(1, ParameterAttributes.In, "type");
     method.DefineParameter(2, ParameterAttributes.In, "arguments");
     iLGenerator = method.GetILGenerator(4);
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Ldarg_1);
     iLGenerator.EmitCall(OpCodes.Call, methodInfo, null);
     iLGenerator.Emit(OpCodes.Ret);
     s_CreateInstance = (CreateInstanceDelegate) method.CreateDelegate(typeof(CreateInstanceDelegate));
     types = new Type[] { typeof(object), typeof(object[]) };
     Type[] parameterTypes = new Type[] { typeof(MethodInfo), typeof(object), typeof(object[]) };
     methodInfo = typeof(MethodInfo).GetMethod("Invoke", types);
     method = new DynamicMethod("InvokeMethod", typeof(object), parameterTypes);
     method.DefineParameter(1, ParameterAttributes.In, "method");
     method.DefineParameter(2, ParameterAttributes.In, "instance");
     method.DefineParameter(3, ParameterAttributes.In, "args");
     iLGenerator = method.GetILGenerator(5);
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Ldarg_1);
     iLGenerator.Emit(OpCodes.Ldarg_2);
     iLGenerator.EmitCall(OpCodes.Callvirt, methodInfo, null);
     iLGenerator.Emit(OpCodes.Ret);
     s_InvokeMethod = (InvokeMethodDelegate) method.CreateDelegate(typeof(InvokeMethodDelegate));
 }
Beispiel #15
0
 public MethodHandler(Method method, MethodParameter[] parameters, InvokeMethodDelegate invoke)
 {
     _invoke    = invoke;
     Method     = method;
     Parameters = parameters;
 }