C# 将ICustomTypeDescriptor.GetProperties返回的属性动态更改为只读

C# 将ICustomTypeDescriptor.GetProperties返回的属性动态更改为只读,c#,.net,propertydescriptor,icustomtypedescriptor,getproperties,C#,.net,Propertydescriptor,Icustomtypedescriptor,Getproperties,我有一个实现ICustomTypeDescriptor的类,它由用户在PropertyGrid中查看和编辑。我的类还有一个IsReadOnly属性,用于确定用户以后是否能够保存其更改。如果用户无法保存,我不想允许他们进行更改。因此,如果IsReadOnly为true,我希望覆盖在属性网格中可编辑为只读的任何属性 我试图使用ICustomTypeDescriptor的GetProperties方法为每个PropertyDescriptor添加ReadOnlyAttribute。但它似乎不起作用。这

我有一个实现ICustomTypeDescriptor的类,它由用户在PropertyGrid中查看和编辑。我的类还有一个IsReadOnly属性,用于确定用户以后是否能够保存其更改。如果用户无法保存,我不想允许他们进行更改。因此,如果IsReadOnly为true,我希望覆盖在属性网格中可编辑为只读的任何属性

我试图使用ICustomTypeDescriptor的GetProperties方法为每个PropertyDescriptor添加ReadOnlyAttribute。但它似乎不起作用。这是我的密码

 public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
 {
    List<PropertyDescriptor> fullList = new List<PropertyDescriptor>();

    //gets the base properties  (omits custom properties)
    PropertyDescriptorCollection defaultProperties = TypeDescriptor.GetProperties(this, attributes, true);

    foreach (PropertyDescriptor prop in defaultProperties)
    {
        if(!prop.IsReadOnly)
        {
            //adds a readonly attribute
            Attribute[] readOnlyArray = new Attribute[1];
            readOnlyArray[0] = new ReadOnlyAttribute(true);
            TypeDescriptor.AddAttributes(prop,readOnlyArray);
        }

        fullList.Add(prop);
    }

    return new PropertyDescriptorCollection(fullList.ToArray());
}
公共属性描述或集合GetProperties(属性[]属性) { List fullList=新列表(); //获取基本属性(忽略自定义属性) PropertyDescriptorCollection defaultProperties=TypeDescriptor.GetProperties(this,attributes,true); foreach(defaultProperties中的PropertyDescriptor属性) { 如果(!prop.IsReadOnly) { //添加只读属性 属性[]readOnlyArray=新属性[1]; readOnlyArray[0]=新的ReadOnlyAttribute(true); AddAttributes(prop,readOnlyArray); } 完整列表。添加(道具); } 返回新的PropertyDescriptorCollection(fullList.ToArray()); }
这是使用TypeDescriptor.AddAttributes()的正确方法吗?在调用后进行调试时,AddAttributes()prop仍然具有相同数量的属性,其中没有一个是ReadOnlyAttribute

TypeDescriptor.AddAttributes
将类级属性添加到给定的对象或对象类型,而不是属性级属性。除此之外,我认为除了返回的
TypeDescriptionProvider
的行为之外,它没有任何影响

相反,我会像这样包装所有默认属性描述符:

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
    return new PropertyDescriptorCollection(
        TypeDescriptor.GetProperties(this, attributes, true)
            .Select(x => new ReadOnlyWrapper(x))
            .ToArray());
}
public class ReadOnlyWrapper : PropertyDescriptor
{
   private readonly PropertyDescriptor innerPropertyDescriptor;

   public ReadOnlyWrapper(PropertyDescriptor inner)
   {
       this.innerPropertyDescriptor = inner;
   }

   public override bool IsReadOnly
   {
       get
       {
           return true;
       }
   }

   // override all other abstract members here to pass through to the
   // inner object, I only show it for one method here:

   public override object GetValue(object component)
   {
       return this.innerPropertyDescriptor.GetValue(component);
   }
}                
其中,
ReadOnlyWrapper
是这样一个类:

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
    return new PropertyDescriptorCollection(
        TypeDescriptor.GetProperties(this, attributes, true)
            .Select(x => new ReadOnlyWrapper(x))
            .ToArray());
}
public class ReadOnlyWrapper : PropertyDescriptor
{
   private readonly PropertyDescriptor innerPropertyDescriptor;

   public ReadOnlyWrapper(PropertyDescriptor inner)
   {
       this.innerPropertyDescriptor = inner;
   }

   public override bool IsReadOnly
   {
       get
       {
           return true;
       }
   }

   // override all other abstract members here to pass through to the
   // inner object, I only show it for one method here:

   public override object GetValue(object component)
   {
       return this.innerPropertyDescriptor.GetValue(component);
   }
}