c# - 服务器端属性在 Silverlight 客户端中不可用

标签 c# silverlight attributes

我有一个自定义属性:

[System.AttributeUsage(System.AttributeTargets.Property)]
public class MyCustomAttribute : System.Attribute
{
}

它是在名为 MyCustomAttribute.shared.cs 的文件中声明的,它使类在客户端上可见,因为它出现在客户端的自动完成/InteliSense 中。

我已将它添加到我的类的属性中,该属性是从数据库返回并通过 WCF RIAServices 传递给 Silverlight 客户端的 Entity:

public partial class MyClass
{
    [Required(AllowEmptyStrings=true)]
    [Display(ResourceType=typeof(ResourceFile), Name="ResourceName")]
    [MyCustom]
    public string MyProperty { get; set; }
}

但是,当我尝试查看该属性是否附加了属性时,它不在列表中:

PropertyInfo prop = GetProperty(myType, "MyProperty");
object[] attributes = prop.GetCustomAttributes(false);
foreach (object attribute in attributes)
{
    if (attribute is MyCustomAttribute)
    {
        // Do my stuff here
    }
}

返回的属性都不是 MyCustomAttribute

我已经在客户端检查了生成的代码,对于这个属性,它看起来像这样:

    /// <summary>
    /// Gets or sets the 'BarcodeNumber' value.
    /// </summary>
    [ConcurrencyCheck()]
    [DataMember()]
    [Display(Name="ResourceName", ResourceType=typeof(ResourceFile))]
    [Required(AllowEmptyStrings=true)]
    [RoundtripOriginal()]
    public string MyProperty
    {
        ....
    }

所以,显然我的属性没有被复制到客户端。

我错过了什么?

最佳答案

我缺少的是我需要编写一个自定义代码生成器来扩展默认代码生成器,以便它可以识别我的新属性。

该过程记录在网络上的各个地方:

.NET - WCF RIA Services code generatie naar je hand zetten (文章为英文)

T4 Code Generator for WCF RIA Services

但是,我们认为仅针对一个属性的一个属性实现这种做法过于矫枉过正,因此使用了不同的方法(基于业务逻辑)来实现我们需要的功能。

关于c# - 服务器端属性在 Silverlight 客户端中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38874112/

相关文章:

c# - 计算文本中没有空行的行数

xml - 在 xml 声明和文档类型之间没有 CR 的 wp7 读取 XML 时出现 InvalidOperationException

c# - 多线程后台 worker 设计

c# - silverlight/wpf 中的交点

javascript - 如何使用 jQuery 更改链接的 "href"属性具有特定的 "href"?

ruby - 我如何使用 savon 嵌套数组属性!哈希?

jsf - 使用属性为转换器创建自定义标签

c# - 如何计算wpf中形状的方向

c# - 报告异步任务的进度

c# - 如何将列表中的项目与子字符串进行比较?