【问题标题】:Custom data annotation with RIa Services使用 RIa 服务的自定义数据注释
【发布时间】:2015-03-16 22:07:46
【问题描述】:

我有一个 Silverlight 应用程序,我正在使用实体框架和 Ria 服务。

我需要创建一个自定义属性,用于识别某些属性。

例如。 :

public class Person
{
    [IsSpecialProperty]
    public string PersonProperty { get; set; }
}

public class IsSpecialPropertyAttribute : Attribute
{

}

我将类文件命名为“IsSpecialPropertyAttribute.shared.cs”,以便从客户端访问它。

从客户端,我使用方法:

var attributes = (IsSpecialPropertyAttribute[])Attribute.GetCustomAttributes(memberInfo, typeof(IsSpecialPropertyAttribute));

此方法适用于系统属性,但不适用于我的自定义属性...

知道为什么吗?

这个方法给了我所有其他的属性,但不是我创建的自定义...

var test = Attribute.GetCustomAttributes(memberInfo);

【问题讨论】:

    标签: c# reflection data-annotations wcf-ria-services


    【解决方案1】:

    您可以尝试其他两种方法:

    var attributes = memberInfo.GetCustomAttributes(typeof(IsSpecialPropertyAttribute), true);
    

    或:

    var attribute = person.GetType().GetCustomAttributes(typeof(IsSpecialPropertyAttribute), true).FirstOrDefault() as IsSpecialPropertyAttribute;
    

    【讨论】:

    • 谢谢,但它不起作用。 “Attribute.GetCustomAttributes(memberInfo);”获取所有其他属性的列表,但不是我创建的...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多