예제 #1
0
        public void CreateIsReadOnlyMethod_ClassWithDynamicReadOnlyProperty_ReturnResultFromValidationMethod(bool isReadOnly)
        {
            // Setup
            var o = new ClassWithDynamicReadOnlyProperty(isReadOnly);

            // Call
            DynamicReadOnlyValidationMethodAttribute.IsPropertyReadOnly result = DynamicReadOnlyValidationMethodAttribute.CreateIsReadOnlyMethod(o);

            // Assert
            Assert.AreEqual(isReadOnly, result("Property"));
        }
예제 #2
0
        /// <summary>
        /// Determines whether the property is read-only or not.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="propertyName">The name of the property of <paramref name="obj"/>.</param>
        /// <returns><c>true</c> if the property is read-only, <c>false</c> otherwise.</returns>
        /// <exception cref="MissingMemberException">Thrown when <paramref name="propertyName"/>
        /// does not correspond to a public property of <paramref name="obj"/>.</exception>
        /// <exception cref="MissingMethodException">Thrown when there isn't a single method
        /// declared on <paramref name="obj"/> marked with <see cref="DynamicReadOnlyValidationMethodAttribute"/>
        /// that is matching the signature defined by <see cref="DynamicReadOnlyValidationMethodAttribute.IsPropertyReadOnly"/>.</exception>
        public static bool IsReadOnly(object obj, string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return(ReadOnlyAttribute.Default.IsReadOnly);
            }

            if (!IsPropertyDynamicallyReadOnly(obj, propertyName))
            {
                return(ReadOnlyAttribute.Default.IsReadOnly);
            }

            DynamicReadOnlyValidationMethodAttribute.IsPropertyReadOnly isPropertyReadOnlyDelegate = DynamicReadOnlyValidationMethodAttribute.CreateIsReadOnlyMethod(obj);
            return(isPropertyReadOnlyDelegate(propertyName));
        }