DotNet Programming World

Thursday, November 10, 2011

Easy way to create anonymous list object


var data = new { OfficerName = "", AccountType = "", AccountNumber = "", ApplicationId = "" };

var itemData = data.ForceCast(e.Item.DataItem);
var itemList = data.GetNewList(); --> create a new list with the anoymous type of data variable.

Code above will generate a dotnet list collection with an anonymous type.
If the type is T whose members are Name, Dept, Role, and Phone in string type.

The implementation is following:


public static class AnonymousClassHelper
{
///
/// Example:
/// var data = new { OfficerName = "", AccountType = "", AccountNumber = "", ApplicationId = "" };
/// var itemData = data.ForceCast(e.Item.DataItem);
///
/// Now, you can use itemData.OfficerName
/// >summary>
///
///
///
///
public static T ForceCast;(this T prototype, object obj)
{
return (T)obj;
}

public static List GetNewList(this T protoType)
{
return new List();
}
}

0 Comments:

Post a Comment

<< Home