Ever wonder how to get at the Properties of an Anonymous Object without Reflection? If you know the data Types of the properties, here is an Extension to the Object object that allows you to do just that:
public static T InferCasting<T>(this object o, T example)
{
return (T)o;
}
For example:
Suppose you have an anonymous object stored in the Tag of a WinForm TreeNode. Now an event fires and passes the node to you, and you can 'see' the Tag but you need to get at the Anonymous properties.
Here's how the Object extension comes into play, allowing you to 'infer' the casting by sending example properties to the extension method:
object o = ((object)((TreeNode)sender).Tag);
var v = o.InferCasting(new { level = "", mode = "", item = "", accept = "" });
string mode = v.mode;
string item = v.item;
string method = v.accept;
In this case the property types were all string, but could just as easily have been any type - just pass the correct type in the example.
In '97 or so I started fiddling around with this new thing called the Internet. Mistake.
I blinked and found myself in 2000 ditching my Accounting designation and starting to write code for a living - mostly ASP Classic back then along with some C++ and a little assembler, PHP, Perl/CGI (ouch) and of course Javascript and most of the other web-side tools.
With v3.0 .Net finally became something I couldn't ignore and so here I am still coding - WinForm, ADO, SQL, .Net, and WPF shortly, no doubt to be followed by Silverlight and maybe some MVC.
"Teach me, learn or get out of the way."