/// <summary> /// 将实体类集合转换为DataTable /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="collection">实体类集合</param> /// <param name="tableName">表名</param> /// <param name="preclusiveColumnNames">排除的列名</param> /// <returns></returns> public static DataTable GetDataTable <T>(this IEnumerable <T> collection, string tableName, params string[] preclusiveColumnNames) { PropertyAccessorFactory factory = new PropertyAccessorFactory(); PropertyInfo[] properties = typeof(T).GetProperties().Where(o => !preclusiveColumnNames.Contains(o.Name)).ToArray(); DataTable dt = new DataTable(); dt.TableName = tableName; foreach (PropertyInfo property in properties) { Type propertyType = property.PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().Equals(typeof(Nullable <>))) { dt.Columns.Add(property.Name, Nullable.GetUnderlyingType(propertyType)); } else { dt.Columns.Add(property.Name, propertyType); } } IPropertyAccessor[] accessors = properties.Select(o => factory.Get(o)).ToArray(); foreach (T data in collection) { DataRow dr = dt.NewRow(); foreach (IPropertyAccessor accessor in accessors) { dr[accessor.Property.Name] = accessor.GetValue(data) ?? DBNull.Value; } dt.Rows.Add(dr); } return(dt); }
public AliasToBeanResultTransformer(System.Type resultClass) { if (resultClass == null) { throw new ArgumentNullException("resultClass"); } this.resultClass = resultClass; constructor = resultClass.GetConstructor(flags, null, System.Type.EmptyTypes, null); // if resultClass is a ValueType (struct), GetConstructor will return null... // in that case, we'll use Activator.CreateInstance instead of the ConstructorInfo to create instances if (constructor == null && resultClass.IsClass) { throw new ArgumentException("The target class of a AliasToBeanResultTransformer need a parameter-less constructor", "resultClass"); } propertyAccessor = new ChainedPropertyAccessor(new[] { PropertyAccessorFactory.GetPropertyAccessor(null), PropertyAccessorFactory.GetPropertyAccessor("field") }); }
public void GetValue() { var accessor = PropertyAccessorFactory.GetPropertyAccessor("readonly"); var getter = accessor.GetGetter(typeof(Calculation), "Sum"); Assert.That(getter.Get(new Calculation()), Is.EqualTo(2)); }
private static IPropertyGetter <TInput, TProperty> GetGetter <TProperty>(string propertyName) { PropertyInfo property = typeof(TInput).GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public); if (property == null || !property.CanRead || !property.HasPublicGetMethod()) { return(null); } if (property.PropertyType != typeof(TProperty)) { string message = String.Format(CultureInfo.CurrentCulture, "If the {0} property is present, it must be a {1}.", propertyName, typeof(TProperty).Name); throw new InvalidOperationException(message); } if (property.GetIndexParameters().Length != 0) { string message = String.Format(CultureInfo.CurrentCulture, "If the {0} property is present, it must not be an indexer.", propertyName); throw new InvalidOperationException(message); } return(PropertyAccessorFactory <TInput> .CreateGetter <TProperty>(property)); }
public PocoComponentTuplizer(Mapping.Component component) : base(component) { componentClass = component.ComponentClass; string parentPropertyName = component.ParentProperty; if (parentPropertyName == null) { parentSetter = null; parentGetter = null; } else { IPropertyAccessor pa = PropertyAccessorFactory.GetPropertyAccessor(null); parentSetter = pa.GetSetter(componentClass, parentPropertyName); parentGetter = pa.GetGetter(componentClass, parentPropertyName); } if (hasCustomAccessors || !Cfg.Environment.UseReflectionOptimizer) { optimizer = null; } else { optimizer = Cfg.Environment.BytecodeProvider.GetReflectionOptimizer(componentClass, getters, setters); } }
static FastReflectionFactories() { MethodInvokerFactory = new MethodInvokerFactory(); PropertyAccessorFactory = new PropertyAccessorFactory(); FieldAccessorFactory = new FieldAccessorFactory(); ConstructorInvokerFactory = new ConstructorInvokerFactory(); }
/// <summary> /// Finds the <see cref="IGetter"/> for the property in the <see cref="System.Type"/>. /// </summary> /// <param name="theClass"></param> /// <param name="propertyName"></param> /// <param name="propertyAccessorName"></param> /// <returns></returns> /// <remarks> /// This one takes a propertyAccessor name as we might know the correct strategy by now so we avoid Exceptions which are costly /// </remarks> public static IGetter GetGetter(System.Type theClass, string propertyName, string propertyAccessorName) { IPropertyAccessor accessor = null; accessor = PropertyAccessorFactory.GetPropertyAccessor(propertyAccessorName); return(accessor.GetGetter(theClass, propertyName)); }
public void StringTextExtraction() { Property property = GenerateTextProperty(); IGetter getter = PropertyAccessorFactory.GetPropertyAccessor(property, EntityMode.Xml).GetGetter(null, null); var name = (string)getter.Get(dom); Assert.That(name, Is.EqualTo("description...")); }
public override void SetUp() { _accessor = PropertyAccessorFactory.GetPropertyAccessor("field.pascalcase-m-underscore"); _getter = _accessor.GetGetter(typeof(FieldClass), "Blah"); _setter = _accessor.GetSetter(typeof(FieldClass), "Blah"); _instance = new FieldClass(); _instance.InitBlah(0); }
public virtual void SetUp() { _accessor = PropertyAccessorFactory.GetPropertyAccessor("field"); _getter = _accessor.GetGetter(typeof(FieldClass), "Id"); _setter = _accessor.GetSetter(typeof(FieldClass), "Id"); _instance = new FieldClass(); _instance.InitId(0); }
private static IPropertySetter <TOutput, EntityProperty> GetOtherSetterGeneric <TProperty>(PropertyInfo property) { IConverter <EntityProperty, TProperty> converter = EntityPropertyToTConverterFactory.Create <TProperty>(); IPropertySetter <TOutput, TProperty> propertySetter = PropertyAccessorFactory <TOutput> .CreateSetter <TProperty>(property); return(new ConverterPropertySetter <TOutput, TProperty, EntityProperty>(converter, propertySetter)); }
public void LongElementAttributeExtraction() { Property property = GenerateAccountIdProperty(); IGetter getter = PropertyAccessorFactory.GetPropertyAccessor(property, EntityMode.Xml).GetGetter(null, null); var id = (long)getter.Get(dom); Assert.That(id, Is.EqualTo(456L)); }
public override void SetUp() { _accessor = PropertyAccessorFactory.GetPropertyAccessor("field.camelcase-m-underscore"); _getter = _accessor.GetGetter(typeof(FieldClass), "CamelMUnderscore"); _setter = _accessor.GetSetter(typeof(FieldClass), "CamelMUnderscore"); _instance = new FieldClass(); _instance.InitCamelCaseMUnderscore(0); }
public override void SetUp() { _accessor = PropertyAccessorFactory.GetPropertyAccessor("field.lowercase-underscore"); _getter = _accessor.GetGetter(typeof(FieldClass), "LowerUnderscoreFoo"); _setter = _accessor.GetSetter(typeof(FieldClass), "LowerUnderscoreFoo"); _instance = new FieldClass(); _instance.InitLowerUnderscoreFoo(0); }
public void StringElementExtraction() { Property property = GenerateNameProperty(); IGetter getter = PropertyAccessorFactory.GetPropertyAccessor(property, EntityMode.Xml).GetGetter(null, null); var name = (string)getter.Get(dom); Assert.That(name, Is.EqualTo("NHForge")); }
public override void SetUp() { _expectedCamelBazGetterCalled = true; _accessor = PropertyAccessorFactory.GetPropertyAccessor("nosetter.camelcase"); _getter = _accessor.GetGetter(typeof(FieldClass), "CamelBaz"); _setter = _accessor.GetSetter(typeof(FieldClass), "CamelBaz"); _instance = new FieldClass(); _instance.InitCamelBaz(0); }
private static IPropertyGetter <TInput, EntityProperty> GetOtherGetterGeneric <TProperty>(PropertyInfo property) { IPropertyGetter <TInput, TProperty> propertyGetter = PropertyAccessorFactory <TInput> .CreateGetter <TProperty>(property); IConverter <TProperty, EntityProperty> converter = TToEntityPropertyConverterFactory.Create <TProperty>(); return(new ConverterPropertyGetter <TInput, TProperty, EntityProperty>(propertyGetter, converter)); }
protected BaseDistinctRootColumnResultTransformer() { resultClass = typeof(TDto); propertyAccessor = new ChainedPropertyAccessor(new[] { PropertyAccessorFactory.GetPropertyAccessor(null), PropertyAccessorFactory.GetPropertyAccessor("field") }); }
public override void SetUp() { _expectedLowerFooGetterCalled = true; _accessor = PropertyAccessorFactory.GetPropertyAccessor("nosetter.lowercase"); _getter = _accessor.GetGetter(typeof(FieldClass), "LowerFoo"); _setter = _accessor.GetSetter(typeof(FieldClass), "LowerFoo"); _instance = new FieldClass(); _instance.InitLowerFoo(0); }
public override void SetUp() { _expectedPascalUnderscoreFooGetterCalled = true; _accessor = PropertyAccessorFactory.GetPropertyAccessor("nosetter.pascalcase-underscore"); _getter = _accessor.GetGetter(typeof(FieldClass), "PascalUnderscoreFoo"); _setter = _accessor.GetSetter(typeof(FieldClass), "PascalUnderscoreFoo"); _instance = new FieldClass(); _instance.InitPascalUnderscoreFoo(0); }
public void CreateSetter_IfClass_ReturnsInstance() { // Arrange PropertyInfo propertyInfo = typeof(Poco).GetProperty("Value"); // Act IPropertySetter <Poco, string> manager = PropertyAccessorFactory <Poco> .CreateSetter <string>(propertyInfo); // Assert Assert.NotNull(manager); }
private static IGetter GetGetter(Mapping.Property mappingProperty) { if (mappingProperty == null || !mappingProperty.PersistentClass.HasPocoRepresentation) { return(null); } IPropertyAccessor pa = PropertyAccessorFactory.GetPropertyAccessor(mappingProperty, EntityMode.Poco); return(pa.GetGetter(mappingProperty.PersistentClass.MappedClass, mappingProperty.Name)); }
public Configuration() { IdentifierAccessorFactory = new PropertyAccessorFactory("Id"); VersionAccessorFactory = new PropertyAccessorFactory("Version"); ContentSerializer = new JsonContentSerializer(); IdGenerator = new DefaultIdGenerator(); IsolationLevel = IsolationLevel.ReadCommitted; TablePrefix = ""; CommandsPageSize = 500; QueryGatingEnabled = true; Logger = NullLogger.Instance; ConcurrentTypes = new HashSet <Type>(); TableNameConvention = new DefaultTableNameConvention(); }
public void SetUp() { _accessor = PropertyAccessorFactory.GetPropertyAccessor(AccessorType); _getters = new IGetter[PropertyNames.Count]; _setters = new ISetter[PropertyNames.Count]; var type = typeof(T); for (var i = 0; i < PropertyNames.Count; i++) { _getters[i] = _accessor.GetGetter(type, PropertyNames[i]); _setters[i] = _accessor.GetSetter(type, PropertyNames[i]); } _optimizer = new ReflectionOptimizer(type, _getters, _setters, null, null).AccessOptimizer; }
public void GetValue() { var accessor = PropertyAccessorFactory.GetPropertyAccessor("backfield"); var getter = accessor.GetGetter(typeof(MyAutoProp), "AutoProp"); var rogetter = accessor.GetGetter(typeof(MyAutoProp), "ReadOnlyAutoProp"); Assert.That(getter.Get(new MyAutoProp { AutoProp = -1 }), Is.EqualTo(-1)); Assert.That(getter.Get(new MyAutoProp { AutoProp = 1 }), Is.EqualTo(1)); Assert.That(rogetter.Get(new MyAutoProp()), Is.EqualTo(0)); Assert.That(rogetter.Get(new MyAutoProp(5)), Is.EqualTo(5)); }
/// <summary> /// /// </summary> /// <param name="componentClass"></param> /// <param name="propertyNames"></param> /// <param name="propertyGetters"></param> /// <param name="propertySetters"></param> /// <param name="foundCustomAcessor"></param> /// <param name="propertyTypes"></param> /// <param name="joinedFetch"></param> /// <param name="cascade"></param> /// <param name="parentProperty"></param> public ComponentType(System.Type componentClass, string[] propertyNames, IGetter[] propertyGetters, ISetter[] propertySetters, // currently not used, see the comment near the end of the method body bool foundCustomAcessor, IType[] propertyTypes, OuterJoinFetchStrategy[] joinedFetch, Cascades.CascadeStyle[] cascade, string parentProperty) { this.componentClass = componentClass; this.propertyTypes = propertyTypes; propertySpan = propertyNames.Length; getters = propertyGetters; setters = propertySetters; string[] getterNames = new string[propertySpan]; string[] setterNames = new string[propertySpan]; System.Type[] propTypes = new System.Type[propertySpan]; for (int i = 0; i < propertySpan; i++) { getterNames[i] = getters[i].PropertyName; setterNames[i] = setters[i].PropertyName; propTypes[i] = getters[i].ReturnType; } if (parentProperty == null) { parentSetter = null; parentGetter = null; } else { IPropertyAccessor pa = PropertyAccessorFactory.GetPropertyAccessor(null); parentSetter = pa.GetSetter(componentClass, parentProperty); parentGetter = pa.GetGetter(componentClass, parentProperty); } this.propertyNames = propertyNames; this.cascade = cascade; this.joinedFetch = joinedFetch; // NH: reflection optimizer works with custom accessors if (/*!foundCustomAcessor &&*/ Cfg.Environment.UseReflectionOptimizer) { this.getset = GetSetHelperFactory.Create(componentClass, setters, getters); } }
private static void AddSubElement(Property property, ValidatableElement element) { if (property != null && property.IsComposite && !property.BackRef) { Component component = (Component)property.Value; if (component.IsEmbedded) { return; } if (property.PersistentClass != null) { var cv = Engine.GetClassValidator(property.PersistentClass.MappedClass); if (cv != null) { if (cv.GetMemberConstraints(property.Name).OfType <ValidAttribute>().Any()) { // the components is already marked as Valid return; } } } IPropertyAccessor accesor = PropertyAccessorFactory.GetPropertyAccessor(property, EntityMode.Poco); IGetter getter = accesor.GetGetter(element.EntityType, property.Name); IClassValidator validator = Engine.GetClassValidator(getter.ReturnType); if (validator != null) { ValidatableElement subElement = new ValidatableElement(getter.ReturnType, validator, getter); foreach (Property currentProperty in component.PropertyIterator) { AddSubElement(currentProperty, subElement); } if (subElement.HasSubElements || subElement.Validator.HasValidationRules) { element.AddSubElement(subElement); } } } }
/// <summary> /// Initializes a new instance of the PositionalToBeanResultTransformer class. /// </summary> /// <param name="resultClass">The return <see cref="Type"/>.</param> /// <param name="positionalAliases">Alias for each position of the query.</param> public PositionalToBeanResultTransformer(Type resultClass, string[] positionalAliases) { if (resultClass == null) { throw new ArgumentNullException("resultClass"); } this.resultClass = resultClass; if (positionalAliases == null || positionalAliases.Length == 0) { throw new ArgumentNullException("positionalAliases"); } this.positionalAliases = positionalAliases; propertyAccessor = new ChainedPropertyAccessor(new[] { PropertyAccessorFactory.GetPropertyAccessor("field"), PropertyAccessorFactory.GetPropertyAccessor(null) }); AssignSetters(); }
public void SetValue() { var accessor = PropertyAccessorFactory.GetPropertyAccessor("backfield"); var getter = accessor.GetGetter(typeof(MyAutoProp), "AutoProp"); var setter = accessor.GetSetter(typeof(MyAutoProp), "AutoProp"); var rogetter = accessor.GetGetter(typeof(MyAutoProp), "ReadOnlyAutoProp"); var rosetter = accessor.GetSetter(typeof(MyAutoProp), "ReadOnlyAutoProp"); var i = new MyAutoProp { AutoProp = -1 }; Assert.That(getter.Get(i), Is.EqualTo(-1)); setter.Set(i, 5); Assert.That(getter.Get(i), Is.EqualTo(5)); Assert.That(rogetter.Get(new MyAutoProp()), Is.EqualTo(0)); rosetter.Set(i, 123); Assert.That(rogetter.Get(i), Is.EqualTo(123)); }
public void CompanyElementGeneration() { ISetter idSetter = PropertyAccessorFactory.GetPropertyAccessor(GenerateIdProperty(), EntityMode.Xml).GetSetter(null, null); ISetter nameSetter = PropertyAccessorFactory.GetPropertyAccessor(GenerateNameProperty(), EntityMode.Xml).GetSetter(null, null); ISetter textSetter = PropertyAccessorFactory.GetPropertyAccessor(GenerateTextProperty(), EntityMode.Xml).GetSetter(null, null); ISetter accountIdSetter = PropertyAccessorFactory.GetPropertyAccessor(GenerateAccountIdProperty(), EntityMode.Xml).GetSetter(null, null); XmlNode root = GenerateRootTestElement(); idSetter.Set(root, 123L); textSetter.Set(root, "description..."); nameSetter.Set(root, "NHForge"); accountIdSetter.Set(root, 456L); Console.WriteLine(dom.OuterXml); //Assert.That(new NodeComparator().Compare(dom, root) == 0); }
public void CreateTest() { PropertyAccessorFactory target = new PropertyAccessorFactory(); PropertyInfo[] myPropertyInfo; Type myType = typeof(MyClass); // Get the type and fields of FieldInfoClass. myPropertyInfo = myType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); PropertyInfo key = myPropertyInfo[0]; IPropertyAccessor actual; actual = target.Create(key); Assert.IsNotNull(actual); }
/// <summary> /// Finds the <see cref="IGetter"/> for the property in the <see cref="System.Type"/>. /// </summary> /// <param name="theClass">The <see cref="System.Type"/> to find the property in.</param> /// <param name="propertyName">The name of the Property to find.</param> /// <param name="propertyAccessorName">The name of the property access strategy.</param> /// <returns>The <see cref="IGetter"/> to get the value of the Property.</returns> /// <remarks> /// This one takes a propertyAccessor name as we might know the correct strategy by now so we avoid Exceptions which are costly /// </remarks> public static IGetter GetGetter(System.Type theClass, string propertyName, string propertyAccessorName) { return(PropertyAccessorFactory .GetPropertyAccessor(propertyAccessorName) .GetGetter(theClass, propertyName)); }
public void PropertyAccessorFactoryConstructorTest() { PropertyAccessorFactory target = new PropertyAccessorFactory(); Assert.IsNotNull(target); }