Lost
Lost

Reputation: 13655

using expando is erroring out on compile time

I have following code just as a little sample of Expando

using System; using System.Collections.Generic; using System.Dynamic;

namespace IssueCatalogExample
{
    class IssueCatalogUsingExpando
    {
         dynamic _expando = new ExpandoObject();
        _expando.something = new ExpandoObject();
    }
}

For some reason when I say dynamic _expando = new ExpandoObject();, it is all fine. But when it goes to next line of code where I say _expando.something it says that "cannot resolve symbol expando." I am not sure why is that. I pretty much remember from my previous experience that it dynamically will create that member and there would be no compile time errors but it does not seem to be the case here. Any suggestions?

Upvotes: 0

Views: 144

Answers (1)

Igby Largeman
Igby Largeman

Reputation: 16747

Only declarations can occur at the class level. You are trying to do a property assignment:

_expando.something = new ExpandoObject();

which can only occur inside a method body. For example, you could put it in the constructor:

class IssueCatalogUsingExpando
{         
    dynamic _expando = new ExpandoObject();

    public IssueCatalogUsingExpando() 
    {
         _expando.something = new ExpandoObject();
         ...
    }
}

Upvotes: 1

Related Questions