' ExpandoObject'不包含' PropertyChanged'的定义

时间:2016-12-28 17:39:46

标签: c# dynamic interface expandoobject impromptu-interface

我正在尝试使用ImpromptuInterface来解决我在这里遇到的问题。 Adding Interface Implementation to ExpandoObject

我现在能够在我的基类中访问我的界面的各种属性,但我不能再订阅ExpandoObject的PropertyChanged事件。

在排除故障时,我能够简化问题,如图所示。

Service.cs

using ImpromptuInterface;

public Service()
{
    InitializeComponent();

    dynamic expando = new ExpandoObject();

    try
    {
        INotifyPropertyChanged obj = Impromptu.ActLike(expando);

        obj.PropertyChanged += obj_PropertyChanged;
    }
    catch (Exception ex)
    {
        EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
    }

    try
    {
        INotifyPropertyChanged obj = Impromptu.ActLike<INotifyPropertyChanged>(expando);

        obj.PropertyChanged += obj_PropertyChanged;
    }
    catch (Exception ex)
    {
        EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
    }
}

private void obj_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    throw new NotImplementedException();
}

我收到一条错误说明

  

&#39; System.Dynamic.ExpandoObject&#39;不包含的定义   &#39;的PropertyChanged&#39;

每次我尝试在构造函数中挂钩事件处理程序时都会发生。

事件记录1

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Dynamic.ExpandoObject' does not contain a definition for 'PropertyChanged'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at ActLike_INotifyPropertyChanged_dc51b6c65bf147d0b5f35218102e3c11.add_PropertyChanged(PropertyChangedEventHandler value)
   at Service..ctor()

事件记录2

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Dynamic.ExpandoObject' does not contain a definition for 'PropertyChanged'
   at CallSite.Target(Closure , CallSite , Object )
   at ActLike_INotifyPropertyChanged_dc51b6c65bf147d0b5f35218102e3c11.add_PropertyChanged(PropertyChangedEventHandler value)
   at Service..ctor()

我不允许以这种方式使用ImpromptuInterface吗?

1 个答案:

答案 0 :(得分:2)

问题来自于ImpromptuInterface使用DLR,而DLR无法使用显式接口调用,这是在Expando上实现的方式。通过让代理检查它的目标是否实现了即兴包装的确切接口,可以一般地修复它。我不得不考虑更多。使用此issue进行跟踪。

作为针对此特定问题的解决方法,Dynamitey.Dynamic.DictionaryBaseDictionary的工作方式与expando类似,并且将PropertyChanged作为普通事件属性。

相关问题