Used as the key for the LanguageContext.GetDelegate method caching system
Example #1
0
        internal DelegateInfo(MethodInfo method, object[] constants, DelegateSignatureInfo sigInfo) {
            Assert.NotNull(method, constants);

            _method = method;
            _constants = constants;
            _sigInfo = sigInfo;
        }
Example #2
0
        public Delegate GetOrCreateDelegateForDynamicObject(object dynamicObject, Type delegateType, MethodInfo invoke)
        {
            var          signatureInfo = new DelegateSignatureInfo(invoke);
            DelegateInfo delegateInfo  = _dynamicDelegateCache.GetOrCreateValue(
                signatureInfo,
                () => new DelegateInfo(_languageContext, signatureInfo.ReturnType, signatureInfo.ParameterTypes)
                );

            return(delegateInfo.CreateDelegate(delegateType, dynamicObject));
        }
Example #3
0
        /// <summary>
        /// Creates a delegate with a given signature that could be used to invoke this object from non-dynamic code (w/o code context).
        /// A stub is created that makes appropriate conversions/boxing and calls the object.
        /// The stub should be executed within a context of this object's language.
        /// </summary>
        /// <returns>The delegate or a <c>null</c> reference if the object is not callable.</returns>
        public static Delegate GetDelegate(LanguageContext context, object callableObject, Type delegateType)
        {
            ContractUtils.RequiresNotNull(context, "context");
            ContractUtils.RequiresNotNull(delegateType, "delegateType");

            Delegate result = callableObject as Delegate;

            if (result != null)
            {
                if (!delegateType.IsAssignableFrom(result.GetType()))
                {
                    throw ScriptingRuntimeHelpers.SimpleTypeError(String.Format("Cannot cast {0} to {1}.", result.GetType(), delegateType));
                }

                return(result);
            }

            IDynamicObject dynamicObject = callableObject as IDynamicObject;

            if (dynamicObject != null)
            {
                MethodInfo invoke;

                if (!typeof(Delegate).IsAssignableFrom(delegateType) || (invoke = delegateType.GetMethod("Invoke")) == null)
                {
                    throw ScriptingRuntimeHelpers.SimpleTypeError("A specific delegate type is required.");
                }

                ParameterInfo[]       parameters    = invoke.GetParameters();
                DelegateSignatureInfo signatureInfo = new DelegateSignatureInfo(
                    context,
                    invoke.ReturnType,
                    parameters
                    );

                DelegateInfo delegateInfo = _dynamicDelegateCache.GetOrCreateValue(signatureInfo,
                                                                                   delegate() {
                    // creation code
                    return(signatureInfo.GenerateDelegateStub());
                });


                result = delegateInfo.CreateDelegate(delegateType, dynamicObject);
                if (result != null)
                {
                    return(result);
                }
            }

            throw ScriptingRuntimeHelpers.SimpleTypeError("Object is not callable.");
        }
        /// <summary>
        /// Creates a delegate with a given signature that could be used to invoke this object from non-dynamic code (w/o code context).
        /// A stub is created that makes appropriate conversions/boxing and calls the object.
        /// The stub should be executed within a context of this object's language.
        /// </summary>
        /// <returns>The converted delegate.</returns>
        /// <exception cref="T:Microsoft.Scripting.ArgumentTypeException">The object is either a subclass of Delegate but not the requested type or does not implement IDynamicMetaObjectProvider.</exception>
        public Delegate GetDelegate(object callableObject, Type delegateType) {
            ContractUtils.RequiresNotNull(delegateType, "delegateType");

            Delegate result = callableObject as Delegate;
            if (result != null) {
                if (!delegateType.IsAssignableFrom(result.GetType())) {
                    throw ScriptingRuntimeHelpers.SimpleTypeError(String.Format("Cannot cast {0} to {1}.", result.GetType(), delegateType));
                }

                return result;
            }

            IDynamicMetaObjectProvider dynamicObject = callableObject as IDynamicMetaObjectProvider;
            if (dynamicObject != null) {

                MethodInfo invoke;

                if (!typeof(Delegate).IsAssignableFrom(delegateType) || (invoke = delegateType.GetMethod("Invoke")) == null) {
                    throw ScriptingRuntimeHelpers.SimpleTypeError("A specific delegate type is required.");
                }

                ParameterInfo[] parameters = invoke.GetParameters();
                DelegateSignatureInfo signatureInfo = new DelegateSignatureInfo(
                    invoke.ReturnType,
                    parameters
                );

                DelegateInfo delegateInfo = _dynamicDelegateCache.GetOrCreateValue(signatureInfo,
                    delegate() {
                        // creation code
                        return signatureInfo.GenerateDelegateStub(_languageContext);
                    });


                result = delegateInfo.CreateDelegate(delegateType, dynamicObject);
                if (result != null) {
                    return result;
                }
            }

            throw ScriptingRuntimeHelpers.SimpleTypeError("Object is not callable.");
        }
Example #5
0
        public override bool Equals(object obj)
        {
            DelegateSignatureInfo dsi = obj as DelegateSignatureInfo;

            if (dsi == null ||
                dsi._parameterTypes.Length != _parameterTypes.Length ||
                dsi._returnType != _returnType)
            {
                return(false);
            }

            for (int i = 0; i < _parameterTypes.Length; i++)
            {
                if (dsi._parameterTypes[i] != _parameterTypes[i])
                {
                    return(false);
                }
            }

            return(true);
        }