Anonymous type as method parameter

My first attempt at using anonymous types (test):

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

    private void test(string a)
    {
    }

      

I am getting the error "cannot convert from" Anonymous type # 1 "to string"

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

    private void test(string[] a)
    {
    }

      

Thanks in advance!

+1


a source to share


5 answers


a is a property of the string in your anonymous type

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

private void test(string a)
{
}

      

Edit: Anonymous types are not inferred from anything other than an object, so you cannot create a method expecting an anonymous type.



Edit 2: When you create an anonymous type, the compiler creates a completely 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 an object). The most common scenario I have used them in is snapping when you need to flatten an object graph.

warning, I'm terrible at coming up with good example scripts, and it's all from memory! for example, if I have a list of Person objects that have a name property and an address property containing a street address and needed to bind to a list of list

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 };

      

+4


a source


Something in your design is wrong. If your test function only accepts strings, then you can never pass it an anonymous type, just like you cannot pass an int, object, or any other non-string type.



+5


a source


There is no clean way to pass an anonymous type between methods; you are not going 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 this type to add later; see this and many similar articles.

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

+2


a source


Just stumbled upon this outdated question and thought someone here should mention the ability to use dynamic

starting in C # 4.0 which is perfect for working with anonymous types. That is, the code might look like this:

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

private void test(dynamic obj)
{
    Console.WriteLine(obj.a);
}

      

0


a source


It's 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);
        }
    }
}

      

-1


a source







All Articles