PropertyDescriptor.SetValue(Object, Object) 方法


当在派生类中被重写时,将组件的值设置为一个不同的值。When overridden in a derived class, sets the value of the component to a different value.

 abstract void SetValue(System::Object ^ component, System::Object ^ value);
public abstract void SetValue (object component, object value);
abstract member SetValue : obj * obj -> unit
Public MustOverride Sub SetValue (component As Object, value As Object)



具有要进行设置的属性值的组件。The component with the property value that is to be set.


新值。The new value.


通常,此方法是通过反射实现的。Typically, this method is implemented through reflection.

此方法按以下顺序自动创建 DesignerTransactionThis method creates a DesignerTransaction automatically in the following order:

  1. 方法调用 IDesignerHost.CreateTransaction 方法来创建新的 DesignerTransaction 以表示更改。The method calls the IDesignerHost.CreateTransaction method to create a new DesignerTransaction to represent the changes.

  2. 方法调用 IComponentChangeService.OnComponentChanging 方法,以指示事务已开始,即将发生更改。The method calls the IComponentChangeService.OnComponentChanging method to indicate that the transaction has begun and the changes are about to occur.

  3. 方法将属性重置为此方法的检查顺序确定的值。The method resets the property to the value determined by this method's checking order.

  4. 方法调用 IComponentChangeService.OnComponentChanged 方法以指示已发生更改。The method calls the IComponentChangeService.OnComponentChanged method to indicate that the changes have occurred.

  5. 方法调用 DesignerTransaction.Commit 以指示事务已完成。The method calls DesignerTransaction.Commit to indicate that the transaction is complete.

事务的目的是支持 UndoRedo 功能。The purpose of the transaction is to support Undo and Redo functionality.


重写此方法时,应通过调用需要实现的适当 "SetMyProperty" 方法来设置属性的值。When you override this method, it should set the value of the property by invoking the appropriate "SetMyProperty" method that you need to implement. 如果指定的值无效,则该组件应引发异常,该异常会向上传递。If the value specified is not valid, the component should throw an exception, which is passed up. 你应设计属性,以便在 "SetMyProperty" 方法之后的 "GetMyProperty" 方法(已实现)在 "SetMyProperty" 方法不引发异常时返回传入的值。You should design the property so that a "GetMyProperty" method (that you have implemented) following a "SetMyProperty" method returns the value passed in when the "SetMyProperty" method does not throw an exception.