【问题标题】:'ExpandoObject' does not contain a definition for 'PropertyChanged'“ExpandoObject”不包含“PropertyChanged”的定义
【发布时间】:2016-12-28 17:39:45
【问题描述】:

我正在尝试使用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();
}

我收到一条错误消息,指出

'System.Dynamic.ExpandoObject' 不包含定义 '属性已更改'

每次我尝试在构造函数中连接事件处理程序时都会发生这种情况。

事件日志 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吗?

【问题讨论】:

  • 从我玩它的几分钟开始,它看起来像ImpromptuInterface 的一个错误。我会report the bug

标签: c# dynamic interface expandoobject impromptu-interface


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    相关资源
    最近更新 更多