Пример #1
0
        public Dictionary <MappedObjectField, object> GetFieldValues()
        {
            MappedObjectType metadata = RegisterType();

            var values = new Dictionary <MappedObjectField, object>();

            foreach (MappedObjectField field in metadata.Fields)
            {
                object value = field.FieldInfo.GetValue(this);
                if (field.ValueSource != null)
                {
                    if (field.ValueSource is FieldInfo)
                    {
                        value = ((FieldInfo)field.ValueSource).GetValue(value);
                    }
                    else if (field.ValueSource is PropertyInfo)
                    {
                        value = ((PropertyInfo)field.ValueSource).GetValue(value, null);
                    }
                    else if (field.ValueSource is MethodInfo)
                    {
                        value = ((MethodInfo)field.ValueSource).Invoke(value, null);
                    }
                }
                values[field] = value;
            }

            return(values);
        }
Пример #2
0
        public MappedObjectType RegisterType()
        {
            Type             type = this.GetType();
            MappedObjectType metadata;

            // Check if type already registered
            if (!_cache.TryGetValue(type, out metadata))
            {
                int typeid = Attribute.IsDefined(type, typeof(MappedObjectTypeIDAttribute)) ?
                             ((MappedObjectTypeIDAttribute)Attribute.GetCustomAttribute(type, typeof(MappedObjectTypeIDAttribute))).TypeID :
                             0;

                lock (_cache)
                {
                    List <MappedObjectField> fields = new List <MappedObjectField>();

                    // Get field indexes
                    foreach (FieldInfo field in type.GetFields())
                    {
                        if (Attribute.IsDefined(field, typeof(MappedObjectFieldIndexAttribute)))
                        {
                            var    attribute       = (MappedObjectFieldIndexAttribute)Attribute.GetCustomAttribute(field, typeof(MappedObjectFieldIndexAttribute));
                            int    columnIndex     = attribute.ColumnIndex;
                            string valueSourceName = attribute.ValueSource;

                            // If a value source was specified, find a field, property that has a getter, or method with no params that returns a value
                            MemberInfo valueSource = null;
                            if (valueSourceName != null)
                            {
                                MemberInfo[] members = field.FieldType.GetMember(valueSourceName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                                foreach (MemberInfo member in members)
                                {
                                    if (
                                        member.MemberType == MemberTypes.Field ||
                                        (
                                            member.MemberType == MemberTypes.Property &&
                                            ((PropertyInfo)member).CanRead
                                        ) ||
                                        (
                                            member.MemberType == MemberTypes.Method &&
                                            ((MethodInfo)member).ReturnType != typeof(void) &&
                                            ((MethodInfo)member).GetParameters().Length == 0 &&
                                            !((MethodInfo)member).IsGenericMethod
                                        )
                                        )
                                    {
                                        valueSource = member;
                                        break;
                                    }
                                }
                            }

                            fields.Add(new MappedObjectField()
                            {
                                ColumnIndex = columnIndex, FieldInfo = field, ValueSource = valueSource
                            });
                        }
                    }

                    _cache[type] = metadata = new MappedObjectType()
                    {
                        TypeID = typeid, Fields = fields.ToArray()
                    };
                }
            }

            return(metadata);
        }