Exemplo n.º 1
0
        async Task <object> GetMemberValue(MemberInfo member, DbClient conn, ResultRow row)
        {
            Type memberType = member.GetUnderlyingType();

            DbFieldAttribute     columnAttribute    = member.GetCustomAttribute <DbFieldAttribute>(true);
            DbReferenceAttribute referenceAttribute = member.GetCustomAttribute <DbReferenceAttribute>(true);

            if (columnAttribute != null)
            {
                object value      = row[columnAttribute.Name ?? member.Name];
                string columnName = columnAttribute.Name ?? member.Name;

                if (columnAttribute.ValidFormatter(memberType))
                {
                    value = columnAttribute.FormatSetValue(memberType, value);
                }
                else if (memberType == typeof(DateTime))
                {
                    value = row.GetDateTime(columnName, default(DateTime));
                }
                else if (memberType == typeof(bool))
                {
                    value = row.GetBoolean(columnName);
                }

                if (!(value is DBNull))
                {
                    return(value);
                }
            }
            if (referenceAttribute != null)
            {
                object value = row[referenceAttribute.Column];

                ReferenceType refType = ReferenceType.Singular;

                if (memberType.IsGenericType && memberType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    memberType = memberType.GetGenericArguments()[0];
                    refType    = ReferenceType.List;
                }
                else if (memberType.IsArray)
                {
                    memberType = memberType.GetElementType();
                    refType    = ReferenceType.Array;
                }

                object result = await(Task <object>) GetNestedMethod().MakeGenericMethod(memberType).Invoke(
                    this,
                    new object[]
                {
                    conn,
                    referenceAttribute.ForeignColumn,
                    value,
                    refType
                }
                    );

                if (!(value is DBNull))
                {
                    return(result);
                }
            }
            return(null);
        }