在实现ICustomTypeDescriptor时,我偶然发现了GetDefaultProperty方法。我刚刚实现了使用TypeDescriptor.GetDefaultProperty,然后我想知道:我实际上不知道这个方法的目的是什么。我猜一个人最有可能首先编辑的属性,比如TextBox.Text,但我似乎无法证实这个想法。
MSDN在这方面没有任何帮助,他说:
返回组件的此实例的默认属性。
文档说返回null是针对没有属性的对象。那么,如果我们为一个3d Box类创建一个ICustomTypeDescriptor,会怎样呢?
public class Box {
public double Length { get; set; }
public double Width { get; set; }
public double Height { get; set; }
}我应该使用什么财产?
发布于 2022-11-15 14:15:00
此方法返回等效的https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.defaultpropertyattribute。它并没有真正精确地指定默认属性(或事件)应该是什么,但您的猜测与常用用法相匹配:当用户最有可能感兴趣的单个属性时,可以以这种方式标记它,例如请参阅https://github.com/dotnet/runtime/blob/e55c908229e36f99a52745d4ee85316a0e8bb6a2/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrintDocument.cs (其名称)或https://github.com/dotnet/runtime/blob/912b04dfd1efea3b0b3ad7586cf73c5d7ffc0c1c/src/libraries/System.ComponentModel.TypeConverter/src/System/Timers/Timer.cs (间隔)。
如果没有适用的方法,返回null实际上是很好的;框架至少在几个地方这样做。
https://stackoverflow.com/questions/34792192
复制相似问题