我有一个自定义属性:
[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/