Пример #1
0
        public void TestSetIncompatibleType()
        {
            IDynamicField inventorPlace = Create(typeof(Inventor).GetField("pob", BINDANY));

            try { inventorPlace.SetValue(new Inventor(), new object()); Assert.Fail(); }
            catch (InvalidCastException) { }
            try { inventorPlace.SetValue(new Inventor(), new DateTime()); Assert.Fail(); }
            catch (InvalidCastException) { }

            IDynamicField inventorDOB = Create(typeof(Inventor).GetField("dob", BINDANY));

            try { inventorDOB.SetValue(new Inventor(), 2); Assert.Fail(); }
            catch (InvalidCastException) { }
            try { inventorDOB.SetValue(new Inventor(), new Place()); Assert.Fail(); }
            catch (InvalidCastException) { }
        }
Пример #2
0
        public void TestStaticFieldsOfStruct()
        {
            // static readonly
            IDynamicField maxValue = Create(typeof(DateTime).GetField("MaxValue"));

            Assert.AreEqual(DateTime.MaxValue, maxValue.GetValue(null));
            try
            {
                maxValue.SetValue(null, DateTime.Now);
            }
            catch (InvalidOperationException) { }

            // const
            IDynamicField int64max = Create(typeof(Int64).GetField("MaxValue", BindingFlags.Public | BindingFlags.Static));

            Assert.AreEqual(Int64.MaxValue, int64max.GetValue(null));
            try
            {
                int64max.SetValue(null, 0);
            }
            catch (InvalidOperationException) { }

            // pure static
            IDynamicField myField = Create(typeof(MyStaticStruct).GetField("staticYear"));

            myField.SetValue(null, 2008);
            Assert.AreEqual(2008, myField.GetValue(null));
        }
Пример #3
0
        public void TestAttemptingToSetFieldOfValueTypeInstance()
        {
            MyStruct      myYearHolder = new MyStruct();
            IDynamicField year         = Create(typeof(MyStruct).GetField("year"));

            year.SetValue(myYearHolder, 2004);
        }
Пример #4
0
        public void TestAttemptingToSetFieldOfValueTypeInstance()
        {
            MyStruct      myYearHolder = new MyStruct();
            IDynamicField year         = Create(typeof(MyStruct).GetField("year"));

            Assert.Throws <InvalidOperationException>(() => year.SetValue(myYearHolder, 2004));
        }
Пример #5
0
        /// <summary>
        /// Gets the value of the dynamic field for the specified target object.
        /// </summary>
        /// <param name="target">
        /// Target object to set field value on.
        /// </param>
        /// <param name="value">
        /// A new field value.
        /// </param>
        public void SetValue(object target, object value)
        {
            if (isOptimizedSet)
            {
                try
                {
                    dynamicField.SetValue(target, value);
                    return;
                }
                catch (InvalidCastException)
                {
                    isOptimizedSet = false;
                }
            }

            if (!canSet)
            {
                throw new InvalidOperationException("Cannot set value of a read-only field or a constant.");
            }

            try
            {
                fieldInfo.SetValue(target, value);
            }
            catch (ArgumentException)
            {
                if (value != null && !fieldInfo.GetType().IsAssignableFrom(value.GetType()))
                {
                    throw new InvalidCastException();
                }
                throw;
            }
        }
Пример #6
0
        public void CannotSetStaticReadOnlyField()
        {
            IDynamicField myReadonlyField = Create(typeof(MyStaticClass).GetField("myReadonlyField"));

            try
            {
                myReadonlyField.SetValue(null, "some other string");
                Assert.Fail();
            }
            catch (InvalidOperationException) { }
        }
Пример #7
0
        public void TestInstanceFields()
        {
            IDynamicField name = Create(typeof(Inventor).GetField("Name"));

            Assert.AreEqual(tesla.Name, name.GetValue(tesla));
            name.SetValue(tesla, "Tesla, Nikola");
            Assert.AreEqual("Tesla, Nikola", tesla.Name);
            Assert.AreEqual("Tesla, Nikola", name.GetValue(tesla));

            MyStruct myYearHolder = new MyStruct();

            myYearHolder.Year = 2004;
            IDynamicField year = Create(typeof(MyStruct).GetField("year", BINDANY));

            Assert.AreEqual(2004, year.GetValue(myYearHolder));
        }
Пример #8
0
        public void TestStaticFieldsOfClass()
        {
            IDynamicField myField = Create(typeof(MyStaticClass).GetField("myField"));

            myField.SetValue(null, "here we go...");

            IDynamicField myConst = Create(typeof(MyStaticClass).GetField("MyConst"));

            Assert.AreEqual(3456, myConst.GetValue(null));
            try
            {
                myConst.SetValue(null, 7890);
            }
            catch (InvalidOperationException) { }

            IDynamicField myReadonlyField = Create(typeof(MyStaticClass).GetField("myReadonlyField"));
            string        s2 = (string)myReadonlyField.GetValue(null);
            string        s1 = MyStaticClass.myReadonlyField;

            Assert.AreEqual(s1, s2);
        }
		private void SetChildControlCollection(ControlCollection controls)
		{
            fControls.SetValue(_targetControl, controls);
		}