first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

C# Code: ToType<T>() Extension Method; “how to handle conversions from an anonymous type to a specific type by using .NET 3.5 extensions”; Ed Guzman; CodeProject.com

public static object ToType<T>(this object obj, T type) {

//create instance of T type object:
var tmp = Activator.CreateInstance(Type.GetType(type.ToString())); 

//loop through the properties of the object you want to covert:          
foreach (PropertyInfo pi in obj.GetType().GetProperties()
{
  try 
  {   

    //get the value of property and try 
    //to assign it to the property of T type object:
    tmp.GetType().GetProperty(pi.Name).SetValue(tmp, 
                              pi.GetValue(obj, null), null)
  }
  catch { }
 }  

//return the T type object:
return tmp; }

[http://www.codeproject.com/KB/linq/AnonymousTypeTransform.aspx]

mod date: 2009-09-18T03:18:30.000Z