---
title: "Turn Anonymous Types into IDictionary of values"
url: "https://osherove.com/blog/2008/3/11/turn-anonymous-types-into-idictionary-of-values.md"
canonical: "https://osherove.com/blog/2008/3/11/turn-anonymous-types-into-idictionary-of-values.html"
published: "2008-03-11T15:22:37"
updated: "2008-03-11T15:22:37"
tags: ["General Software Devel..."]
author: "Roy Osherove"
description: "Hers is one cool use I found for Anonymous Types (stolen from the ASP.NET MVC Framework): Use anonymous types to create and initialize a list of dictionary values which you can pass around: void…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Turn Anonymous Types into IDictionary of values

By Roy Osherove March 11, 2008 In [General Software Devel...](https://osherove.com/blog/category/general-software-devel)

**Historical article** · Originally published March 11, 2008; technical details may be outdated.

Hers is one cool use I found for Anonymous Types (stolen from the ASP.NET MVC Framework): Use anonymous types to create and initialize a list of dictionary values which you can pass around:

void CreateADictionaryFromAnonymousType()
       {
           var dictionary = MakeDictionary(new {Name="Roy",Country="Israel"});
           Console.WriteLine(dictionary\["Name"\]);
       }

private IDictionary MakeDictionary(object withProperties)
       {
           IDictionary dic = new Dictionary<string, object>();
           var properties = System.ComponentModel.TypeDescriptor.GetProperties(withProperties);
           foreach (PropertyDescriptor property in properties)
           {
               dic.Add(property.Name,property.GetValue(withProperties));
           }
           return dic;
       }

## More in General Software Devel...

[All posts in General Software Devel...](https://osherove.com/blog/category/general-software-devel)

- Mar 16, 2008

    ### [Dark Colored Visual Studio Color Scheme](https://osherove.com/blog/2008/3/16/dark-colored-visual-studio-color-scheme.html)

- Mar 15, 2008

    ### [Looking into VIM and ViEmu.](https://osherove.com/blog/2008/3/15/looking-into-vim-and-viemu.html)

- Mar 4, 2008

    ### [Generate an object model from an XML string](https://osherove.com/blog/2008/3/4/generate-an-object-model-from-an-xml-string.html)

- Feb 27, 2008

    ### [Private builds (Gated Checkins) with Team Foundation Server](https://osherove.com/blog/2008/2/27/private-builds-gated-checkins-with-team-foundation-server.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
