コード例 #1
0
ファイル: Table.cs プロジェクト: vinhquyen/ExcelToObject
        internal bool ReadDictionaryInternal(Type valueType, IDictionary destDic, KeySelector keySelector = null)
        {
            List <object> values = new List <object>();

            bool retn = ReadListInternal(valueType, 0, values);

            if (retn == false)
            {
                return(false);
            }

            if (keySelector == null)
            {
                keySelector = KeySelector.From(valueType, GetColumnName(0));
            }

            foreach (object value in values)
            {
                object key = keySelector.Select(value);

                try
                {
                    destDic.Add(key, value);
                }
                catch (ArgumentException ae)
                {
                    // Key already exists?
                    throw new Exception(string.Format("Duplicated key : tablePath={0}, key={1}", mName, key), ae);
                }
            }

            return(true);
        }
コード例 #2
0
        internal bool ReadDictionaryInternal(string tablePath, Type valueType, ReadContext context, IDictionary destDic, KeySelector keySelector = null)
        {
            List <object> values = new List <object>();

            bool retn = ReadListInternal(tablePath, valueType, context, values);

            if (retn == false)
            {
                return(false);
            }

            if (keySelector == null)
            {
                keySelector = KeySelector.From(valueType, context.table.GetColumnName(0));
            }

            foreach (object value in values)
            {
                object key = keySelector.Select(value);

                destDic.Add(key, value);
            }

            return(true);
        }
コード例 #3
0
ファイル: Table.cs プロジェクト: vinhquyen/ExcelToObject
        public Dictionary <TKey, T> ReadDictionary <TKey, T>(Func <T, TKey> keySelector) where T : new()
        {
            var dic = new Dictionary <TKey, T>();

            bool retn = ReadDictionaryInternal(typeof(T), dic, KeySelector.From(keySelector));

            return(retn ? dic : null);
        }
コード例 #4
0
ファイル: Table.cs プロジェクト: vinhquyen/ExcelToObject
        public Dictionary <TKey, T> ReadDictionary <TKey, T>(string keyName) where T : new()
        {
            var dic = new Dictionary <TKey, T>();

            var keySelector = keyName.IsValid() ? KeySelector.From(typeof(T), keyName) : null;

            bool retn = ReadDictionaryInternal(typeof(T), dic, keySelector);

            return(retn ? dic : null);
        }
コード例 #5
0
        public Dictionary <TKey, T> ReadDictionary <TKey, T>(string tablePath, Func <T, TKey> keySelector, CustomParser customParser = null) where T : new()
        {
            var context = new ReadContext()
            {
                customParser = customParser
            };

            var dic = new Dictionary <TKey, T>();

            bool retn = ReadDictionaryInternal(tablePath, typeof(T), context, dic, KeySelector.From(keySelector));

            return(retn ? dic : null);
        }
コード例 #6
0
        public Dictionary <TKey, T> ReadDictionary <TKey, T>(string tablePath, string keyName, CustomParser customParser = null) where T : new()
        {
            var context = new ReadContext()
            {
                customParser = customParser
            };

            var dic = new Dictionary <TKey, T>();

            var keySelector = keyName.IsValid() ? KeySelector.From(typeof(T), keyName) : null;

            bool retn = ReadDictionaryInternal(tablePath, typeof(T), context, dic, keySelector);

            return(retn ? dic : null);
        }
コード例 #7
0
ファイル: Table.cs プロジェクト: vinhquyen/ExcelToObject
        public object ReadField(FieldInfo fi, string dicKeyName = null)
        {
            Type fieldType = fi.FieldType;

            if (fieldType.Name == "List`1")
            {
                Type itemType = fieldType.GetGenericArguments()[0];

                object listObj = Util.New(fieldType);

                bool retn = ReadListInternal(itemType, 0, listObj as IList);

                return(retn ? listObj : null);
            }
            else if (fieldType.Name == "Dictionary`2")
            {
                var subTypes = fieldType.GetGenericArguments();

                Type keyType   = subTypes[0];
                Type valueType = subTypes[1];

                object dicObj = Util.New(fieldType);

                KeySelector keySelector = dicKeyName.IsValid() ? KeySelector.From(valueType, dicKeyName) : null;

                bool retn = ReadDictionaryInternal(valueType, dicObj as IDictionary, keySelector);

                return(retn ? dicObj : null);
            }
            else
            {
                List <object> listObj = new List <object>();

                bool retn = ReadListInternal(fieldType, 1, listObj);

                return(retn ? listObj[0] : null);
            }
        }