コード例 #1
0
 /// <summary>
 /// Creates the mapping between <paramref name="source"/> and <paramref name="destination"/> using the SOURCE to generate candidate names for the mapping
 /// </summary>
 public static MappingResult <Thing, Thing> CreateFromSource(Type source, Type destination, string removablePrefix = null)
 => CreateFromSource(Types.ReadablePublicThings(source), Types.WriteablePublicThings(destination), removablePrefix);
コード例 #2
0
        static Delegate CreateMapFunc(Type typeT, IReadOnlyCollection <Column> columns)
        {
            Contract.Requires(columns != null);
            Contract.Requires(typeT != null);
            Contract.Ensures(Contract.Result <Delegate>() != null);

            if (typeT.IsPrimitiveOrEnum() || typeT.IsNullable() ||
                typeT == typeof(string) || typeT == typeof(Guid) || typeT == typeof(DateTime) || typeT == typeof(DateTimeOffset) ||
                (typeT.IsClass == false && typeT.Name.Contains("Id")) && typeT.Namespace.Contains("BusterWood"))    // special case for Id, IntId, LongId and GuidId structs
            {
                return(CreatePrimativeMapFunc(typeT, columns));
            }

            MappingResult <Thing, Thing> result = Mapping.CreateFromSource(columns.Cast <Thing>().ToList(), Types.WriteablePublicThings(typeT), typeT.Name);

            if (result.Mapped.Count == 0)
            {
                throw new InvalidOperationException("No columns were mapped to type " + typeT);
            }
            var readerParam = Expression.Parameter(typeof(DbDataReader), "reader");
            var resultParam = Expression.Parameter(typeT, "result");
            var block       = CreateMapBlock(typeT, result.Mapped, readerParam, resultParam);
            var func        = typeof(Func <,>).MakeGenericType(new[] { typeof(DbDataReader), typeT });

            return(Expression.Lambda(func, block, new[] { readerParam }).Compile());
        }