public IMappingOperation[] GetMappingOperations(Type from, Type to) { var members = ReflectionUtils.GetPublicFieldsAndProperties(to); return(members .Select( m => new DestWriteOperation() { Destination = new MemberDescriptor(m), Getter = (ValueGetter <object>) ( (form, valueProviderObj) => { var valueProvider = valueProviderObj as Func <string, object>; if (valueProvider == null) { valueProvider = (k) => ((NameValueCollection)form)[k]; } var val = valueProvider(m.Name); if (val != null) { return ValueToWrite <object> .ReturnValue(val); } else { return ValueToWrite <object> .Skip(); } } ) } ) .ToArray()); }
public IMappingOperation[] GetMappingOperations(Type from, Type to) { var members = ReflectionUtils.GetPublicFieldsAndProperties(to); return(members .Select(m => new DestWriteOperation() { Destination = new MemberDescriptor(m), Getter = (ValueGetter <object>) ( (form, status) => { FormCollection forms = new FormCollection((NameValueCollection)form); IValueProvider valueProvider = forms.ToValueProvider(); ValueProviderResult res = valueProvider.GetValue(m.Name); if (res != null) { return ValueToWrite <object> .ReturnValue(res.ConvertTo(ReflectionUtils.GetMemberType(m))); } else { return ValueToWrite <object> .Skip(); } } ) } ) .ToArray()); }
private ValueGetter <T> GetValuesGetter(int ind, MemberInfo m) { var memberType = ReflectionUtils.GetMemberType(m); Func <object, object> converter = StaticConvertersManager.DefaultInstance.GetStaticConverterFunc(typeof(object), memberType); if (converter == null) { throw new EmitMapperException("Could not convert an object to " + memberType.ToString()); } int fieldNum = -1; return ((value, state) => { var reader = ((IDataReader)state); if (fieldNum == -1) { fieldNum = reader.GetOrdinal(m.Name); } object result = reader[fieldNum]; if (result is DBNull) { return ValueToWrite.ReturnValue(null); } return ValueToWrite.ReturnValue(converter(result)); }); }
/// <summary> /// 使用指定的方式来填充目标 /// </summary> /// <typeparam name="TFrom"></typeparam> /// <typeparam name="TTo"></typeparam> /// <param name="destinationMember"></param> /// <param name="sourceMember"></param> /// <returns></returns> public FlatteringConfig ResolveUsing <TFrom, TTo>(Expression <Func <TTo, object> > destinationMember, Func <TFrom, object> sourceMember) { AppendMappingOperations.Add(new DestWriteOperation() { Destination = new MemberDescriptor(ReflectionHelper.FindMember(destinationMember)), Getter = ((ValueGetter <object>)((from, state) => { return(ValueToWrite <object> .ReturnValue(sourceMember((TFrom)from))); })) }); return(this); }
/// <summary> /// Gets the mapping operations. /// </summary> /// <param name="from">The type from.</param> /// <param name="to">To type to.</param> /// <returns>The mapping operations.</returns> public override IEnumerable <IMappingOperation> GetMappingOperations(Type from, Type to) { return(FilterOperations( from, to, ReflectionHelper.GetTypeDataContainerDescription(to).Select( fieldsDescription => { var fieldName = fieldsDescription.Key; var destinationMember = fieldsDescription.Value.Item1; var fieldType = fieldsDescription.Value.Item2; return new DestWriteOperation { Destination = new MemberDescriptor(destinationMember), Getter = (ValueGetter <object>)((item, state) => { if (item is not DataContainer container) { return ValueToWrite <object> .Skip(); } if (container.Fields == null || !container.Fields.TryGetValue( fieldName, out var value)) { return ValueToWrite <object> .Skip(); } var destinationType = ReflectionHelper.GetMemberReturnType(destinationMember); var destinationMemberValue = ReflectionHelper.ConvertValue( value, fieldType, destinationType); return ValueToWrite <object> .ReturnValue( destinationMemberValue); }) }; }))); }
/// <summary> /// Gets the mapping operations. /// </summary> /// <param name="from">The type from.</param> /// <param name="to">To type to.</param> /// <returns>The mapping operations.</returns> public override IEnumerable <IMappingOperation> GetMappingOperations(Type from, Type to) { return(FilterOperations( from, to, ReflectionHelper.GetPublicFieldsAndProperties(to) .Where( member => (member.MemberType == MemberTypes.Field || member.MemberType == MemberTypes.Property) && ((PropertyInfo)member).GetSetMethod() != null).Select( destinationMember => (IMappingOperation) new DestWriteOperation { Destination = new MemberDescriptor(destinationMember), Getter = (ValueGetter <object>)((item, state) => { if (item is not DataContainer value) { return ValueToWrite <object> .Skip(); } var destinationType = ReflectionHelper.GetMemberReturnType( destinationMember); var fieldDescription = ReflectionHelper.GetDataMemberDefinition( destinationMember); var destinationMemberValue = ConvertFieldToDestinationProperty( value, destinationType, fieldDescription.FirstOrDefault()); return destinationMemberValue == null ? ValueToWrite <object> .Skip() : ValueToWrite <object> .ReturnValue( destinationMemberValue); }) })));
/// <summary> /// Gets the mapping operations. /// </summary> /// <param name="from">The from.</param> /// <param name="to">The to.</param> /// <returns><![CDATA[IEnumerable<IMappingOperation>]]></returns> public override IEnumerable <IMappingOperation> GetMappingOperations(Type from, Type to) { var members = ReflectionHelper.GetPublicFieldsAndProperties(to); return(members.Select( m => (IMappingOperation) new DestWriteOperation { Destination = new MemberDescriptor(m), Getter = (ValueGetter <object>)((form, valueProviderObj) => { if (((FormCollection)form).TryGetValue( m.Name, out var res)) { return ValueToWrite <object> .ReturnValue( Convert( new ValueProviderResult(res), ReflectionHelper.GetMemberReturnType(m))); } return ValueToWrite <object> .Skip(); }) }).ToArray());
public IMappingOperation[] GetMappingOperations(Type from, Type to) { var members = ReflectionUtils.GetPublicFieldsAndProperties(to); return(members .Select( m => new DestWriteOperation() { Destination = new MemberDescriptor(m), Getter = (ValueGetter <object>) ( (form, valueProviderObj) => { var valueProvider = valueProviderObj as IValueProvider; if (valueProvider == null) { valueProvider = ((FormCollection)form).ToValueProvider(); } ValueProviderResult res = valueProvider.GetValue(m.Name); // here need to check the value of res is not null, then can convert the value if (res != null) { return ValueToWrite <object> .ReturnValue(res.ConvertTo(ReflectionUtils.GetMemberType(m))); } else { return ValueToWrite <object> .Skip(); } } ) } ) .ToArray()); }
public IMappingOperation[] GetMappingOperations(Type from, Type to) { var members = ReflectionUtils.GetPublicFieldsAndProperties(to); return(members .Select( m => new DestWriteOperation() { Destination = new MemberDescriptor(m), Getter = (ValueGetter <object>) ( (form, valueProviderObj) => { var valueProvider = valueProviderObj as IDictionary <string, ValueProviderResult>; if (valueProvider == null) { valueProvider = ((FormCollection)form).ToValueProvider(); } ValueProviderResult res; if (valueProvider.TryGetValue(m.Name, out res)) { return ValueToWrite <object> .ReturnValue(res.ConvertTo(ReflectionUtils.GetMemberType(m))); } else { return ValueToWrite <object> .Skip(); } } ) } ) .ToArray()); }
public override IMappingOperation[] GetMappingOperations(Type from, Type to) { return (FilterOperations( from, to, ReflectionUtils .GetPublicFieldsAndProperties(to) .Select( m => (IMappingOperation) new DestWriteOperation { Destination = new MemberDescriptor(m), Getter = (ValueGetter <object>) ( (value, state) => ValueToWrite <object> .ReturnValue( ((IDictionary <string, object>)value)[m.Name] ) ) } ) ).ToArray()); }
private ValueToWrite <object> GetReturnValue(dynamic value, MemberInfo destMember) { Type type = value.GetType(); List <MemberInfo> _sourceMembers = GetSourceMemebers(type); var matchedChain = GetMatchedChain(destMember.Name, _sourceMembers); if (matchedChain == null || matchedChain.Count == 0) { return(ValueToWrite <object> .Skip()); } if (matchedChain[0] is PropertyInfo) { var val = (matchedChain[0] as PropertyInfo).GetValue(value, null); return(ValueToWrite <object> .ReturnValue(val)); } else if (matchedChain[0] is FieldInfo) { var val = (matchedChain[0] as FieldInfo).GetValue(value); return(ValueToWrite <object> .ReturnValue(val)); } return(ValueToWrite <object> .Skip()); }
private Delegate GetValuesGetter(int ind, MemberInfo m) { var memberType = ReflectionUtils.GetMemberType(m); //需要判断非数据库字段,否则会抛异常 if (_mappingKey != null && _mappingKey.IndexOf(string.Concat(m.Name, "$").ToLower()) != -1) { if (memberType == typeof(string)) { return(new ReaderValuesExtrator <string>(m.Name, (idx, reader) => reader.IsDBNull(idx) ? null : reader.GetString(idx)).ExtrationDelegate); } else if (memberType == typeof(bool)) { return(new ReaderValuesExtrator <bool>(m.Name, (idx, reader) => reader.GetBoolean(idx)).ExtrationDelegate); } else if (memberType == typeof(bool?)) { return(new ReaderValuesExtrator <bool?>(m.Name, (idx, reader) => reader.GetBoolean(idx)).ExtrationDelegate); } else if (memberType == typeof(Int16)) { return(new ReaderValuesExtrator <Int16>(m.Name, (idx, reader) => reader.GetInt16(idx)).ExtrationDelegate); } else if (memberType == typeof(Int16?)) { return(new ReaderValuesExtrator <Int16?>(m.Name, (idx, reader) => reader.GetInt16(idx)).ExtrationDelegate); } else if (memberType == typeof(Int32)) { return(new ReaderValuesExtrator <Int32>(m.Name, (idx, reader) => reader.GetInt32(idx)).ExtrationDelegate); } else if (memberType == typeof(Int32?)) { return(new ReaderValuesExtrator <Int32?>(m.Name, (idx, reader) => reader.GetInt32(idx)).ExtrationDelegate); } else if (memberType == typeof(Int64)) { return(new ReaderValuesExtrator <Int64>(m.Name, (idx, reader) => reader.GetInt64(idx)).ExtrationDelegate); } else if (memberType == typeof(Int64?)) { return(new ReaderValuesExtrator <Int64?>(m.Name, (idx, reader) => reader.GetInt64(idx)).ExtrationDelegate); } else if (memberType == typeof(byte)) { return(new ReaderValuesExtrator <byte>(m.Name, (idx, reader) => reader.GetByte(idx)).ExtrationDelegate); } else if (memberType == typeof(byte?)) { return(new ReaderValuesExtrator <byte?>(m.Name, (idx, reader) => reader.GetByte(idx)).ExtrationDelegate); } else if (memberType == typeof(char)) { return(new ReaderValuesExtrator <char>(m.Name, (idx, reader) => reader.GetChar(idx)).ExtrationDelegate); } else if (memberType == typeof(char?)) { return(new ReaderValuesExtrator <char?>(m.Name, (idx, reader) => reader.GetChar(idx)).ExtrationDelegate); } else if (memberType == typeof(DateTime)) { return(new ReaderValuesExtrator <DateTime>(m.Name, (idx, reader) => reader.GetDateTime(idx)).ExtrationDelegate); } else if (memberType == typeof(DateTime?)) { return(new ReaderValuesExtrator <DateTime?>(m.Name, (idx, reader) => reader.GetDateTime(idx)).ExtrationDelegate); } else if (memberType == typeof(decimal)) { return(new ReaderValuesExtrator <decimal>(m.Name, (idx, reader) => reader.GetDecimal(idx)).ExtrationDelegate); } else if (memberType == typeof(decimal?)) { return(new ReaderValuesExtrator <decimal?>(m.Name, (idx, reader) => reader.GetDecimal(idx)).ExtrationDelegate); } else if (memberType == typeof(double)) { return(new ReaderValuesExtrator <double>(m.Name, (idx, reader) => reader.GetDouble(idx)).ExtrationDelegate); } else if (memberType == typeof(double?)) { return(new ReaderValuesExtrator <double?>(m.Name, (idx, reader) => reader.GetDouble(idx)).ExtrationDelegate); } else if (memberType == typeof(float)) { return(new ReaderValuesExtrator <float>(m.Name, (idx, reader) => reader.GetFloat(idx)).ExtrationDelegate); } else if (memberType == typeof(float?)) { return(new ReaderValuesExtrator <float?>(m.Name, (idx, reader) => reader.GetFloat(idx)).ExtrationDelegate); } else if (memberType == typeof(Guid)) { return(new ReaderValuesExtrator <Guid>(m.Name, (idx, reader) => reader.GetGuid(idx)).ExtrationDelegate); } else if (memberType == typeof(Guid?)) { return(new ReaderValuesExtrator <Guid?>(m.Name, (idx, reader) => reader.GetGuid(idx)).ExtrationDelegate); } } Func <object, object> converter = StaticConvertersManager.DefaultInstance.GetStaticConverterFunc(typeof(object), memberType); if (converter == null) { throw new EmitMapperException("Could not convert an object to " + memberType.ToString()); } int fieldNum = -1; string fieldName = m.Name; Type fieldType = m.GetType(); return ((ValueGetter <object>) ( (value, state) => { var reader = ((DbDataReader)state); object result = null; //需要判断非数据库字段,否则会抛异常 if (_mappingKey != null && _mappingKey.IndexOf(string.Concat(fieldName, "$").ToLower()) == -1) { PropertyInfo p = (PropertyInfo)m; if (p.PropertyType.IsValueType) { var blank = Activator.CreateInstance(p.PropertyType); return ValueToWrite <object> .ReturnValue(blank); } else { return ValueToWrite <object> .ReturnValue(null); } } if (_mappingKey != null) { if (fieldNum == -1) { fieldNum = reader.GetOrdinal(fieldName); } result = reader[fieldNum]; } else { result = reader[fieldName]; } if (result is DBNull) { return ValueToWrite <object> .ReturnValue(null); } return ValueToWrite <object> .ReturnValue(converter(result)); } ) ); }
private Delegate GetValuesGetter(int ind, MemberInfo m) { Type memberType = ReflectionUtils.GetMemberType(m); //if (_mappingKey != null) //{ //if (memberType == typeof (string)) //{ // return // new ReaderValuesExtrator<string>(m.Name, // (idx, reader) => reader.IsDBNull(idx) ? null : reader.GetString(idx)).ExtrationDelegate; //} //if (memberType == typeof (bool)) //{ // return // new ReaderValuesExtrator<bool>(m.Name, (idx, reader) => reader.GetBoolean(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (bool?)) //{ // return // new ReaderValuesExtrator<bool?>(m.Name, (idx, reader) => reader.GetBoolean(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (Int16)) //{ // return // new ReaderValuesExtrator<Int16>(m.Name, (idx, reader) => reader.GetInt16(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (Int16?)) //{ // return // new ReaderValuesExtrator<Int16?>(m.Name, (idx, reader) => reader.GetInt16(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (Int32)) //{ // return // new ReaderValuesExtrator<Int32>(m.Name, (idx, reader) => reader.GetInt32(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (Int32?)) //{ // return // new ReaderValuesExtrator<Int32?>(m.Name, (idx, reader) => reader.GetInt32(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (Int64)) //{ // return // new ReaderValuesExtrator<Int64>(m.Name, (idx, reader) => reader.GetInt64(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (Int64?)) //{ // return // new ReaderValuesExtrator<Int64?>(m.Name, (idx, reader) => reader.GetInt64(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (byte)) //{ // return // new ReaderValuesExtrator<byte>(m.Name, (idx, reader) => reader.GetByte(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (byte?)) //{ // return // new ReaderValuesExtrator<byte?>(m.Name, (idx, reader) => reader.GetByte(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (char)) //{ // return // new ReaderValuesExtrator<char>(m.Name, (idx, reader) => reader.GetChar(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (char?)) //{ // return // new ReaderValuesExtrator<char?>(m.Name, (idx, reader) => reader.GetChar(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (DateTime)) //{ // return // new ReaderValuesExtrator<DateTime>(m.Name, (idx, reader) => reader.GetDateTime(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (DateTime?)) //{ // return // new ReaderValuesExtrator<DateTime?>(m.Name, (idx, reader) => reader.GetDateTime(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (decimal)) //{ // return // new ReaderValuesExtrator<decimal>(m.Name, (idx, reader) => reader.GetDecimal(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (decimal?)) //{ // return // new ReaderValuesExtrator<decimal?>(m.Name, (idx, reader) => reader.GetDecimal(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (double)) //{ // return // new ReaderValuesExtrator<double>(m.Name, (idx, reader) => reader.GetDouble(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (double?)) //{ // return // new ReaderValuesExtrator<double?>(m.Name, (idx, reader) => reader.GetDouble(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (float)) //{ // return // new ReaderValuesExtrator<float>(m.Name, (idx, reader) => reader.GetFloat(idx)) // .ExtrationDelegate; //} //if (memberType == typeof (float?)) //{ // return // new ReaderValuesExtrator<float?>(m.Name, (idx, reader) => reader.GetFloat(idx)) // .ExtrationDelegate; //} if (memberType == typeof(Guid)) { return (new ReaderValuesExtrator <Guid>(m.Name, (idx, reader) => reader.GetGuid(idx)) .ExtrationDelegate); } if (memberType == typeof(Guid?)) { return (new ReaderValuesExtrator <Guid?>(m.Name, (idx, reader) => reader.GetGuid(idx)) .ExtrationDelegate); } //} Func <object, object> converter = StaticConvertersManager.DefaultInstance.GetStaticConverterFunc(typeof(object), memberType); if (converter == null) { throw new EmitMapperException("Could not convert an object to " + memberType); } int fieldNum = -1; string fieldName = m.Name; return ((ValueGetter <object>) ( (value, state) => { var reader = ((DbDataReader)state); object result = null; if (_mappingKey != null) { if (fieldNum == -1) { fieldNum = reader.GetOrdinal(fieldName); } result = reader[fieldNum]; } else { result = reader[fieldName]; } if (result is DBNull) { return ValueToWrite <object> .ReturnValue(null); } return ValueToWrite <object> .ReturnValue(converter(result)); } ) ); }
/// <summary> /// Gets the mapping operations. /// </summary> /// <param name="from">The type from.</param> /// <param name="to">To type to.</param> /// <returns>The mapping operations.</returns> public override IMappingOperation[] GetMappingOperations(Type from, Type to) { return(this.FilterOperations(from, to, EmitMapper.Utils.ReflectionUtils.GetPublicFieldsAndProperties(to) .Where(member => (member.MemberType == MemberTypes.Field || member.MemberType == MemberTypes.Property) && ((PropertyInfo)member).GetSetMethod() != null) .Select(destinationMember => (IMappingOperation) new DestWriteOperation { Destination = new MemberDescriptor(destinationMember), Getter = (ValueGetter <object>)((item, state) => { if (item == null || !(item is DataContainer)) { return ValueToWrite <object> .Skip(); } var destinationType = EmitMapper.Utils.ReflectionUtils.GetMemberType(destinationMember); var fieldDescription = ReflectionUtils.GetDataMemberDefinition(destinationMember); var destinationMemberValue = ConvertFieldToDestinationProperty((DataContainer)item, destinationType, fieldDescription.FirstOrDefault()); return destinationMemberValue == null ? ValueToWrite <object> .Skip() : ValueToWrite <object> .ReturnValue(destinationMemberValue); }) })).ToArray()); }
/// <summary> /// Gets the mapping operations. /// </summary> /// <param name="from">The type from.</param> /// <param name="to">To type to.</param> /// <returns>The mapping operations.</returns> public override IMappingOperation[] GetMappingOperations(Type from, Type to) { return(FilterOperations(from, to, ReflectionUtils.GetTypeDataContainerDescription(to) .Select(fieldsDescription => { string fieldName = fieldsDescription.Key; MemberInfo destinationMember = fieldsDescription.Value.Item1; Type fieldType = fieldsDescription.Value.Item2; return new DestWriteOperation { Destination = new MemberDescriptor( destinationMember), Getter = (ValueGetter <object>) ((item, state) => { if (item == null || !(item is DataContainer)) { return ValueToWrite < object > .Skip (); } var container = item as DataContainer; string value; if ( container .Fields == null || !container .Fields .TryGetValue (fieldName, out value)) { return ValueToWrite < object > .Skip (); } Type destinationType = EmitMapper .Utils .ReflectionUtils .GetMemberType (destinationMember); object destinationMemberValue = ReflectionUtils .ConvertValue (value, fieldType, destinationType); return ValueToWrite <object > .ReturnValue (destinationMemberValue); }) }; })).ToArray()); }