给定的密​​钥没有出现在字典中这个错误来自我的代码plz帮助我 [英] The given key was not present in the dictionary this error is coming through my code plz help me

查看:65
本文介绍了给定的密​​钥没有出现在字典中这个错误来自我的代码plz帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private Dictionary<int, Dictionary<int, FieldParseInfo>> parseMap = new Dictionary<int, Dictionary<int, FieldParseInfo>>();

public void SetParseDictionary(int type, Dictionary<int, FieldParseInfo> dict)
        {
            parseMap[type] = dict;
            List<int> index = new List<int>();
            ///Agregar todas las llaves del dict a index
            ///ordenar index por numeros
            for (int i = 2; i < 129; i++)
            {
                if (dict.ContainsKey(i))
                {
                    index.Add(i);
                }
            }
            parseOrder[type] = index;
        }

 

public IsoMessage ParseMessage(byte[] buf, int isoHeaderLength, Encoding encoder)
        {
            IsoMessage m = new IsoMessage(isoHeaderLength > 0 ? encoder.GetString(buf, 0, isoHeaderLength) : null);
            int type = ((buf[isoHeaderLength] - 48) << 12)
            | ((buf[isoHeaderLength + 1] - 48) << 8)
            | ((buf[isoHeaderLength + 2] - 48) << 4)
            | (buf[isoHeaderLength + 3] - 48);
            m.Type = type;

            //Parse the bitmap
            bool extended = (HexByteValue(buf[isoHeaderLength + 4]) & 8) > 0;
            BitArray bs = new BitArray(extended ? 128 : 64);
            int pos = 0;
            for (int i = isoHeaderLength + 4; i < isoHeaderLength + 20; i++)
            {
                int hex = HexByteValue(buf[i]);
                bs.Set(pos++, (hex & 8) > 0);
                bs.Set(pos++, (hex & 4) > 0);
                bs.Set(pos++, (hex & 2) > 0);
                bs.Set(pos++, (hex & 1) > 0);
            }
            //Extended bitmap?
            if (bs.Get(0))
            {
                for (int i = isoHeaderLength + 20; i < isoHeaderLength + 36; i++)
                {
                    int hex = HexByteValue(buf[i]);
                    bs.Set(pos++, (hex & 8) > 0);
                    bs.Set(pos++, (hex & 4) > 0);
                    bs.Set(pos++, (hex & 2) > 0);
                    bs.Set(pos++, (hex & 1) > 0);
                }
                pos = 36 + isoHeaderLength;
            }
            else
            {
                pos = 20 + isoHeaderLength;
            }
            //////Parse each field
            //Dictionary<int, FieldParseInfo> guide = parseMap[type];
            Dictionary<int, FieldParseInfo> guide = new Dictionary<int, FieldParseInfo>();
            //Dictionary<int, FieldParseInfo> guide = parseMap[type];

            //Dictionary<int, Dictionary<int, FieldParseInfo>> guide = new Dictionary<int, Dictionary<int, FieldParseInfo>>();

            List<int> index = parseOrder[type];

             
            try
            {
                foreach (int i in index)
                {
                    FieldParseInfo fpi = guide[i];
                    if (bs.Get(i - 1))
                    {
                        IsoValue val = fpi.Parse(buf, pos, encoder);
                        m.SetField(i, val);
                        pos += val.Length;
                        if (val.Type == IsoType.LLVAR)
                        {
                            pos += 2;
                        }
                        else if (val.Type == IsoType.LLLVAR)
                        {
                            pos += 3;
                        }
                    }
                }
            }
            catch
            {
            }

            return m;
        }





我的尝试:





What I have tried:

public class FieldParseInfo {
		private IsoType type;
		private int length;
        //private int parseOrder;

		/// <summary>
		/// Creates a new instance that knows how to parse a value of the given
		/// type and the given length (the length is necessary for ALPHA and NUMERIC
		/// values only).
		/// 
		/// <param name="t">The ISO8583 type.
		/// <param name="len">The length of the value to parse (for ALPHA and NUMERIC values).
		public FieldParseInfo(IsoType t, int len)  // , int PO
        { 

            type = t;
			length = len;
            //parseOrder = PO;
		}

		/// <summary>
		/// The field length to parse.
		/// 
		public int Length {
			get { return length; }
		}

		/// <summary>
		/// The type of the value that will be parsed.
		/// 
		public IsoType Type {
			get { return type; }
		}

        //public int ParseOrder
        //{
        //    get { return parseOrder; }
        //}

		/// <summary>
		/// Parses a value of the type and length specified in the constructor
		/// and returns the IsoValue.
		/// 
		/// <param name="buf">The byte buffer containing the value to parse.
		/// <param name="pos">The position inside the byte buffer where the parsing must start.
		/// <param name="encoder">The encoder to use for converting bytes to strings.
		/// <returns>The resulting IsoValue with the given types and length, and the stored value.
		public IsoValue Parse(byte[] buf, int pos, Encoding encoder)
        {
			if (type == IsoType.NUMERIC || type == IsoType.ALPHA) {
				return new IsoValue(type, encoder.GetString(buf, pos, length), length);
			} else if (type == IsoType.LLVAR) {
				length = ((buf[pos] - 48) * 10) + (buf[pos + 1] - 48);
				if (length < 1 || length > 99) {
					throw new ArgumentException("LLVAR field with invalid length");
				}
				return new IsoValue(type, encoder.GetString(buf, pos + 2, length));
			} else if (type == IsoType.LLLVAR) {
				length = ((buf[pos] - 48) * 100) + ((buf[pos + 1] - 48) * 10) + (buf[pos + 2] - 48);
				if (length < 1 || length > 999) {
					throw new ArgumentException("LLLVAR field with invalid length");
				}
				return new IsoValue(type, encoder.GetString(buf, pos + 3, length));
			} else if (type == IsoType.AMOUNT) {
				byte[] c = new byte[13];
				Array.Copy(buf, pos, c, 0, 10);
				Array.Copy(buf, pos + 10, c, 11, 2);
				c[10] = (byte)'.';
				return new IsoValue(type, Decimal.Parse(encoder.GetString(c)));
			} else if (type == IsoType.DATE10) {
				DateTime dt = DateTime.Now;
				dt = new DateTime(dt.Year,
					((buf[pos] - 48) * 10) + buf[pos + 1] - 48,
					((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48,
					((buf[pos + 4] - 48) * 10) + buf[pos + 5] - 48,
					((buf[pos + 6] - 48) * 10) + buf[pos + 7] - 48,
					((buf[pos + 8] - 48) * 10) + buf[pos + 9] - 48);
				if (dt.CompareTo(DateTime.Now) > 0) {
					dt.AddYears(-1);
				}
				return new IsoValue(type, dt);
			} else if (type == IsoType.DATE4) {
				DateTime dt = DateTime.Now;
				dt = new DateTime(dt.Year,
					((buf[pos] - 48) * 10) + buf[pos + 1] - 48,
					((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48);
				if (dt.CompareTo(DateTime.Now) > 0) {
					dt.AddYears(-1);
				}
				return new IsoValue(type, dt);
			} else if (type == IsoType.DATE_EXP) {
				DateTime dt = DateTime.Now;
				dt = new DateTime(dt.Year - (dt.Year % 100) + ((buf[pos] - 48) * 10) + buf[pos + 1] - 48,
					((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48, 1);
				return new IsoValue(type, dt);
			} else if (type == IsoType.TIME) {
				DateTime dt = DateTime.Now;
				dt = new DateTime(dt.Year, dt.Month, dt.Day,
					((buf[pos] - 48) * 10) + buf[pos + 1] - 48,
					((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48,
					((buf[pos + 4] - 48) * 10) + buf[pos + 5] - 48);
				return new IsoValue(type, dt);
			}
			return null;
		}

	}

推荐答案

我们可以为你解决这个问题。错误是非常明确的:
We can;t solve this for you. The error is very explicit:
Quote:

字典中没有给定的密钥

意味着它说的 - 你试过从字典中读取一个你没有放在那里的值:

Means what it says - you have tried to read a value out of a Dictionary that you didn't put in there:

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("John", "Smith");
dict.Add("Mike", "Hunt");
Console.WriteLine(dict["Joe"]);

所以使用调试器。

在导致异常的行上放置一个断点,并查看您尝试访问的索引值。然后查看词典本身,看看你在那里有什么价值。索引应该在那里吗?如果没有,为什么不呢?如果它应该,那么为什么不添加呢?



我们不能为你做那个 - 我们无法运行你的代码,也没有访问您的数据 - 请试一试,看看您能找到哪些信息。

So use the debugger.
Put a breakpoint on the line that causes the exception, and look at the index value you are trying to access. Then look at the Dictionary itself, and see what values you do have in there. Should the index be there? If not, why not? If it should, then why wasn't it added?

We can't do that for you - we can't run your code, and don't have access to your data - so give it a try, and see what information you can find out.


这篇关于给定的密​​钥没有出现在字典中这个错误来自我的代码plz帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆