public static IComparable GetPropertyValue(object obj, string propertyName)
        {
            //TODO respect the client's choice in how Hibernate accesses property values.
            IGetter getter = new BasicPropertyAccessor().GetGetter(obj.GetType(), propertyName);

            return((IComparable)getter.Get(obj));
        }
示例#2
0
 /// <summary>
 /// Creates an <see cref="BasicPropertyAccessor.BasicGetter"/> to <c>get</c> the value from the Property.
 /// </summary>
 /// <param name="type">The <see cref="System.Type"/> to find the Property in.</param>
 /// <param name="propertyName">The name of the mapped Property to get.</param>
 /// <returns>
 /// The <see cref="BasicPropertyAccessor.BasicGetter"/> to use to get the value of the Property from an
 /// instance of the <see cref="System.Type"/>.</returns>
 /// <exception cref="PropertyNotFoundException" >
 /// Thrown when a Property specified by the <c>propertyName</c> could not
 /// be found in the <see cref="System.Type"/>.
 /// </exception>
 public IGetter GetGetter(System.Type type, string propertyName)
 {
     BasicPropertyAccessor.BasicGetter result = BasicPropertyAccessor.GetGetterOrNull(type, propertyName);
     if (result == null)
     {
         throw new PropertyNotFoundException(type, propertyName, "getter");
     }
     return(result);
 }
示例#3
0
        internal static IEnumerable <KeyValuePair <object, object> > AsKeyValue(this IEnumerable source)
        {
            var itemsIterator = source.GetEnumerator();

            if (itemsIterator.MoveNext())
            {
                IGetter valueProperty = new BasicPropertyAccessor().GetGetter(itemsIterator.Current.GetType(), "Value");
                IGetter keyProperty   = new BasicPropertyAccessor().GetGetter(itemsIterator.Current.GetType(), "Key");
                return(source.Cast <object>().Select(
                           item => new KeyValuePair <object, object>(keyProperty.Get(item), valueProperty.Get(item))));
            }
            return(new KeyValuePair <object, object> [0]);
        }
示例#4
0
        public void SubElements()
        {
            IGetter            getter = new BasicPropertyAccessor().GetGetter(typeof(AClass), "Address");
            ClassValidator     cvadd  = new ClassValidator(typeof(Address));
            ClassValidator     cv     = new ClassValidator(typeof(AClass));
            ValidatableElement ve     = new ValidatableElement(typeof(AClass), cv);

            try
            {
                ve.AddSubElement(new ValidatableElement(typeof(Address), cvadd));
                Assert.Fail("No exception adding a subelement without getter");
            }
            catch (ArgumentException)
            {
                //ok
            }
            Assert.IsFalse(ve.HasSubElements);
            ve.AddSubElement(new ValidatableElement(typeof(Address), cvadd, getter));
            Assert.IsTrue(ve.HasSubElements);
        }