在属性上触发一个事件/功能? (C#)属性、事件、功能

由网友(等太阳升起)分享简介:我使用,我不能编辑类,它有一个属性(一个布尔值),其中,这将是很好当它改变时被告知,我不能编辑属性获取或设置为我导入类从一个.dll(我没有$ C $下)。如何创建当特性改变所触发的事件/功能?其他 它仅是在其自己的类发生变化,直接到底层的私有变量。例如。私人布尔m_MyValue = FALSE;公共BOOL my...

我使用,我不能编辑类,它有一个属性(一个布尔值),其中,这将是很好当它改变时被告知,我不能编辑属性获取或设置为我导入类从一个.dll(我没有$ C $下)。

如何创建当特性改变所触发的事件/功能?

其他 它仅是在其自己的类发生变化,直接到底层的私有变量。

例如。

 私人布尔m_MyValue = FALSE;

公共BOOL myvalue的
  {
  {返回m_MyValue; }
  }

私人无效SomeFunction()
  {
  m_MyValue = TRUE;
  }
 

解决方案 工作十余年,还是一直被问 委托和事件 有什么区别 真是够了

您不能,基本上......不是没有使用类似的调试器API注入code的执行时间和修改IL原库(和我不建议任何的解决方案,除了别的它可能违反了图书馆的许可证)

基本上如果一个属性不支持通知,它不支持通知。你应该寻找接近你的问题的不同方式。 (可否民意调查工作,例如?)

I am using a class that I cannot edit, it has a property (a boolean) of which it would be nice to be informed when it changes, I can't edit the properties get or set as I am importing the class from a .dll (which I don't have the code for).

How do I create an event/function that is fired when the property is changed?

Additional It is only changed within its own class, directly to the underlying private variable.

E.g.

private bool m_MyValue = false;

public bool MyValue
  {
  get { return m_MyValue; }
  }

private void SomeFunction()
  {
  m_MyValue = true;
  }

解决方案

You can't, basically... not without using something like the debugger API to inject code at execution time and modifying the IL of the original library (and I'm not recommending either of those solutions; aside from anything else it may violate the licence of the library).

Basically if a property doesn't support notification, it doesn't support notification. You should look for a different way of approaching your problem. (Would polling work, for example?)

阅读全文

相关推荐

最新文章