Click here to Skip to main content
13,558,664 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


32 bookmarked
Posted 27 Jul 2009
Licenced CC (ASA 2.5)

Anonymous Types - Dynamic Programming With C#

, 28 Jul 2009
Rate this:
Please Sign up or sign in to vote.
Anonymous Types are a powerful feature in the .NET language, but are limited when you need to pass that information around. In this post, I discuss a class that I wrote to allow you to work with Anonymous Types easily, even after they leave their initial scope.


Have you ever tried passing around an Anonymous Type in C#? It doesn't work all that well since once your type loses scope, you can't cast it again (not easily at least). In this post, I discuss a class that I wrote to allow you to work with Anonymous Types easily, even after they leave their initial scope.

An Easier Way

In a previous post, I discussed trying to work with Anonymous Types in .NET. I had included some code for working with those types to try and make it easier to pass your information around inside your projects.

Essentially, it was a wrapper for Reflection that allowed you to access properties by providing the correct type and property name. It would keep the instance of the Anonymous Type in memory so it could work directly with the object.

You may have noticed that there were .Get() functions, but no .Set() function. Why is that?

You may have never noticed this before, but all of the properties in an Anonymous Type are read-only. Most of the time, you aren't going to make changes to those values anyways, but if you can't assign the value of a property in one pass, then it's a bummer.

Extending It A Bit Further

For fun, I took a little time to write another version of AnonymousType. This one is a lot more flexible than the one before, but you aren't really using C# the way it is intended to be. Instead of using Reflection, you're using a Dictionary, specifically with strings (for the property names) and objects (for the values).

You create objects the same way as before, but now you have access to a few more features and functions. Below are some examples of how you can use this class.

//Creating Instances of AnonymousType
//Create a using an Anonymous type
AnonymousType type = AnonymousType.Create(new {
  name = "Jim",
  age = 40
  //method = ()=> { Console.WriteLine(); } // still doesn't work here

//or an empty AnonymousType
AnonymousType empty = new AnonymousType();

//or even an existing object
var something = new {
  name = "Mark",
  age = 50
AnonymousType another = new AnonymousType(something);

//Creating And Using Methods
//append methods and call them
another.SetMethod("print", (string name, int age, bool admin) => {
  Console.WriteLine("{0} :: {1} :: {2}", name, age, admin);
another.Call("print", "Mark", 40, false);

//append a method with a return value
another.SetMethod("add", (int a, int b) => {
  return a + b;
int result = another.Call<int>("add", 5, 10);

//Working With Multiple Properties
//add properties and work with them
//(NOTE: a better way is shown below)
another.Set("list", new List<string>());
another.Get<List<string>>("list").Add("String 1");
another.Get<List<string>>("list").Add("String 2");
another.Get<List<string>>("list").Add("String 3");

//or use it an easier way
another.With((List<string> list) => {
  list.Add("String 4");
  list.Add("String 5");
  list.Add("String 6");

//you can work with more than one type
another.With((string name, int age) => {
  Console.Write("{0} :: {1}", name, age);

Again, this is breaking away from the way C# was intended to be used, so be careful how much you make use of it. For the most part, you're going to want to create your own classes to hold this information, but while working with Anonymous Types, this is a quick and easy way to pass your information around.

Stay tuned for version three (something really cool!).

Source Code


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralDynamic implementation Pin
Asher Barak12-Aug-09 2:36
memberAsher Barak12-Aug-09 2:36 
GeneralRe: Dynamic implementation Pin
webdev_hb12-Aug-09 3:55
memberwebdev_hb12-Aug-09 3:55 
GeneralSounds like a collection object I did... Pin
supercat928-Jul-09 10:43
membersupercat928-Jul-09 10:43 
GeneralInteresting Read Pin
kurt gooding27-Jul-09 23:04
memberkurt gooding27-Jul-09 23:04 
GeneralRe: Interesting Read Pin
webdev_hb28-Jul-09 3:50
memberwebdev_hb28-Jul-09 3:50 
GeneralSorry everyone... Pin
webdev_hb27-Jul-09 2:11
memberwebdev_hb27-Jul-09 2:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 28 Jul 2009
Article Copyright 2009 by webdev_hb
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid