Pages

Monday 26 March 2012

Anonymous Types implementation in VB.NET

Anonymous types were designed to provide greater flexibility to create your own type at run time with a simple keyword {New} with as many properties you need, there is no limit in declaring properties with different types to create an anonymous type.

How to create an anonymous type?

Create an object and declare properties that you require, similar to the below mentioned code. After that .NET compiler will take care of everything.

New With {.FirstName = Robert, _
.MiddleName = De, _
                    .LastName = niro, _
                    .Age =27
                     }

What .NET compiler does?

It generates a sealed class similar to the anonymous type declared and it makes the type as immutable and properties declared will be read-only.

Advantages of using Anonymous type:


  • Compiler creates the code on fly and I believe compiler creates a better code than any developer.
  • Reduce in number of code files that holds properties and need not bother about maintenance.

Caveats in using anonymous type:


  • Scope of an anonymous type will be specific to the implemented member
  • Anonymous type neither can be used as parameters nor used as return types
  • Cannot be generated in designer files [HLD/DSD] as separate entity

HOW TO RETURN AN ANONYMOUS TYPE?

It’s really great that we can return an anonymous type and direct cast the object with the original type, then you can access the properties in the instead of using reflection. Kudos to Type inference in VB.NET which makes developer life easier with one setting {Option Infer On}, this helps you to get the actual type from calling methods without the mentioning the actual type name.

Below is the sample that return an anonymous type as object and in the caller class how we type cast using type inference,

Declare a member the returns an object as mentioned below, in the calling class

Public Function GetCustomerDetails() As Object
Return New With {.FirstName = Robert, _
                        .MiddleName = Sr, _
                    .LastName = Deniro, _
                    .Age =27
                     }
End Function
In the caller class
Dim customerDetail = CastCustomerDetail(GetCustomerDetails(), _
                                               New With {                       .FirstName = String.Empty, _
                                                                                                .MiddleName = String.Empty, _
                                                                                                               .LastName = String.Empty, _
                                                                                      .Age = 0
                                                    })

Private Function CastCustomerDetail (Of T)(customerDetail As Object, customerDetailType As T) As T
        Return DirectCast(customerDetail, T)
End Function
Now in the customerDetail variable you can access the properties of {.FirstName as Robert and .LastName as Deniro}

Caveats:

Anonymous types cannot be identified across assemblies with same type and can be realized at runtime and not in compile time {we will discuss this in the below section}