views:

553

answers:

4
+1  Q: 

Anonymous types

Hello,

My first try to use anonymous types(test):

    private void button4_Click(object sender, EventArgs e)
    {
        test(new { a = "asd" });
    }

    private void test(string a)
    {
    }

I get an error "cannot convert from 'AnonymousType#1' to 'string' "

Also I'd like to know how to pass an anonymous type if the parameter is a string[]

    private void test(string[] a)
    {
    }

Thanks in advance!

+2  A: 

There is no clean way to pass an anonymous type between methods; you're not meant to do it. Create a real type instead.

If you really really want to do this, you can use a helper method to fake it awkwardly by creating another "template" of that type to cast to later; see this and many similar articles.

EDIT: On closer examination, you don't appear to understand what an anonymous type actually is, so I suggest you take the other answers.

mquander
Actually,I'd like ot take your answer,show me how it should look like using an anonymous type.
John
John, check the link in mquander's post.
John Fisher
Alright I understood the parameter should be object,but how do I get the string[] out of the object?
John
Edit:Please provide an example,I still get errors.
John
+5  A: 

Something about your design is faulty. If your test function accepts only strings, then you can never pass an anonymous type to it, just as you can't pass an int, object, or any other non-string type.

John Fisher
+4  A: 

a is a string property on your anonymous type

private void button4_Click(object sender, EventArgs e)
{
    test((new { a = "asd" }).a);
}

private void test(string a)
{
}

Edit: Anonymous types do not derive from anything other than object so you cannot create a method that expects an anonymous type parameter.

Edit 2: when you create an anonymous type the compiler create an entirely new type based on the properties you set and the order in which they appear. You cannot create an anonymous type and use it in place of any other type (other than object). The most common scenario I've used them in is for binding when you need to flatten your object graph.

warning, I am horrible at coming up with good example scenarios and this is all from memory! for example if I had a list of Person objects that had a name property and an address property that contained the street address and needed to bind to a list box

var people = new List<Person>()
listbox.TextMember = "Text";
listbox.ValueMember = "Value"
listbox.DataSource = from p in people 
select new { Text = p.Name, Value = p.Address.StreetAddress };
Venr
@Venr, This won't work with a string[] array.
John
@Venr,thanks for the update! You seem to be the only one still viewing this question.I understood from the answers above that the parameter in the void "test" should be object.So I have one parameter - object.I want to pass a string[] array to it using an anonymous type,how?
John
@ John, if you have a method that takes a string array as the parameter, pass it a string array. You would not create an anonymous type for this, typically you create an anonymous type because you want a custom property container. but just for the sake of argument, either define your anonymous type to include a string[] property or construct a new string[] with the string property from your anonymous type and pass it to the method.
Venr
Alright,Thanks.
John
A: 

This is bad, but it works ...

namespace AnonType
{
    class Program
    {
        static void Main(string[] args)
        {
            var foo = new { a = "123", b = "abc" };

            Anon(foo);
            Console.ReadLine();
        }

        public static void Anon(object o)
        {
            Console.WriteLine(o);
        }
    }
}
JP Alioto