User Profile

Collapse

Profile Sidebar

Collapse
venil7
venil7
Last Activity: Aug 22 '09, 08:12 PM
Joined: Aug 22 '09
Location:
  •  
  • Time
  • Show
  • Source
Clear All
new posts

  • venil7
    replied to C# cast to anonymous type
    in .NET
    ok, I found the answer:

    first we need an Extension class:

    Code:
    static class Extension
        {
            static public T CastTo<T>(this object o, T t)
            {
                return (T)o;
            }
        }
    which will add CastTo method to all Objects, then

    Code:
    var v = al[0];
    Console.WriteLine(v.CastTo(new { field1=String.Empty}).field1);
    the trick is to...
    See more | Go to post

    Leave a comment:


  • venil7
    started a topic C# cast to anonymous type
    in .NET

    C# cast to anonymous type

    consider the following code:

    Code:
    var test1 = new { field1 = "hello" };
    ArrayList al = new ArrayList();
    al.Add(test1);
    
    //compile error
    Console.WriteLine(al[0].field1);
    an anonymous type is created than placed into ArrayList.
    al[0] obviously returns Object, how to cast this back to anonymous type to retrieve value of 'field1'?
    See more | Go to post
No activity results to display
Show More
Working...