Example #1
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));
        }
Example #2
0
 /// <summary>
 /// Provides the implementation for operations that get a value by index. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for indexing operations.
 /// </summary>
 /// <param name="binder">Provides information about the operation.</param>
 /// <param name="indexes">The indexes that are used in the operation. For example, for the sampleObject[3] operation in C# (sampleObject(3) in Visual Basic), where sampleObject is derived from the DynamicObject class, <paramref name="indexes[0]" /> is equal to 3.</param>
 /// <param name="result">The result of the index 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 run-time exception is thrown.)
 /// </returns>
 public override bool TryGetIndex(System.Dynamic.GetIndexBinder binder, object[] indexes, out object result)
 {
     try
     {
         PropertyInfo prop = TargetType.GetProperty("Item", AccessFlags);
         result = prop.GetValue(Target, indexes);
         if (WrapReturn)
         {
             result = new DynamicReflector(result)
             {
                 WrapReturn = this.WrapReturn
             };
         }
         return(true);
     }
     catch (Exception)
     {
         return(base.TryGetIndex(binder, indexes, out result));
     }
 }
Example #3
0
        /// <summary>
        /// Provides the implementation for operations that invoke a member. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as calling a method.
        /// </summary>
        /// <param name="binder">Provides information about the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleMethod". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="args">The arguments that are passed to the object member during the invoke operation. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, <paramref name="args[0]" /> is equal to 100.</param>
        /// <param name="result">The result of the member invocation.</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 TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
        {
            //Try and find normal method
            MethodInfo method = FindMethod(binder.Name, args);
            Type t;
            if (method == null)
            {
                //If we cant find one look for an event
                EventInfo evt = null;
                t = base.TargetType;
                do
                {
                    evt = t.GetEvent(binder.Name, this.AccessFlags);
                } while (evt == null && (t = t.BaseType) != null);

                if (evt != null)
                {
                    //Try and get the raise method
                    method = evt.GetRaiseMethod(true);
                    if (method == null)
                    {
                        //Look for the events backing field
                        var field = t.GetField(evt.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                        if (field == null)
                        {
                            field = t.GetField("_" + evt.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                            if (field == null)
                            {
                                field = t.GetField("m_" + evt.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                            }
                        }
                        if (field != null)
                        {
                            var eventDelegate = (MulticastDelegate)field.GetValue(base.Target);
                            if (eventDelegate != null)
                            {
                                //Trigger each handler
                                foreach (var handler in eventDelegate.GetInvocationList())
                                {
                                    handler.Method.Invoke(handler.Target, args);
                                }
                                result = new DynamicReflector(evt, Target) { WrapReturn = this.WrapReturn };
                                return true;
                            }
                        }

                        //If we still dont have one, look for an On[eventname] method to raise
                        method = FindMethod("On" + binder.Name, args);
                        if (method == null)
                        {
                            //Try without the sender argument
                            var a = args.Skip(1).ToArray();
                            method = FindMethod("On" + binder.Name, a);
                            if (method != null)
                            {
                                args = a;
                            }
                            else
                            {
                                result = new DynamicReflector(evt, Target) { WrapReturn = this.WrapReturn };
                                return false;
                            }
                        }
                    }
                }
            }

            if (method != null)
            {
                result = method.Invoke(Target, args);
                if (WrapReturn)
                {
                    result = new DynamicReflector(result) { WrapReturn = this.WrapReturn };
                }
                return true;
            }

            //We arent invoking a method or event, so check for a nested type to construct
            Type nested = null;
            t = base.TargetType;
            do
            {
                nested = t.GetNestedType(binder.Name, this.AccessFlags);
            } while (nested == null && (t = t.BaseType) != null);

            if (nested != null)
            {
                var ctor = nested.GetConstructor(this.AccessFlags, null, args.Select(a => a == null ? null : a.GetType()).ToArray(), null);
                if (ctor != null)
                {
                    result = ctor.Invoke(args);
                    if (this.WrapReturn)
                    {
                        result = new DynamicReflector(result) { WrapReturn = this.WrapReturn };
                    }
                    return true;
                }
            }

            return base.TryInvokeMember(binder, args, out result);
        }
Example #4
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);
        }
Example #5
0
 /// <summary>
 /// Provides the implementation for operations that get a value by index. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for indexing operations.
 /// </summary>
 /// <param name="binder">Provides information about the operation.</param>
 /// <param name="indexes">The indexes that are used in the operation. For example, for the sampleObject[3] operation in C# (sampleObject(3) in Visual Basic), where sampleObject is derived from the DynamicObject class, <paramref name="indexes[0]" /> is equal to 3.</param>
 /// <param name="result">The result of the index 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 run-time exception is thrown.)
 /// </returns>
 public override bool TryGetIndex(System.Dynamic.GetIndexBinder binder, object[] indexes, out object result)
 {
     try
     {
         PropertyInfo prop = TargetType.GetProperty("Item", AccessFlags);
         result = prop.GetValue(Target, indexes);
         if (WrapReturn)
         {
             result = new DynamicReflector(result) { WrapReturn = this.WrapReturn };
         }
         return true;
     }
     catch (Exception)
     {
         return base.TryGetIndex(binder, indexes, out result);
     }
 }
Example #6
0
        /// <summary>
        /// Provides the implementation for operations that invoke a member. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as calling a method.
        /// </summary>
        /// <param name="binder">Provides information about the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleMethod". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="args">The arguments that are passed to the object member during the invoke operation. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, <paramref name="args[0]" /> is equal to 100.</param>
        /// <param name="result">The result of the member invocation.</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 TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
        {
            //Try and find normal method
            MethodInfo method = FindMethod(binder.Name, args);
            Type       t;

            if (method == null)
            {
                //If we cant find one look for an event
                EventInfo evt = null;
                t = base.TargetType;
                do
                {
                    evt = t.GetEvent(binder.Name, this.AccessFlags);
                } while (evt == null && (t = t.BaseType) != null);

                if (evt != null)
                {
                    //Try and get the raise method
                    method = evt.GetRaiseMethod(true);
                    if (method == null)
                    {
                        //Look for the events backing field
                        var field = t.GetField(evt.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                        if (field == null)
                        {
                            field = t.GetField("_" + evt.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                            if (field == null)
                            {
                                field = t.GetField("m_" + evt.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                            }
                        }
                        if (field != null)
                        {
                            var eventDelegate = (MulticastDelegate)field.GetValue(base.Target);
                            if (eventDelegate != null)
                            {
                                //Trigger each handler
                                foreach (var handler in eventDelegate.GetInvocationList())
                                {
                                    handler.Method.Invoke(handler.Target, args);
                                }
                                result = new DynamicReflector(evt, Target)
                                {
                                    WrapReturn = this.WrapReturn
                                };
                                return(true);
                            }
                        }

                        //If we still dont have one, look for an On[eventname] method to raise
                        method = FindMethod("On" + binder.Name, args);
                        if (method == null)
                        {
                            //Try without the sender argument
                            var a = args.Skip(1).ToArray();
                            method = FindMethod("On" + binder.Name, a);
                            if (method != null)
                            {
                                args = a;
                            }
                            else
                            {
                                result = new DynamicReflector(evt, Target)
                                {
                                    WrapReturn = this.WrapReturn
                                };
                                return(false);
                            }
                        }
                    }
                }
            }

            if (method != null)
            {
                result = method.Invoke(Target, args);
                if (WrapReturn)
                {
                    result = new DynamicReflector(result)
                    {
                        WrapReturn = this.WrapReturn
                    };
                }
                return(true);
            }

            //We arent invoking a method or event, so check for a nested type to construct
            Type nested = null;

            t = base.TargetType;
            do
            {
                nested = t.GetNestedType(binder.Name, this.AccessFlags);
            } while (nested == null && (t = t.BaseType) != null);

            if (nested != null)
            {
                var ctor = nested.GetConstructor(this.AccessFlags, null, args.Select(a => a == null ? null : a.GetType()).ToArray(), null);
                if (ctor != null)
                {
                    result = ctor.Invoke(args);
                    if (this.WrapReturn)
                    {
                        result = new DynamicReflector(result)
                        {
                            WrapReturn = this.WrapReturn
                        };
                    }
                    return(true);
                }
            }

            return(base.TryInvokeMember(binder, args, out result));
        }