我有一本字典,其中的值是在运行时确定的。我可以将它创建为一个IDictionary
并添加到它,但是我无法排序。有没有办法将其创建为 aDictionary
以便我可以访问OrderBy
,或者是否有另一种方法将其排序为 a IDictionary
?
void func (PropertyDescriptor prop)
{
//Create dynamic dictionary
Type GenericTypeDictionary = typeof(Dictionary<,>);
Type SpecificTypeDictionary = GenericTypeDictionary.MakeGenericType(typeof(T), prop.PropertyType);
var genericDictionary = Activator.CreateInstance(SpecificTypeDictionary) as IDictionary ;
//Add some items to it
//....
//Sort items (this line doesn't compile)
genericDictionary = genericDictionary.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
}