コード例 #1
0
        public IICCodeTable(IICConfigTableBuffer buffer)
            : base(buffer.TableName, buffer.Version)
        {
            _innerTable = new Dictionary <K, V>();
            int columnCount = buffer.ColumnNames.Length;

            Type keyType   = typeof(K);
            Type valueType = typeof(V);
            bool simpleKey = keyType.BaseType != typeof(IICCodeTableItemKey);

            FieldInfo[] valueFields = new FieldInfo[columnCount];
            FieldInfo[] keyFields   = new FieldInfo[columnCount];
            IICCodeTableFieldAttribute[] valueAttrs = new IICCodeTableFieldAttribute[columnCount];
            IICCodeTableFieldAttribute[] keyAttrs   = new IICCodeTableFieldAttribute[columnCount];

            FieldInfo[] fields = valueType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                IICCodeTableFieldAttribute attr = AttributeHelper.TryGetAttribute <IICCodeTableFieldAttribute>(field);

                if (attr != null)
                {
                    bool find = false;
                    for (int i = 0; i < columnCount; i++)
                    {
                        if (buffer.ColumnNames[i] == attr.FieldName)
                        {
                            valueFields[i] = field;
                            valueAttrs[i]  = attr;
                            find           = true;
                            if (simpleKey && attr.IsKeyField)
                            {
                                keyFields[i] = field;
                            }
                            break;
                        }
                    }
                    if (!find && attr.IsRequired)
                    {
                        throw new ConfigurationNotFoundException(TableName, "", attr.FieldName);
                    }
                }
            }

            if (!simpleKey)
            {
                fields = keyType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                foreach (FieldInfo field in fields)
                {
                    IICCodeTableFieldAttribute attr = AttributeHelper.TryGetAttribute <IICCodeTableFieldAttribute>(field);

                    if (attr != null)
                    {
                        bool find = false;
                        for (int i = 0; i < columnCount; i++)
                        {
                            if (buffer.ColumnNames[i] == attr.FieldName)
                            {
                                keyFields[i] = field;
                                keyAttrs[i]  = attr;
                                find         = true;
                                break;
                            }
                        }
                        if (!find)
                        {
                            throw new ConfigurationNotFoundException(TableName, "", attr.FieldName);
                        }
                    }
                }
            }

            foreach (RpcClass <string[]> row in buffer.Rows)
            {
                object key   = null;
                object value = Activator.CreateInstance(valueType);

                if (!simpleKey)
                {
                    key = Activator.CreateInstance(keyType);
                }

                for (int i = 0; i < columnCount; i++)
                {
                    if (valueFields[i] == null)
                    {
                        continue;
                    }

                    string valStr = row.Value[i];
                    if (valueAttrs[i].TrimString)
                    {
                        valStr = valStr.Trim();
                    }

                    object fieldValue = ObjectHelper.ConvertTo(valStr, valueFields[i].FieldType);
                    valueFields[i].SetValue(value, fieldValue);
                    if (keyFields[i] != null)
                    {
                        if (simpleKey)
                        {
                            key = fieldValue;
                        }
                        else
                        {
                            keyFields[i].SetValue(key, fieldValue);
                        }
                    }
                }

                K k1 = (K)key;
                V v1 = (V)value;
                _innerTable.Add(k1, v1);
            }
        }