示例#1
1
        public void ReadOnlyProperty_instance_setter()
        {
            var instance = new TestClass();
            dynamic expando = new Expando(instance);

            expando.ReadOnlyFullName = "John Doe";
        }
示例#2
0
        public void PublicProperty_default()
        {
            dynamic expando = new Expando();

            expando.Property = "value";
            Assert.AreEqual("value", expando.Property);
        }
示例#3
0
        public void ReadOnlyProperty_instance_getter()
        {
            var instance = new TestClass();
            dynamic expando = new Expando(instance);

            expando.Name = "John";
            Assert.AreEqual(" John", expando.ReadOnlyFullName);
            Assert.AreEqual(" John", instance.ReadOnlyFullName);
        }
示例#4
0
        public void PublicProperty_instance()
        {
            var instance = new TestClass();
            dynamic expando = new Expando(instance);

            expando.Name = "John";
            Assert.AreEqual("John", expando.Name);
            Assert.AreEqual("John", instance.Name);
        }
示例#5
0
        public void PrivateProperty_instance_getter()
        {
            var instance = new TestClass();
            dynamic expando = new Expando(instance);

            Assert.Throws<MemberAccessException>(() => { var value = expando.PrivateAge; });
        }
示例#6
0
        public void ReadOnlyProperty_instance_setter()
        {
            var instance = new TestClass();
            dynamic expando = new Expando(instance);

            Assert.Throws<MemberAccessException>(() => expando.ReadOnlyFullName = "John Doe");
        }
示例#7
0
 public void Constructor_instance()
 {
     var instance = new TestClass();
     dynamic expando = new Expando(instance);
 }
示例#8
0
 public void Constructor_default()
 {
     dynamic expando = new Expando();
 }
示例#9
0
        public bool TryInvokeMember_Method_with_instance(int age)
        {
            var instance = new DerivedExpando();
            instance.Age = age;
            dynamic expando = new Expando(instance);

            var isOld = expando.IsOld();
            return isOld;
        }
示例#10
0
        public async Task Dynamic_Property_non_concurrent()
        {
            dynamic expando = new Expando();
            Exception exception = null;
            Action accessor = () =>
            {
                try
                {
                    for (var i = 0; i <= 2000; i++)
                    {
                        expando["Prop" + Thread.CurrentThread.Name + i] = Thread.CurrentThread.Name + i;
                    }
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
            };

            var tasks = new List<Task>();
            for (var j = 0; j < 200; j++)
            {
                tasks.Add(Task.Factory.StartNew(accessor));
            }

            // normally it should crash
            await Task.WhenAll(tasks);

            if (exception == null)
            {
                Assert.Inconclusive("If the inner dictionary did not crash it doesn't mean it is thread safe.");
            }
        }
示例#11
0
        public async Task Dynamic_Property_concurrent()
        {
            dynamic expando = new Expando(isThreadSafe: true);
            Action accessor = () =>
                {
                    for (var i = 0; i <= 2000; i++)
                    {
                        expando["Prop" + Thread.CurrentThread.Name + i] = Thread.CurrentThread.Name + i;
                    }
                };

            var tasks = new List<Task>();
            for (var j = 0; j < 200; j++)
            {
                tasks.Add(Task.Factory.StartNew(accessor));    
            }

            await Task.WhenAll(tasks);
        }
示例#12
0
        public void Dynamic_Property(object age)
        {
            dynamic expando = new Expando();
            expando.Age = age;

            Assert.AreEqual(age, expando.Age);
        }
示例#13
0
        public void TryInvokeMember_Func_property_with_instance()
        {
            dynamic instance = new Expando(new TestClass());
            instance.GetName = (Func<int, string>)(age => $"John Doe: {age}");

            var name = instance.GetName(30);
            Assert.AreEqual("John Doe: 30", name);
        }
示例#14
0
        public void PrivateProperty_instance_setter()
        {
            var instance = new TestClass();
            dynamic expando = new Expando(instance);

            Assert.Throws<MemberAccessException>(() => expando.PrivateAge = "John Doe");
        }
示例#15
0
        public void PrivateProperty_instance_getter()
        {
            var instance = new TestClass();
            dynamic expando = new Expando(instance);

            var age = expando.PrivateAge;
        }
示例#16
0
        public void PrivateProperty_instance_setter()
        {
            var instance = new TestClass();
            dynamic expando = new Expando(instance);

            expando.PrivateAge = "John Doe";
        }