如何处理财产变更价值而不进行调查。 [英] How To Handle Property Change Value Without Inquiry it.

查看:57
本文介绍了如何处理财产变更价值而不进行调查。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例让我有一个'MyObject'类



  public   class  MyObject 
{
public 字符串名称{获取; set ; }
}





如果我想知道名称属性是否更改而不使用

  public   event  PropertyChangedEventHandler PropertyChanged; 



我会做我的来电课这样:



<前lang =c#> public class MyCaller
{
private MyObject myObject;
private string InitialName;
private 线程myThread;
public event PropertyChangedEventHandler PropertyChanged;
public MyCaller()
{
this .myObject = < span class =code-keyword> new MyObject();
this .InitialName = this .myObject.Name;
this .Thread = new 主题( 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));
}
}
}
}
}





我知道我可以在MyObject类上创建PropertyChanged事件

但是如果我没有MyObject类的源代码,我会做另一个

解决方案。



我的问题是有另一种解决这个问题的方法。





拜托,给我答案在我的邮件中:[删除电子邮件]



谢谢

解决方案

这是不可能的没有意义。如果你想处理一些事件,当 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


这篇关于如何处理财产变更价值而不进行调查。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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