且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何处理财产变更价值而不进行调查。

更新时间:2023-02-05 22:49:56

这是不可能的没有意义。如果你想处理一些事件,当 MyObject 类型的实例的某个属性改变它的值时触发,你需要有一个事件 PropertyChanged 此类型中没有其他地方不在 MyCaller 或其他类型。



说我不知道拥有 MyObject 的源代码在这个故事中设置一个句点。你不能这样使用这种类型。但是,您可以在派生类中执行此操作。



如果属性 MyObject.Name 是虚拟的,那么您可以在你的类中覆盖它,你可以添加 PropertyChanged 事件,在 MyObject.Name 的setter中调用它。反过来,这将使您的派生类密封此事件的调用,因为任何事件实例只能在其声明类中调用,这可以被视为一个重要的傻瓜式功能。请参阅我以前的回答:

UserControl自定义事件 [ ^ ]。



但由于其非虚拟声明,您的类 MyObject 在不更改其代码的情况下因其属性行为的任何修改而完全关闭。无论如何,作为一个图书馆类,它在各方面都是无用的。 :-)



-SA


for Example Let that i have a 'MyObject' class

public class MyObject
{
    public string Name { get; set; }
}



if i want know if Name Property is Change Without Using

public event PropertyChangedEventHandler PropertyChanged;


I will do My caller class Like This:

public class MyCaller 
{
   private MyObject myObject;
   private string InitialName;
   private Thread myThread;  
   public event PropertyChangedEventHandler PropertyChanged;
   public MyCaller()
   {
      this.myObject = new MyObject();
      this.InitialName = this.myObject.Name;
      this.Thread  = new Thread(new ThreadStart(this.HandleName));
   } 
   
   private void HandleName()
   {
      while (true)
      {
        if (this.InitialName != this.myObject.Name)
        {
           this.InitialName = this.myObject.Name;
           if (this.PropertyChanged != null)
           {
              this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
           }
        }
      }
   }
} 



I Know that i can create PropertyChanged event on MyObject Class
but if i dont have the source code for MyObject Class I will do the another
solution.

My Question is there another way to solve this problem.


please, send me the answer at my Mail : [REMOVED EMAIL]

thank you

It is impossible and makes no sense. If you want to handle some event triggered when some property of the instance of the type MyObject changes its value, you need to have the event PropertyChanged in this type and nowhere else. Not in MyCaller or some other type.

Saying "I don't have the source code for MyObject" sets a period in this story. You cannot use this type this way. However, you can do it in a derived class.

If the property MyObject.Name was virtual, you could override it in your class where you could add the PropertyChanged event, invoking it in the setter of MyObject.Name. That would, in turn, make your derived class sealing the invocation of this event, because any event instance can only be invoked in its declaring class, which can be considered as an important fool-proof feature. Please see my past answer:
UserControl custom event[^].

But you class MyObject, without changing its code, is completely closed for any modifications of its property behavior, due to its non-virtual declaration. Anyway, as a library class it would be useless in all respects. :-)

—SA