public static void MapComponentWithPrefix <T, TComponent>(this ComponentPart <T> map,
                                                           Expression <Func <T, TComponent> > f)
 {
     map.Component(f).ColumnPrefix(MemberUtility.GetMemberInfo(f).Name);
 }
예제 #2
0
        private void BuildComponent1 <TChild>(IComponentColumn column, PropertyInfo propertyInfo,
                                              ComponentPart <TChild> factory)
        {
            // build expression<func<T,Ttype>>
            var agument = Expression.Parameter(typeof(TChild), propertyInfo.Name);
            var express = Expression.Property(agument, propertyInfo);
            //Expression conversion = Expression.Convert(express, typeof(object));
            var     cdelegate = typeof(Func <,>).MakeGenericType(typeof(TChild), propertyInfo.PropertyType);
            dynamic lambda    = Expression.Lambda(cdelegate, express, agument);
            var     member    = ReflectionExtensions.ToMember(lambda);
            var     ctype     = typeof(ComponentPart <>).MakeGenericType(propertyInfo.PropertyType);
            var     t         = Activator.CreateInstance(ctype, propertyInfo.PropertyType, member);

            factory.Component(t);
            //factory.Component(lambda);
            // build action
            //factory.Component(lambda, x =>
            //{
            var properties = propertyInfo.PropertyType.GetProperties();

            foreach (var info in properties)
            {
                var bsc = info.GetCustomAttribute <BasicColumnAttribute>();
                //foreach (var attr in allAtribute)
                {
                    if (bsc != null) // build các basic column
                    {
                        try
                        {
                            var     tmpArg    = Expression.Parameter(propertyInfo.PropertyType, info.Name);
                            var     tmpExp    = Expression.Property(tmpArg, info);
                            var     tmpObject = Expression.Convert(tmpExp, typeof(object));
                            var     markType  = typeof(Func <,>).MakeGenericType(propertyInfo.PropertyType, typeof(object));
                            dynamic tmpLambda = Expression.Lambda(markType, tmpObject, tmpArg);
                            var     mapResult = t.Map(tmpLambda);
                            if (bsc.NotNull)
                            {
                                mapResult.Not.Nullable();
                            }
                            if (column.Index > 0)
                            {
                                mapResult.Column(!string.IsNullOrEmpty(bsc.Name)
                                    ? bsc.Name + column.Index
                                    : info.Name + column.Index);
                            }
                            else
                            {
                                mapResult.Column(!string.IsNullOrEmpty(bsc.Name) ? bsc.Name : info.Name);
                            }
                            if (!string.IsNullOrEmpty(bsc.CustomSqlType))
                            {
                                mapResult.CustomSqlType(bsc.CustomSqlType);
                            }
                            if (!string.IsNullOrEmpty(bsc.Default))
                            {
                                mapResult.Default(bsc.Default);
                            }
                            if (bsc.IsIndex)
                            {
                                mapResult.Index(!string.IsNullOrEmpty(bsc.Name)
                                    ? bsc.Name + column.Index
                                    : info.Name + column.Index);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    else
                    {
                        // buil components column
                        var com = info.GetCustomAttribute <ComponentColumnAttribute>();
                        if (com != null)
                        {
                            BuildComponent1(com, info, t);
                        }
                    }
                }
            }
            //});
        }