Tag: idictionary

System.Collections.Generic.KeyNotFoundException:给定的键不在字典中

我在对方法执行unit testing时收到上述错误消息。 我知道问题出在哪里,我只是不知道为什么它不在字典中。 这是字典: var nmDict = xelem.Descendants(plantNS + “Month”).ToDictionary( k => new Tuple(int.Parse(k.Ancestors(plantNS + “Year”).First().Attribute(“Year”).Value), Int32.Parse(k.Attribute(“Month1”).Value), k.Ancestors(plantNS + “Report”).First().Attribute(“Location”).Value.ToString()), v => { var detail = v.Descendants(plantNS + “Details”).First(); return new HoursContainer { BaseHours = detail.Attribute(“BaseHours”).Value, OvertimeHours = detail.Attribute(“OvertimeHours”).Value, TotalHours = float.Parse(detail.Attribute(“BaseHours”).Value) + float.Parse(detail.Attribute(“OvertimeHours”).Value) }; }); var mergedDict = new Dictionary<Tuple, HoursContainer>(); foreach (var item […]

C# – 需要一个允许空键的IDictionary实现

基本上,我想要这样的东西: Dictionary dict = new Dictionary(); dict.Add(null, “Nothing”); dict.Add(1, “One”); 是否有任何内置到基类库中允许这样做? 添加null键时,上面的代码将在运行时抛出exception。 谢谢

Dictionary.ContainsKey() – 它是如何工作的?

我已经阅读了关于Dictionary.ContainsKey()如何工作的MSDN文档,但我想知道它是如何实际进行相等比较的? 基本上,我有一个键入引用类型*的字典,我希望ContainsKey()方法检查该引用类型的某个属性作为确定密钥是否存在的基础。 例如,如果我有一个Dictionary(MyObject, int)并且MyObject有一个名为“TypeID”的公共属性( int ),我是否可以获取ContainsKey(MyObject myObject)以检查其中一个键是否具有TypeID等于myObject ? 我可以重载==运算符吗? 引用类型是一个名为“Duration”的对象,它保存一个值( double Length ); “持续时间”是我的音乐节目中使用的基本类型,表示特定声音持续多长时间。 我从中派生出类,其中包含更复杂的时序概念,如西方音乐时间签名,但希望所有这些概念在长度方面具有可比性。 编辑:正如所建议的,我在我的对象上实现了IEquitable,如下所示: public class Duration : IEquatable { protected double _length; /// /// Gets or Sets the duration in Miliseconds. /// public virtual double Length { get { return _length; } set { _length = value; } } // removed all […]

使用reflection获取通用IDictionary的值

我有一个实现IDictionary的实例,我在编译时不知道T和K,并希望从中获取所有元素。 我不想出于某种原因使用IEnumerable ,这将是IDictionary实现的唯一非generics接口。 我到目前为止的代码: // getting types Type iDictType = instance.GetType().GetInterface(“IDictionary`2”); Type keyType = iDictType.GetGenericArguments()[0]; Type valueType = iDictType.GetGenericArguments()[1]; // getting the keys IEnumerable keys = (IEnumerable)dictType.GetProperty(“Keys”) .GetValue(instance, null); foreach (object key in keys) { // ==> this does not work: calling the [] operator object value = dictType.GetProperty(“Item”) .GetValue(instance, new object[] {key } ); […]

如果值是对象并且这些对象的属性是键,那么是否有比Dictionary更好的数据结构?

我有一个Dictionary ,其中int是obj一个属性。 是否有更好的数据结构? 我觉得使用属性是关键是多余的。 此Dictionary是容器类中的一个字段,允许根据int id号随机索引到obj值。 容器类中的简化(无exception处理)索引器如下所示: obj this[int id] { get{ return this.myDictionary[id];} } 其中myDictionary是前面提到的Dictionary持有对象。 这可能是快速随机访问的典型方式,但我想获得第二意见。

从列表值c#动态创建匿名对象

我有一个列表(或可以是数组)的字符串,我想动态创建一个匿名对象。 我该怎么做呢? var dataSet = new DataSet(); dataSet.ReadXml(@””); var dataTable = dataSet.Tables[0]; var dataRow = dataTable.Rows[0]; var keys = new List {“Column1″,”Column2”}; var result = new {keys[0] = dataRow[keys[0]], keys[1] = dataRow[keys[1]]} 因此,名为“keys”的列表将在此方法之外创建,并且可以包含1到多个值。 我尝试创建一个字典并循环遍历列表并向字典添加键/值对,但后来我无法弄清楚如何将字典转换回匿名类型。 我也尝试过expando对象,但这似乎并没有让我更进一步。 我必须能够返回一个匿名类型,因为此方法的结果将与LINQ查询的GroupBy子句一起使用。 这是我必须动态创建字典的方法: public object Key(DataRow dataRow, List keys) { var dictionary = new IDictionary; foreach (string key in keys) { […]

ASP.NET MVC中htmlAttributes的匿名类和IDictionary 之间的区别?

例如,如果检查这两个扩展方法,唯一的区别是htmlAttributes的类型,因此您可以通过两种不同的方式传递htmlAttributes: public static MvcHtmlString TextBoxFor( this HtmlHelper htmlHelper, Expression<Func> expression, IDictionary htmlAttributes); public static MvcHtmlString TextBoxFor( this HtmlHelper htmlHelper, Expression<Func> expression, object htmlAttributes); 并以下列方式之一使用它们: @Html.TextBoxFor(model => model.TagLine, new { @placeholder = “We live to make art.” }) @Html.TextBoxFor(model => model.TagLine, new Dictionary { { “placeholder”, “We live to make art.” } }) 我检查了MVC源代码,我知道在后台他们使用相同的方法,但是接受匿名对象的HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)使用HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)来使匿名对象成为字典。 在我看来,使用匿名对象的视图更清晰。 你们觉得怎么样? […]