idictionary

Recreating a Dictionary from an IEnumerable<KeyValuePair<>>

徘徊边缘 提交于 2019-11-26 12:38:34
问题 I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the callers require the result of the method to be a dictionary. How can I convert the IEnumerable<KeyValuePair<string, ArrayList>> into a Dictionary<string, ArrayList> so that I can use TryGetValue ? method: public IEnumerable<KeyValuePair<string, ArrayList>> GetComponents() { // ... yield return new KeyValuePair<string, ArrayList>(t.Name, controlInformation); } caller: Dictionary<string, ArrayList>

Mapping object to dictionary and vice versa

牧云@^-^@ 提交于 2019-11-26 03:05:44
问题 Are there any elegant quick way to map object to a dictionary and vice versa? Example: IDictionary<string,object> a = new Dictionary<string,object>(); a[\"Id\"]=1; a[\"Name\"]=\"Ahmad\"; // ..... becomes SomeClass b = new SomeClass(); b.Id=1; b.Name=\"Ahmad\"; // .......... 回答1: Using some reflection and generics in two extension methods you can achieve that. Right, others did mostly the same solution, but this uses less reflection which is more performance-wise and way more readable: public