Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Brian Bondy
Hub FS
Full 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 following code is not legal. 

 Dim o As Object = SomeCall() 
Dim x = Directcast(o, New With {.a = 5})

Even though it's not possible to directly express the type in code, we can indirectly express it via type inference.  Anonymous Types are strongly typed in the compiler and it is possible to infer these types where inference is available. 

 Public Function CastSpecial(Of T)(ByVal o As Object, ByVal t As T) As T 
    Return DirectCast(o, T)
 End Function 

The above function allows us to perform a DirectCast as long as we have an instance of the type we want to cast to.  Now we can call the code and passing in the appropriate anonymous type instance.

 Dim o As Object = SomeCall()
 Dim x = CastSpecial(o, New With {.a = 5})

There are a couple of caveats to this approach though. 

  1. The actual anonymous type and the one you are casting to must be exactly the same.  No polymorphism can be involved.
  2. The anonymous type must be created in the same assembly where you are performing the cast.  Anonymous types are specific to the assembly they are created in so performing the cast accross assemblies will cause a InvalidCastException to occur.
  3. It does needlessly create an object to perform the cast.

Published Monday, October 01, 2007 11:15 AM by Jared Parsons

Filed under: , ,

Comments

# Techy News Blog » Casting to an Anonymous Type @ Monday, October 01, 2007 11:33 AM

PingBack from http://www.artofbam.com/wordpress/?p=4276

Techy News Blog » Casting to an Anonymous Type

# C# Lambda Type Inference @ Friday, December 14, 2007 12:41 PM

One of the limitations of C# type inference is that you cannot use it to infer the type of a lambda expression.

jaredpar's WebLog

# Tuples Part 1 @ Thursday, January 03, 2008 11:25 AM

A tuple in computer science can be described as a set of name/value pairs. In some cases it can be described

jaredpar's WebLog

# Tuples Part 1 @ Thursday, January 03, 2008 12:14 PM

A tuple in computer science can be described as a set of name/value pairs. In some cases it can be described

Noticias externas

New Comments to this post are disabled
Page view tracker