简体   繁体   English

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

[英]'ExpandoObject' does not contain a definition for 'PropertyChanged'

I am attempting to use ImpromptuInterface to solve the issue I am having here. 我试图使用ImpromptuInterface来解决我在这里遇到的问题。 Adding Interface Implementation to ExpandoObject . 将接口实现添加到ExpandoObject

I am now able to access various properties of my interface in my base class but I can no longer subscribe to ExpandoObject's PropertyChanged event. 我现在能够在我的基类中访问我的接口的各种属性,但我不能再订阅ExpandoObject的PropertyChanged事件。

While troubleshooting I was able to simplify the issue as shown. 在排除故障时,我能够简化问题,如图所示。

Service.cs 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();
}

I receive an error stating that 我收到一条错误说明

'System.Dynamic.ExpandoObject' does not contain a definition for 'PropertyChanged' 'System.Dynamic.ExpandoObject'不包含'PropertyChanged'的定义

It occurs the each time I attempt to hook up the event handler in the constructor. 每次我尝试在构造函数中挂钩事件处理程序时都会发生这种情况。

Event Log 1 事件日志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()

Event Log 2 事件日志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()

Am I not allowed to use ImpromptuInterface this way? 我不允许这样使用ImpromptuInterface吗?

The issue comes from the fact that ImpromptuInterface uses the DLR, and the DLR won't working with explicit interface calls, which is how it's implemented on Expando. 问题来自ImpromptuInterface使用DLR,DLR无法使用显式接口调用,这是在Expando上实现的方式。 It might be possible to be fixed generically by having the proxy check if it's target implements the exact interface impromptu is wrapping. 通过让代理检查它的目标是否实现了即兴包装的确切接口,可以一般地修复它。 I'd have to think about it more. 我不得不考虑更多。 Tracking with this issue . 跟踪此问题

As a work around for this specific issue, Dynamitey.Dynamic.Dictionary : BaseDictionary works just like expando, and has PropertyChanged as a normal event property. 作为解决此特定问题的方法, Dynamitey.Dynamic.DictionaryBaseDictionary的工作原理与expando类似,并将PropertyChanged作为普通事件属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM