public void TestMethodNulls()
    {
      var t = new TestWrapper();
      dynamic w = new DynamicProxy(t);

      string r = w.EchoInternal(null);
      Assert.AreEqual("Internal: ", r);

      r = w.EchoInternal2(1);
      Assert.AreEqual("Int: 1", r);
      r = w.EchoInternal2("Test");
      Assert.AreEqual("String: Test", r);

      r = w.EchoInternal2("Test", "Testing");
      Assert.AreEqual("Name: Test Value: Testing", r);

      r = w.EchoInternal2("Test", null);
      Assert.AreEqual("Name: Test Value: ", r);


      r = w.EchoInternal3("Test", "Testing", null);
      Assert.AreEqual("Name: Test Value: Testing Value2: ", r);

      r = w.EchoInternal3("Test", 1, null);
      Assert.AreEqual("Name: Test Value: 1 Value2: ", r);

    }
Example #2
0
        private void CloneGenericCollection(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly collections
            object targetValue = accessor.HasSetter
              ? CreateTargetValue(accessor, originalValue, target)
              : accessor.GetValue(target);

            if (targetValue == null)
            {
                return;
            }

            var sourceList = originalValue as IEnumerable;

            if (sourceList == null)
            {
                return;
            }

            // dynamic wrapper to call generic Add method
            dynamic targetList = new DynamicProxy(targetValue);

            foreach (var sourceItem in sourceList)
            {
                var cloneItem = CloneInstance(sourceItem);
                targetList.Add(cloneItem);
            }

            if (!accessor.HasSetter)
            {
                return;
            }

            accessor.SetValue(target, targetValue);
        }
    public void TestMethod()
    {
      var t = new TestWrapper();
      dynamic w = new DynamicProxy(t);

      string r = w.Name;
      Assert.AreEqual("Test", r);

      r = w.Internal;
      Assert.AreEqual("InternalTest", r);

      r = w.Private;
      Assert.AreEqual("PrivateTest", r);

      r = w.EchoPublic("Tester");
      Assert.AreEqual("Public: Tester", r);

      r = w.EchoPrivate("Tester");
      Assert.AreEqual("Private: Tester", r);

      r = w.EchoInternal("Tester");
      Assert.AreEqual("Internal: Tester", r);

      r = w.EchoInternal(null);
      Assert.AreEqual("Internal: ", r);

    }
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>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;

            var memeber = _typeAccessor.Find(binder.Name, _flags);

            if (memeber == null)
            {
                return(SafeMode);
            }

            if (!memeber.HasGetter)
            {
                return(SafeMode);
            }

            var value = memeber.GetValue(_wrapped);

            if (value == null)
            {
                return(true);
            }

            result = new DynamicProxy(value, SafeMode);
            return(true);
        }
Example #5
0
        private void CloneGenericDictionary(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly dictionary
            object targetValue = accessor.HasSetter
              ? CreateTargetValue(accessor, originalValue, target)
              : accessor.GetValue(target);

            if (targetValue == null)
            {
                return;
            }

            // must support IEnumerable
            var sourceList = originalValue as IEnumerable;

            if (sourceList == null)
            {
                return;
            }

            // dynamic wrapper to call generic Add method
            dynamic targetDictionary = new DynamicProxy(targetValue);

            var e = sourceList.GetEnumerator();

            while (e.MoveNext())
            {
                // dynamic wrapper to get the key and value
                dynamic proxy      = new DynamicProxy(e.Current);
                var     keyProxy   = proxy.Key as IDynamicProxy;
                var     valueProxy = proxy.Value as IDynamicProxy;

                if (keyProxy == null)
                {
                    continue;
                }

                object key   = keyProxy.Wrapped;
                object value = valueProxy != null ? valueProxy.Wrapped : null;

                object cloneItem = CloneInstance(value);
                targetDictionary.Add(key, cloneItem);
            }

            if (!accessor.HasSetter)
            {
                return;
            }

            accessor.SetValue(target, targetValue);
        }
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(InvokeMemberBinder binder, object[] args, out object result)
        {
            result = null;

            var types  = args.Select(a => a == null ? typeof(object) : a.GetType()).ToArray();
            var method = _typeAccessor.FindMethod(binder.Name, types, _flags);

            if (method == null)
            {
                return(SafeMode);
            }

            var value = method.Invoke(_wrapped, args);

            if (value == null)
            {
                return(true);
            }

            result = new DynamicProxy(value, SafeMode);
            return(true);
        }
        /// <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(InvokeMemberBinder binder, object[] args, out object result)
        {
            result = null;

            var types = args.Select(a => a == null ? typeof(object) : a.GetType()).ToArray();
            var method = _typeAccessor.FindMethod(binder.Name, types, _flags);

            if (method == null)
                return SafeMode;

            var value = method.Invoke(_wrapped, args);
            if (value == null)
                return true;

            result = new DynamicProxy(value, SafeMode);
            return true;
        }
        /// <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>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;

            var memeber = _typeAccessor.Find(binder.Name, _flags);
            if (memeber == null)
                return SafeMode;

            if (!memeber.HasGetter)
                return SafeMode;

            var value = memeber.GetValue(_wrapped);
            if (value == null)
                return true;

            result = new DynamicProxy(value, SafeMode);
            return true;
        }
        private void CloneGenericDictionary(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly dictionary
            object targetValue = accessor.HasSetter
              ? CreateTargetValue(accessor, originalValue, target)
              : accessor.GetValue(target);

            if (targetValue == null)
                return;

            // must support IEnumerable
            var sourceList = originalValue as IEnumerable;
            if (sourceList == null)
                return;

            // dynamic wrapper to call generic Add method
            dynamic targetDictionary = new DynamicProxy(targetValue);
            
            var e = sourceList.GetEnumerator();
            while (e.MoveNext())
            {
                // dynamic wrapper to get the key and value
                dynamic proxy = new DynamicProxy(e.Current);
                var keyProxy = proxy.Key as IDynamicProxy;
                var valueProxy = proxy.Value as IDynamicProxy;

                if (keyProxy == null)
                    continue;

                object key = keyProxy.Wrapped;
                object value = valueProxy != null ? valueProxy.Wrapped : null;

                object cloneItem = CloneInstance(value);
                targetDictionary.Add(key, cloneItem);
            }

            if (!accessor.HasSetter)
                return;

            accessor.SetValue(target, targetValue);
        }
        private void CloneGenericCollection(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly collections
            object targetValue = accessor.HasSetter
              ? CreateTargetValue(accessor, originalValue, target)
              : accessor.GetValue(target);

            if (targetValue == null)
                return;

            var sourceList = originalValue as IEnumerable;
            if (sourceList == null)
                return;

            // dynamic wrapper to call generic Add method
            dynamic targetList = new DynamicProxy(targetValue);
            
            foreach (var sourceItem in sourceList)
            {
                var cloneItem = CloneInstance(sourceItem);
                targetList.Add(cloneItem);
            }

            if (!accessor.HasSetter)
                return;

            accessor.SetValue(target, targetValue);
        }