本文整理汇总了C#中System.ComponentModel.PropertyDescriptor类的典型用法代码示例。如果您正苦于以下问题:C# PropertyDescriptor类的具体用法?C# PropertyDescriptor怎么用?C# PropertyDescriptor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PropertyDescriptor类属于System.ComponentModel命名空间,在下文中一共展示了PropertyDescriptor类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
// Creates a new collection and assign it the properties for button1.
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(button1);
// Sets an PropertyDescriptor to the specific property.
System.ComponentModel.PropertyDescriptor myProperty = properties.Find("Text", false);
// Prints the property and the property description.
textBox1.Text = myProperty.DisplayName+ '\n' ;
textBox1.Text += myProperty.Description + '\n';
textBox1.Text += myProperty.Category + '\n';
示例2: SerializeReadOnlyPropertyDescriptor
//引入命名空间
using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
namespace ReadOnlyPropertyDescriptorTest
{
// The SerializeReadOnlyPropertyDescriptor shows how to implement a
// custom property descriptor. It provides a read-only wrapper
// around the specified PropertyDescriptor.
internal sealed class SerializeReadOnlyPropertyDescriptor : PropertyDescriptor
{
private PropertyDescriptor _pd = null;
public SerializeReadOnlyPropertyDescriptor(PropertyDescriptor pd)
: base(pd)
{
this._pd = pd;
}
public override AttributeCollection Attributes
{
get
{
return( AppendAttributeCollection(
this._pd.Attributes,
ReadOnlyAttribute.Yes) );
}
}
protected override void FillAttributes(IList attributeList)
{
attributeList.Add(ReadOnlyAttribute.Yes);
}
public override Type ComponentType
{
get
{
return this._pd.ComponentType;
}
}
// The type converter for this property.
// A translator can overwrite with its own converter.
public override TypeConverter Converter
{
get
{
return this._pd.Converter;
}
}
// Returns the property editor
// A translator can overwrite with its own editor.
public override object GetEditor(Type editorBaseType)
{
return this._pd.GetEditor(editorBaseType);
}
// Specifies the property is read only.
public override bool IsReadOnly
{
get
{
return true;
}
}
public override Type PropertyType
{
get
{
return this._pd.PropertyType;
}
}
public override bool CanResetValue(object component)
{
return this._pd.CanResetValue(component);
}
public override object GetValue(object component)
{
return this._pd.GetValue(component);
}
public override void ResetValue(object component)
{
this._pd.ResetValue(component);
}
public override void SetValue(object component, object val)
{
this._pd.SetValue(component, val);
}
// Determines whether a value should be serialized.
public override bool ShouldSerializeValue(object component)
{
bool result = this._pd.ShouldSerializeValue(component);
if (!result)
{
DefaultValueAttribute dva = (DefaultValueAttribute)_pd.Attributes[typeof(DefaultValueAttribute)];
if (dva != null)
{
result = !Object.Equals(this._pd.GetValue(component), dva.Value);
}
else
{
result = true;
}
}
return result;
}
// The following Utility methods create a new AttributeCollection
// by appending the specified attributes to an existing collection.
static public AttributeCollection AppendAttributeCollection(
AttributeCollection existing,
params Attribute[] newAttrs)
{
return new AttributeCollection(AppendAttributes(existing, newAttrs));
}
static public Attribute[] AppendAttributes(
AttributeCollection existing,
params Attribute[] newAttrs)
{
if (existing == null)
{
throw new ArgumentNullException(nameof(existing));
}
newAttrs ??= new Attribute[0];
Attribute[] attributes;
Attribute[] newArray = new Attribute[existing.Count + newAttrs.Length];
int actualCount = existing.Count;
existing.CopyTo(newArray, 0);
for (int idx = 0; idx < newAttrs.Length; idx++)
{
if (newAttrs[idx] == null)
{
throw new ArgumentNullException("newAttrs");
}
// Check if this attribute is already in the existing
// array. If it is, replace it.
bool match = false;
for (int existingIdx = 0; existingIdx < existing.Count; existingIdx++)
{
if (newArray[existingIdx].TypeId.Equals(newAttrs[idx].TypeId))
{
match = true;
newArray[existingIdx] = newAttrs[idx];
break;
}
}
if (!match)
{
newArray[actualCount++] = newAttrs[idx];
}
}
// If some attributes were collapsed, create a new array.
if (actualCount < newArray.Length)
{
attributes = new Attribute[actualCount];
Array.Copy(newArray, 0, attributes, 0, actualCount);
}
else
{
attributes = newArray;
}
return attributes;
}
}
}
示例3: PostFilterProperties
//引入命名空间
using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms.Design;
namespace ReadOnlyPropertyDescriptorTest
{
class DemoControlDesigner : ControlDesigner
{
// The PostFilterProperties method replaces the control's
// Size property with a read-only Size property by using
// the SerializeReadOnlyPropertyDescriptor class.
protected override void PostFilterProperties(IDictionary properties)
{
if (properties.Contains("Size"))
{
PropertyDescriptor original = properties["Size"] as PropertyDescriptor;
SerializeReadOnlyPropertyDescriptor readOnlyDescriptor =
new SerializeReadOnlyPropertyDescriptor(original);
properties["Size"] = readOnlyDescriptor;
}
base.PostFilterProperties(properties);
}
}
}
示例4: DemoControl
//引入命名空间
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ReadOnlyPropertyDescriptorTest
{
[Designer(typeof(DemoControlDesigner))]
public class DemoControl : Control
{
public DemoControl()
{
}
}
}