Search code examples
c#dynamicmvvmmef

ExpandoObject in MEF Export


Edit: ExpandoObject works fine with MEF. I just had a syntax error. Silly me.


I am currently using ExpandoObjects to dynamically create DelegateCommand's for my ViewModels.

private dynamic _commands = new ExpandoObject();
public dynamic Commands
{
    get
    {
        return _commands;
    }
}

private void initializeCommands()
{
    _commands.TestSql(new DelegateCommand(() => testSqlConnection()));
}

This command is used in my View.

<Button Content="Test Connection" Command={Binding Commands.TestSql}/>

I am now updating the code to use MEF and it is working great for most modules. However, when this ViewModel is being composed, I get the following exception:

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) 'System.Dynamic.ExpandoObject' does not contain a definition for 'TestSql'

This error makes sense since ExpandoObject doesn't have a property with this name. It should be created dynamically. Has anyone dealt with ExpandoObjects in MEF? I could always get rid of the ExpandoObject if I have to, but I was wondering if there is an easy fix that would allow this situation to work.


Solution

  • Have you tried this?:

    private void initializeCommands()
    {
        _commands.TestSql = new DelegateCommand( () => testSqlConnection());
    }