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

发布于 2024-08-24 08:08:31 字数 1193 浏览 6 评论 0原文

我有一个实现 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());
}

这是否是使用 TypeDescriptor.AddAttributes() 的正确方法?在调用后进行调试时,AddAttributes() 属性仍然具有相同数量的属性,其中没有一个是 ReadOnlyAttribute。

I have a class which implements ICustomTypeDescriptor, and is viewed and edited by the user in a PropertyGrid. My class also has a IsReadOnly property which determines if the user will be able to save their changes later. I don't want to allow the user to make changes if they will not be able to save. So if IsReadOnly is true I want to override any properties that would otherwise be editable to be read-only in a property grid.

I'm trying to use the GetProperties method of ICustomTypeDescriptor to add a ReadOnlyAttribute to each PropertyDescriptor. But it doesn't seem to be working. Here's my code.

 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());
}

Is this even the correct way to use TypeDescriptor.AddAttributes()? While debuging after the call the AddAttributes() prop still has the same number of attributes none of which are a ReadOnlyAttribute.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2024-08-31 08:08:31

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

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

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

其中 ReadOnlyWrapper 是这样的类:

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);
   }
}                

TypeDescriptor.AddAttributes adds class-level attributes to a given object or object type, not property-level attributes. On top of that, I don't think it has any effect other than in the behavior of the returned TypeDescriptionProvider.

Instead, I would wrap all the default property descriptors like this:

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

where ReadOnlyWrapper is a class like this:

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);
   }
}                
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文