TypeDescriptor.AddAttributes()取代其添加的当前属性INSEAD [英] TypeDescriptor.AddAttributes() replaces the current attribute insead of adding it

查看:315
本文介绍了TypeDescriptor.AddAttributes()取代其添加的当前属性INSEAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code该部分:

var hasAttribute = TypeDescriptor.GetAttributes(property.DeclaringType).OfType<CrmTypeAttribute>()
            .Any((attr)
                    => ((CrmTypeAttribute)attr).Name == property.Name);

            if (!hasAttribute)
            {
                var crmTypeAttribute = new CrmTypeAttribute(property.Name, crmType);
                TypeDescriptor.AddAttributes(property.DeclaringType, crmTypeAttribute);
            }

它有两个问题:

It has two problems:

  1. 出于某种原因OfType尽管它应该返回该类型的权利属性,我检查返回一个空IEnumerable的。它们的存在。
  2. 这是严重的问题。相反,添加属性的它取代了同类型crmTypeAttribute的旧的属性。 我已标记的AllowMultiple是真实的。

谁能告诉我有什么不对的code?
编辑:
出于某种原因,它允许添加的属性类型的一个属性,我已经在运行时添加另一个属性的类型和它的工作。

Can anyone tell me what's wrong with this code?

For some reason it allows adding only one attribute of an attribute type, I have added another attribute type at runtime and it worked.

推荐答案

原来,该属性需要重写不被处理的属性类的typeid的财产重复。
请参见此处的细节,它是非常隐蔽的,应当提到在GetAttributes作为井。

Turns out that the attribute needs to override the TypeId property of the Attribute class to not be treated as duplicated.
See here for details, it's very hidden and should be mentioned in GetAttributes as well.

这篇关于TypeDescriptor.AddAttributes()取代其添加的当前属性INSEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆