private static bool SetMemberValue <T>
        (
            T target
            , MemberInfo member
            , string settingValueText
        )
            where T : new()
        {
            var  r = false;
            Type memberValueType = null;

            if (member.MemberType == MemberTypes.Field)
            {
                var fieldInfo = (FieldInfo)member;
                memberValueType = fieldInfo.FieldType;
            }
            else if (member.MemberType == MemberTypes.Property)
            {
                var propertyInfo = (PropertyInfo)member;
                memberValueType = propertyInfo.PropertyType;
            }
            if (memberValueType.IsNullableType())
            {
                memberValueType = memberValueType
                                  .GetNullableUnderlyingType();
            }
            var memberName   = member.Name;
            var memberSetter = DynamicExpressionTreeHelper
                               .CreateMemberSetter         //<T, object>
                                   (typeof(T), memberName);
            var methodInfo = memberValueType
                             .GetMethod
                             (
                "Parse"
                , new Type[] { typeof(string) }
                             );

            if (methodInfo != null)
            {
                var delegateInvoker = DynamicExpressionTreeHelper
                                      .CreateDelegate
                                      (
                    methodInfo
                                      );
                var settingValue = delegateInvoker
                                   .DynamicInvoke(settingValueText);
                memberSetter(target, settingValue);
            }
            else
            {
                memberSetter(target, (object)settingValueText);
            }

            r = true;
            return(r);
        }
        public static List <TEntry> ToList <TEntry>(this DataTable target)
            where TEntry : new()
        {
            var type    = typeof(TEntry);
            var columns = target.Columns;
            var actions = new Dictionary <string, Action <object, object> >();

            foreach (DataColumn c in columns)
            {
                var columnName = c.ColumnName;
                var action     = DynamicExpressionTreeHelper
                                 .CreateMemberSetter
                                 (
                    typeof(TEntry)
                    , columnName
                                 );
                actions[columnName] = action;
            }
            List <TEntry> list = null;
            var           rows = target.Rows;

            foreach (DataRow r in rows)
            {
                var entry = new TEntry();
                if (list == null)
                {
                    list = new List <TEntry>();
                }
                foreach (DataColumn c in columns)
                {
                    var columnName = c.ColumnName;
                    var v          = r[columnName];
                    if
                    (
                        !DBNull
                        .Value
                        .Equals(v)
                    )
                    {
                        var action = actions[columnName];
                        action(entry, v);
                    }
                }
                list.Add(entry);
            }
            return(list);
        }
Beispiel #3
0
        private static void SetMemberValueToTarget <TTarget, TMember>
        (
            TTarget target
            , MemberInfo memberInfo
            , TMember memberValue
        )
        {
            string memberName = memberInfo.Name;

            //not support static member
            //
            //if (memberInfo.GetGetMethod().IsStatic)
            //{
            //    var setter = DynamicExpressionTreeHelper
            //                        .CreateTargetSetStaticPropertyValueAction
            //                                <TTarget, TProperty>
            //                                    (
            //                                        memberName
            //                                    );
            //    setter(propertyValue);
            //}
            //else
            //{

            if (target != null)
            {
                //var setter = DynamicExpressionTreeHelper
                //                       .CreateTargetSetPropertyValueAction
                //                            <TTarget, TProperty>
                //                               (
                //                                   propertyName
                //                               );

                var setter = DynamicExpressionTreeHelper
                             .CreateMemberSetter <TTarget, TMember>
                             (
                    memberName
                             );
                setter(target, memberValue);
            }
            //}
        }
Beispiel #4
0
        GetModelMembersAccessors
        (
            Type targetType
            , bool needDefinitionAttributeProcess = false
        )

        {
            var members = TypeHelper
                          .GetModelMembers
                          (
                targetType
                          );

            foreach (var member in members)
            {
                var memberName = member.Name;

                Type        memberType  = null;
                MemberTypes?memberTypes = null;
                if (member is FieldInfo)
                {
                    var fieldInfo = member as FieldInfo;
                    memberType = fieldInfo.FieldType;
                }
                else if (member is PropertyInfo)
                {
                    var propertyInfo = member as PropertyInfo;
                    memberType = propertyInfo.PropertyType;
                }

                var accessor = new MemberAccessor()
                {
                    Getter = DynamicExpressionTreeHelper
                             .CreateMemberGetter(targetType, memberName)
                    ,
                    Setter = DynamicExpressionTreeHelper
                             .CreateMemberSetter(targetType, memberName)
                    ,
                    Member = member
                    ,
                    Types = memberTypes

                    ,
                    Name = memberName
                    ,
                    Key = memberName
                    ,
                    MemberType = GetNullableUnderlyingType(memberType)
                };
                if (needDefinitionAttributeProcess)
                {
                    var attribute = member
                                    .GetCustomAttributes
                                    (
                        typeof(MemberAdditionalDefinitionAttribute)
                        , true
                                    )
                                    .FirstOrDefault();         //as DataTableColumnIDAttribute;
                    if (attribute != null)
                    {
                        var asAttribute =
                            attribute as MemberAdditionalDefinitionAttribute;
                        if (asAttribute != null)
                        {
                            //if (asAttribute.DataTableColumnDataType != null)
                            //{
                            //    accessor
                            //        .MemberType = asAttribute
                            //                                    .DataTableColumnDataType;
                            //}
                            accessor
                            .DefinitionAttribute = asAttribute;
                            if
                            (
                                !asAttribute
                                .DataTableColumnName
                                .IsNullOrEmptyOrWhiteSpace()
                            )
                            {
                                accessor
                                .Key
                                    = asAttribute
                                      .DataTableColumnName;
                            }
                        }
                    }
                }
                yield
                return
                    (accessor);
            }

            //var properties = type.GetProperties();
            //foreach (var property in properties)
            //{
            //    if
            //        (
            //            ModelMemberTypes
            //                .Any
            //                    (
            //                        (x) =>
            //                        {
            //                            var r = false;
            //                            var propertyType = property.PropertyType;
            //                            if (x == propertyType)
            //                            {
            //                                r = true;
            //                            }
            //                            if (!r)
            //                            {
            //                                if
            //                                    (
            //                                        propertyType
            //                                            .IsGenericType
            //                                        &&
            //                                        propertyType
            //                                            .GetGenericTypeDefinition()
            //                                            .Equals
            //                                                (
            //                                                    typeof(Nullable<>)
            //                                                )
            //                                    )
            //                                {
            //                                    if
            //                                        (
            //                                            x
            //                                            ==
            //                                            GetNullableUnderlyingType
            //                                                    (propertyType)
            //                                        )
            //                                    {
            //                                        r = true;
            //                                    }
            //                                }
            //                            }
            //                            return r;
            //                        }
            //                    )
            //        )
            //    {
            //        var propertyName = property.Name;
            //        var propertyType = property.PropertyType;
            //        var accessor = new MemberAccessor()
            //        {
            //            Getter = DynamicPropertyAccessor
            //                        .CreateGetPropertyValueFunc(type, propertyName)
            //            , Setter = DynamicPropertyAccessor
            //                        .CreateSetPropertyValueAction(type, propertyName)
            //            , Member = property
            //            , Name = property.Name
            //            , Key = property.Name
            //            , MemberType = GetNullableUnderlyingType(propertyType)
            //        };
            //        if (needDefinitionAttributeProcess)
            //        {
            //            var attribute = property
            //                                .GetCustomAttributes
            //                                    (
            //                                        typeof(MemberAdditionalDefinitionAttribute)
            //                                        , true
            //                                    )
            //                                    .FirstOrDefault(); //as DataTableColumnIDAttribute;
            //            if (attribute != null)
            //            {
            //                var asAttribute =
            //                                attribute as MemberAdditionalDefinitionAttribute;
            //                if (asAttribute != null)
            //                {
            //                    if (asAttribute.DataTableColumnDataType != null)
            //                    {
            //                        accessor
            //                            .MemberType = asAttribute
            //                                                        .DataTableColumnDataType;
            //                    }
            //                    accessor
            //                            .DefinitionAttribute = asAttribute;
            //                    if
            //                        (
            //                            !asAttribute
            //                                    .DataTableColumnName
            //                                    .IsNullOrEmptyOrWhiteSpace()
            //                        )
            //                    {
            //                        accessor
            //                            .Key
            //                                    = asAttribute
            //                                            .DataTableColumnName;
            //                    }
            //                }
            //            }
            //        }
            //        yield
            //            return
            //                accessor;
            //    }
            //}
            //);
            //return dictionary;
        }
        InitializeProcessingTypedPerformanceCountersPairs
        <TPerformanceCountersContainer>
        (
            TPerformanceCountersContainer target
            , PerformanceCounterProcessingFlagsType
            inclusivePerformanceCounterProcessingFlagsType
            , PerformanceCounterProcessingFlagsType
            exclusivePerformanceCounterProcessingFlagsType
            = PerformanceCounterProcessingFlagsType.None
        )
            where
        TPerformanceCountersContainer : AbstractPerformanceCountersContainer
        {
            //var properties = GetPerformanceCountersPropertiesOrFields<TPerformanceCountersContainer, PerformanceCountersPair>();
            var members    = GetPerformanceCountersPairsMembers <TPerformanceCountersContainer>();
            var memberName = string
                             .Format
                             (
                "{0}{1}"
                , Enum
                .GetName
                (
                    typeof(PerformanceCounterProcessingFlagsType)
                    , inclusivePerformanceCounterProcessingFlagsType
                )
                , "PerformanceCountersPairs"
                             );
            var setter = DynamicExpressionTreeHelper
                         .CreateMemberSetter
                         <TPerformanceCountersContainer, PerformanceCountersPair[]>
                         (
                memberName
                         );
            var targetValue = members
                              .Where
                              (
                (x) =>
            {
                var r         = false;
                var attribute = x
                                .GetCustomAttribute
                                <PerformanceCounterDefinitionAttribute>();
                if (attribute != null)
                {
                    r = attribute
                        .CounterProcessingType
                        .HasFlag(inclusivePerformanceCounterProcessingFlagsType);
                }
                return(r);
            }
                              )
                              .Select
                              (
                (x) =>
            {
                return
                (DynamicExpressionTreeHelper
                 .CreateMemberGetter
                 <TPerformanceCountersContainer, PerformanceCountersPair>
                 (
                     x.Name
                 )(target));
            }
                              )
                              .ToArray();

            setter(target, targetValue);
        }
        public static IEnumerable <TEntry> GetEnumerable <TEntry>
        (
            IDataReader dataReader
            , bool needDefinitionAttributeProcess = false
        )
            where TEntry : new()
        {
            var type = typeof(TEntry);
            MemberAdditionalDefinitionAttribute attribute = null;
            var members = TypeHelper
                          .GetModelMembers(type)
                          .Select
                          (
                (x) =>
            {
                attribute =
                    x
                    .GetCustomAttributes
                        (typeof(MemberAdditionalDefinitionAttribute), true)
                    .FirstOrDefault() as MemberAdditionalDefinitionAttribute;
                return(x);
            }
                          );

            while (dataReader.Read())
            {
                TEntry entry = new TEntry();
                foreach (var x in members)
                {
                    var dataColumnName = x.Name;
                    if (needDefinitionAttributeProcess)
                    {
                        if (attribute != null)
                        {
                            if
                            (
                                !attribute
                                .DataTableColumnName
                                .IsNullOrEmptyOrWhiteSpace()
                            )
                            {
                                dataColumnName = attribute.DataTableColumnName;
                            }
                        }
                    }
                    var setter = DynamicExpressionTreeHelper
                                 .CreateMemberSetter <TEntry, object>
                                 (
                        x.Name
                                 );
                    setter
                    (
                        entry
                        , dataReader[dataColumnName]
                    );
                }
                yield
                return
                    (entry);
            }
        }