none
Creating a parameter to accept an anonymous type RRS feed

  • Question

  • I am trying to figure out how I can create a constructor in a class that I'm trying to create, that can take in an anonymous type such as new { someval = "value", someval2 = "val2" } for example.  And then how to take that anonymous type and parse it in the class that I'm creating an instance of.
    C# Web Developer
    Tuesday, June 23, 2009 7:22 PM

Answers

  • A constructor cannot take an anonymous type.  The best it can do is take a value of the type object and then you could use reflection to find the information within the class.  Either that, or you could wait until VS 2010 and .NET 4.0 comes out and you could use the dynamic type.  Either way, I wouldn't go there.  Write a class. 
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marked as answer by NoEgo Wednesday, June 24, 2009 12:34 AM
    Tuesday, June 23, 2009 7:24 PM
    Moderator
  • Hi,

    Looks like you have a solution but another idea would have been to use generics. Create a generic class and pass the type information in. 

    Something like this off the top of my head.....

    public class TheClass<T>

         public TheClass(T value)

        

    www.dsmyth.net | www.dsmyth.net/wiki
    • Marked as answer by NoEgo Wednesday, June 24, 2009 12:35 AM
    Tuesday, June 23, 2009 8:56 PM
  • nevermind, type object.  Then I used RouteDictionary to convert it to IDictionary<string, object> so I can iterate through it as shown here: http://www.asp.net/LEARN/mvc/tutorial-35-cs.aspx
    C# Web Developer
    • Edited by NoEgo Wednesday, June 24, 2009 12:34 AM
    • Marked as answer by NoEgo Wednesday, June 24, 2009 12:35 AM
    • Marked as answer by NoEgo Wednesday, June 24, 2009 12:35 AM
    Tuesday, June 23, 2009 7:37 PM

All replies

  • A constructor cannot take an anonymous type.  The best it can do is take a value of the type object and then you could use reflection to find the information within the class.  Either that, or you could wait until VS 2010 and .NET 4.0 comes out and you could use the dynamic type.  Either way, I wouldn't go there.  Write a class. 
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marked as answer by NoEgo Wednesday, June 24, 2009 12:34 AM
    Tuesday, June 23, 2009 7:24 PM
    Moderator
  • nevermind, type object.  Then I used RouteDictionary to convert it to IDictionary<string, object> so I can iterate through it as shown here: http://www.asp.net/LEARN/mvc/tutorial-35-cs.aspx
    C# Web Developer
    • Edited by NoEgo Wednesday, June 24, 2009 12:34 AM
    • Marked as answer by NoEgo Wednesday, June 24, 2009 12:35 AM
    • Marked as answer by NoEgo Wednesday, June 24, 2009 12:35 AM
    Tuesday, June 23, 2009 7:37 PM
  • Hi,

    Looks like you have a solution but another idea would have been to use generics. Create a generic class and pass the type information in. 

    Something like this off the top of my head.....

    public class TheClass<T>

         public TheClass(T value)

        

    www.dsmyth.net | www.dsmyth.net/wiki
    • Marked as answer by NoEgo Wednesday, June 24, 2009 12:35 AM
    Tuesday, June 23, 2009 8:56 PM