/// <summary> /// Initializes the static members in <see cref="PropertyAccessorFactory"/>. /// </summary> static PropertyAccessorFactory() { accessors = new Dictionary<string, IPropertyAccessor>(19); accessors["property"] = new BasicPropertyAccessor(); accessors["field"] = new FieldAccessor(); accessors["backfield"] = new FieldAccessor(new BackFieldStrategy()); accessors["readonly"] = new ReadOnlyAccessor(); accessors["field.camelcase"] = new FieldAccessor(new CamelCaseStrategy()); accessors["field.camelcase-underscore"] = new FieldAccessor(new CamelCaseUnderscoreStrategy()); accessors["field.lowercase"] = new FieldAccessor(new LowerCaseStrategy()); accessors["field.lowercase-underscore"] = new FieldAccessor(new LowerCaseUnderscoreStrategy()); accessors["field.pascalcase-underscore"] = new FieldAccessor(new PascalCaseUnderscoreStrategy()); accessors["field.pascalcase-m-underscore"] = new FieldAccessor(new PascalCaseMUnderscoreStrategy()); accessors["field.pascalcase-m"] = new FieldAccessor(new PascalCaseMStrategy()); accessors["nosetter.camelcase"] = new NoSetterAccessor(new CamelCaseStrategy()); accessors["nosetter.camelcase-underscore"] = new NoSetterAccessor(new CamelCaseUnderscoreStrategy()); accessors["nosetter.lowercase"] = new NoSetterAccessor(new LowerCaseStrategy()); accessors["nosetter.lowercase-underscore"] = new NoSetterAccessor(new LowerCaseUnderscoreStrategy()); accessors["nosetter.pascalcase-underscore"] = new NoSetterAccessor(new PascalCaseUnderscoreStrategy()); accessors["nosetter.pascalcase-m-underscore"] = new NoSetterAccessor(new PascalCaseMUnderscoreStrategy()); accessors["nosetter.pascalcase-m"] = new NoSetterAccessor(new PascalCaseMStrategy()); accessors["embedded"] = new EmbeddedPropertyAccessor(); accessors["noop"] = new NoopAccessor(); accessors["none"] = new NoopAccessor(); }
/// <summary> /// Initializes the static members in <see cref="PropertyAccessorFactory"/>. /// </summary> static PropertyAccessorFactory() { accessors = new Dictionary <string, IPropertyAccessor>(19); accessors["property"] = new BasicPropertyAccessor(); accessors["field"] = new FieldAccessor(); accessors["backfield"] = new FieldAccessor(new BackFieldStrategy()); accessors["readonly"] = new ReadOnlyAccessor(); accessors["field.camelcase"] = new FieldAccessor(new CamelCaseStrategy()); accessors["field.camelcase-underscore"] = new FieldAccessor(new CamelCaseUnderscoreStrategy()); accessors["field.camelcase-m-underscore"] = new FieldAccessor(new CamelCaseMUnderscoreStrategy()); accessors["field.lowercase"] = new FieldAccessor(new LowerCaseStrategy()); accessors["field.lowercase-underscore"] = new FieldAccessor(new LowerCaseUnderscoreStrategy()); accessors["field.pascalcase-underscore"] = new FieldAccessor(new PascalCaseUnderscoreStrategy()); accessors["field.pascalcase-m-underscore"] = new FieldAccessor(new PascalCaseMUnderscoreStrategy()); accessors["field.pascalcase-m"] = new FieldAccessor(new PascalCaseMStrategy()); accessors["nosetter.camelcase"] = new NoSetterAccessor(new CamelCaseStrategy()); accessors["nosetter.camelcase-underscore"] = new NoSetterAccessor(new CamelCaseUnderscoreStrategy()); accessors["nosetter.camelcase-m-underscore"] = new NoSetterAccessor(new CamelCaseMUnderscoreStrategy()); accessors["nosetter.lowercase"] = new NoSetterAccessor(new LowerCaseStrategy()); accessors["nosetter.lowercase-underscore"] = new NoSetterAccessor(new LowerCaseUnderscoreStrategy()); accessors["nosetter.pascalcase-underscore"] = new NoSetterAccessor(new PascalCaseUnderscoreStrategy()); accessors["nosetter.pascalcase-m-underscore"] = new NoSetterAccessor(new PascalCaseMUnderscoreStrategy()); accessors["nosetter.pascalcase-m"] = new NoSetterAccessor(new PascalCaseMStrategy()); accessors["embedded"] = new EmbeddedPropertyAccessor(); accessors["noop"] = new NoopAccessor(); accessors["none"] = new NoopAccessor(); }
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); }
/// <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); }
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); }
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]; }
private void MakeCollectionValidation(IEnumerable value, object entity, MemberInfo member, ISet circularityState, ICollection<InvalidValue> results) { if (TypeUtils.IsGenericDictionary(value.GetType())) //Generic Dictionary { foreach (object item in value) { IGetter ValueProperty = new BasicPropertyAccessor().GetGetter(item.GetType(), "Value"); IGetter KeyProperty = new BasicPropertyAccessor().GetGetter(item.GetType(), "Key"); object valueValue = ValueProperty.Get(item); object keyValue = KeyProperty.Get(item); string indexedPropName = string.Format("{0}[{1}]", member.Name, keyValue); if (ShouldNeedValidation(ValueProperty.ReturnType)) { InvalidValue[] invalidValuesKey = GetClassValidator(ValueProperty.ReturnType).GetInvalidValues(valueValue, circularityState); foreach (InvalidValue invalidValue in invalidValuesKey) { invalidValue.AddParentEntity(entity, indexedPropName); results.Add(invalidValue); } } if (ShouldNeedValidation(KeyProperty.ReturnType)) { InvalidValue[] invalidValuesValue = GetClassValidator(KeyProperty.ReturnType).GetInvalidValues(keyValue, circularityState); foreach (InvalidValue invalidValue in invalidValuesValue) { invalidValue.AddParentEntity(entity, indexedPropName); results.Add(invalidValue); } } } } else //Generic collection { int index = 0; foreach (object item in value) { if (item == null) { index++; continue; } System.Type itemType = item.GetType(); if (ShouldNeedValidation(itemType)) { InvalidValue[] invalidValues = GetClassValidator(itemType).GetInvalidValues(item, circularityState); String indexedPropName = string.Format("{0}[{1}]", member.Name, index); foreach (InvalidValue invalidValue in invalidValues) { invalidValue.AddParentEntity(entity, indexedPropName); results.Add(invalidValue); } } index++; } } }