Dictionary<TKey,TValue>.TryGetValue and Anonymous Types

One of the methods I find to be the most useful in .Net is the method Dictionary<TKey,TValue>.TryGetValue.  This method is a nice compromise between performance, explicit return vs. exception, and a being verbal about the chance of failure.  It returns false on failure and uses an out parameter to return the actual requested value.  This…


Names of Anonymous Type Members

Recently I was asked how can you get a list of anonymous type member names given a query of anonymous types.  The quick answer is that you can use a quick bit of reflection to get back the names.  Public Function GetAnonymousTypeMemberNames(Of T)(ByVal anonymousType As T) As List(Of String) Dim type = GetType(T) Dim list…


Casting to an Anonymous Type

This discussion is building upon a previous post on how to acquire an anonymous type … type.  The next question is, how can you cast an arbitrary object into an anonymous type?  At a glance this doesn’t seem possible as you cannot directly express the type of an anonymous type in code.  For instance the…


Coding Quiz: Anonymous Type Types

Question: How can you create a variable in VB which is typed as an anonymous type but not actually create an instance of that type? Answer in comments.  Note, doing this is not particularly useful it came about while I was playing around with a feature a few days ago.