コード例 #1
0
        /// <summary>
        /// Provides implementation for binary operations. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as addition and multiplication.
        /// </summary>
        /// <param name="binder">Provides information about the binary operation. The binder.Operation property returns an <see cref="T:System.Linq.Expressions.ExpressionType" /> object. For example, for the sum = first + second statement, where first and second are derived from the DynamicObject class, binder.Operation returns ExpressionType.Add.</param>
        /// <param name="arg">The right operand for the binary operation. For example, for the sum = first + second statement, where first and second are derived from the DynamicObject class, <paramref name="arg" /> is equal to second.</param>
        /// <param name="result">The result of the binary operation.</param>
        /// <returns>
        /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)
        /// </returns>
        public override bool TryBinaryOperation(System.Dynamic.BinaryOperationBinder binder, object arg, out object result)
        {
            //If we are binding from another dynamic object then our method info will be bound to an object
            if (typeof(BoundMethodInfo).IsAssignableFrom(arg.GetType()))
            {
                BoundMethodInfo mi = (BoundMethodInfo)arg;
                //Create a delegate from the method instance on the bound object
                arg = Delegate.CreateDelegate(this.BoundEvent.EventHandlerType, mi.BoundObject, mi.MethodInfo);
            }

            //If we have a delegate then handle assigning the event handler
            if (typeof(Delegate).IsAssignableFrom(arg.GetType()))
            {
                switch (binder.Operation)
                {
                case System.Linq.Expressions.ExpressionType.AddAssign:
                    this.BoundEvent.GetAddMethod(true).Invoke(base.Target, new[] { arg });
                    break;

                case System.Linq.Expressions.ExpressionType.SubtractAssign:
                    this.BoundEvent.GetRemoveMethod(true).Invoke(base.Target, new[] { arg });
                    break;

                default:
                    throw new NotImplementedException();
                }
                result = null;
                return(true);
            }

            return(base.TryBinaryOperation(binder, arg, out result));
        }
コード例 #2
0
        /// <summary>
        /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result" />.</param>
        /// <returns>
        /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
        /// </returns>
        /// <exception cref="NotImplementedException">
        /// </exception>
        public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
        {
            MemberInfo member = FindMember(binder.Name);

            //If we have a member then return that
            if (member != null)
            {
                switch (member.MemberType)
                {
                case MemberTypes.Event:
                    //Return an event binder
                    result = new DynamicReflector((EventInfo)member, base.Target)
                    {
                        WrapReturn = this.WrapReturn
                    };
                    break;

                case MemberTypes.Property:
                    //Check if we should wrap the result in another dynamic reflector
                    result = ((PropertyInfo)member).GetValue(base.Target);
                    if (WrapReturn)
                    {
                        result = new DynamicReflector(result)
                        {
                            WrapReturn = this.WrapReturn
                        };
                    }
                    break;

                case MemberTypes.Field:
                    //Check if we should wrap the result in another dynamic reflector
                    result = ((FieldInfo)member).GetValue(base.Target);
                    if (WrapReturn)
                    {
                        result = new DynamicReflector(result)
                        {
                            WrapReturn = this.WrapReturn
                        };
                    }
                    break;

                case MemberTypes.Method:
                    //Return the method info bound to the target object
                    result = new BoundMethodInfo((MethodInfo)member, base.Target);
                    break;

                case MemberTypes.NestedType:
                    //TODO: Implement
                    throw new NotImplementedException();

                default:
                    throw new NotImplementedException();
                }

                return(true);
            }

            return(base.TryGetMember(binder, out result));
        }
コード例 #3
0
ファイル: DynamicReflector.cs プロジェクト: TGOSeraph/StUtil
        /// <summary>
        /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result" />.</param>
        /// <returns>
        /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
        /// </returns>
        /// <exception cref="NotImplementedException">
        /// </exception>
        public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
        {
            MemberInfo member = FindMember(binder.Name);

            //If we have a member then return that
            if (member != null)
            {
                switch (member.MemberType)
                {
                    case MemberTypes.Event:
                        //Return an event binder
                        result = new DynamicReflector((EventInfo)member, base.Target) { WrapReturn = this.WrapReturn };
                        break;
                    case MemberTypes.Property:
                        //Check if we should wrap the result in another dynamic reflector
                        result = ((PropertyInfo)member).GetValue(base.Target);
                        if (WrapReturn)
                        {
                            result = new DynamicReflector(result) { WrapReturn = this.WrapReturn };
                        }
                        break;
                    case MemberTypes.Field:
                        //Check if we should wrap the result in another dynamic reflector
                        result = ((FieldInfo)member).GetValue(base.Target);
                        if (WrapReturn)
                        {
                            result = new DynamicReflector(result) { WrapReturn = this.WrapReturn };
                        }
                        break;
                    case MemberTypes.Method:
                        //Return the method info bound to the target object
                        result = new BoundMethodInfo((MethodInfo)member, base.Target);
                        break;
                    case MemberTypes.NestedType:
                        //TODO: Implement
                        throw new NotImplementedException();
                    default:
                        throw new NotImplementedException();
                }

                return true;
            }

            return base.TryGetMember(binder, out result);
        }