I have a dictionary where the value is determined at runtime. I can create it as an IDictionary
and add to it fine however I can't sort.
Is there a way to create it as a Dictionary
so I can access OrderBy
or is there another way to sort it as an 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);
}