隐藏继承的成员(PropertyChanged) [英] Hiding inherited member (PropertyChanged)

查看:61
本文介绍了隐藏继承的成员(PropertyChanged)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我有数百个类,所有类都实现了INotifyPropertyChanged接口。所有的类都是单独使用的,但也有类的情况,因此编译器会给出很多警告,基类和派生类都有PropertyChanged事件。



ClassA从ClassB隐藏继承的成员'PropertyChanged'。如果想要隐藏,请使用new关键字。



所以如果我在每个班级中声明



公共事件PropertyChangedEventHandler PropertyChanged; 





as

新的公共事件PropertyChangedEventHandler PropertyChanged; 





如果这样做是好的吗?单独使用类或在继承发生的情况下数据绑定是否会工作?



注意:我知道使用baseclass为所有类提供PropertyChanged事件将是理想的解决方案,但不幸的是,在这种情况下,普通基类不是一种选择。



也想听听是否有其他选择



希望你有个主意:)



干杯!

解决方案

如果可以的话你知道为什么你躲在第一位,但是你不知道并问了一个问题,这很可能是你的错。此关键字和此警告专门为此设计。



在此上下文中, new 表示以下内容:



嘿,编辑同志,不要太聪明。我知道自己隐藏 PropertyChanged 看起来很愚蠢。我故意这样做肮脏的伎俩。实际上,我不需要这个成员。我只是喜欢这个成员的名字而想用它来做一些语义相似,但不相关的事件,这就是为什么我使用相同的名称,作为普通编码实践的罕见排除。是的,我理解,如果我需要使用继承的 PropertyChanged ,我将能够通过限定名称 base.PropertyChanged 使用它。所以,请关闭你的愚蠢警告;我知道我在做什么。




不要误解这个 new 和构造函数调用。



当你提出这个问题时,情况就不是这样了伊利。使用其他名称创建不同的事件。使用相同名称的唯一情况是覆盖虚拟方法或实现接口方法。这是你应该非常了解的OOP的核心。隐藏与此没有任何共同之处。如果您没有明确指定覆盖但使用相同的名称,则只需将继承的成员隐藏在一个完全不同的成员后面并获得警告;您可以使用 new 来取消此警告。但你不能覆盖一个事件,所以你所做的只是隐藏。在你的情况下,我没有理由这样做。







我基本了解你的目的。您只需要实现接口 INotifyPropertyChanged 。隐藏会让你的新实现完全是假的。您需要在不同的类中实现它。如果一个公共库不是一个选项,则可以使用几个公共基类和虚拟实现方法。另一种常用方法是某些实现者类的组成成员。 (我不知道这是一个有着名的设计模式,但这是一个典型的,着名的设计模式。)



-SA


如果某个类静态继承了实现INotifyPropertyChanged的内容,则无需再次显式实现它。只需确保基类提供了一种通知您仍然可以使用的方法(即触发事件的方法受到保护而不是私有)。



对于更广泛的问题我推荐你到SAK的答案。


不确定这个问题是否得到解决。如果有人遇到类似的问题,这是解决方案之一。我刚刚通过更改属性更改事件处理程序名称解决了这个问题。



基类的名称为PropertyChanged,我在派生类中使用了相同的名称,遇到这个警告。我只是将PropertyChanged更改为xxx_PropertyChanged,其中xxx是您可以提供的合适属性/对象名称。摆脱了这个警告。



SA的解释说明了一切。


Hi all!

I have hundreds of classes and all the classes implement INotifyPropertyChanged interface. All the classes are used individually but there are also cases where classes inhert each other so compiler gives lots of warnings that baseclass and derived class both have PropertyChanged event.

"ClassA hides inherited member 'PropertyChanged' from ClassB. Use the new keyword if hiding was intended."

So if I declare in every class

public event PropertyChangedEventHandler PropertyChanged;



as

new public event PropertyChangedEventHandler PropertyChanged;



So is it Ok if do this? Will databinding work when using class alone OR in scenarios where inheritation occur?

NOTE: I know that using baseclass wich offers PropertyChanged event with all the classes would be ideal solution, but unfortunatelly in this case common base class is not an option.

Also like to hear if there is another options

Hope you got idea :)

Cheers!

解决方案

It would be OK if you knew why are you hiding in first place, but as you did not know and asked a question, this is most likely your mistake. This new keyword and this warning are designed specially for that.

In this context, the new means the following:

"Hey, comrade compiler, don't be so smart. I know myself that hiding PropertyChanged looks stupid. I do this dirty trick on purpose. Actually, I don't need this member. I just like the name of this member and want to use it for something semantically similar, but unrelated event, that's why I use the same name, as a rare exclusion from normal coding practices. Yes, I understand that in case I need to use inherited PropertyChanged, I will be able to use it through the qualified name base.PropertyChanged. So, please shut up your stupid warning; I know what I am doing."



Don't mist up this new and a constructor call.

As you are asking this question, this is not the case, most likely. Create a different event with some other name. The only case where you use the same name is when you override a virtual method or implement an interface method. This is a heart of OOP which you are supposed to know very well. Hiding has nothing in common with that. If you are not explicitly specify "override" but use the same name, you simply hide the inherited member behind a completely different member and get the warning for that; you can suppress this warning with new. But you cannot override an event, so all you do is hiding. I don't see a reason for doing it in your case.

[EDIT]

I basically understand your purpose. You just need to implement the interface INotifyPropertyChanged. Hiding would make your new "implementation" of the class totally fake. You need to just implement it in different classes. If one common base is not an option, you can use several common base classes with virtual implementation method. Another usual approach is a composed member of some "implementor class". (I don't know if this is a design pattern with a well-known name, but this is a typical and well-known design pattern.)

—SA


If a class is statically inheriting from something which implements INotifyPropertyChanged, you don't need to explicitly implement it again. Just make sure the base class provides a way of notifying that you can still use (i.e. the method to fire the event is protected not private).

For the wider issues I refer you to SAK's answer.


Not sure if this issue is resolved. This is one of the solutions should there be similar issue someone faces. I just resolved this one by changing the property changed event handler name.

The baseclass had this name "PropertyChanged", I had used the same name in my derived class and encountered this warning. I simply changed "PropertyChanged" to "xxx_PropertyChanged", where "xxx" is a suitable property/object name that you could provide. Got rid of this warning.

SA's explanation says it all.


这篇关于隐藏继承的成员(PropertyChanged)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆