如何给分部类添加数据注解?
我有一个自动生成的类,上面有一个属性。我想在同一类型的另一个部分类中向该属性添加一些数据注释。我该怎么做呢?
namespace MyApp.BusinessObjects
{
[DataContract(IsReference = true)]
public partial class SomeClass: IObjectWithChangeTracker, INotifyPropertyChanged
{
[DataMember]
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
private string _name;
}
}
在另一个文件中,我有:
namespace MyApp.BusinessObjects
{
public partial class SomeClass
{
private SomeClass()
{
}
[Required]
public string Name{ get; set; }
}
}
目前,我收到一条错误,指出 name 属性已存在。
I have an auto generated class with a property on it. I want to add some data annotations to that property in another partial class of the same type. How would I do that?
namespace MyApp.BusinessObjects
{
[DataContract(IsReference = true)]
public partial class SomeClass: IObjectWithChangeTracker, INotifyPropertyChanged
{
[DataMember]
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
private string _name;
}
}
and in another file I have:
namespace MyApp.BusinessObjects
{
public partial class SomeClass
{
private SomeClass()
{
}
[Required]
public string Name{ get; set; }
}
}
Currently, I get an error stating that the name property already exists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来我使用
MetadataTypeAttribute
找到了类似于上面链接的不同方法:Looks like I figured out a different way similar to the link above using
MetadataTypeAttribute
:我还使用下面的内容来支持同一个表中引用同一个表的多个外键。例如,这个人有两个父母(父亲和母亲),他们都是 Person 类。
I'm using the below to also support multiple foreign keys in the same table that refer to the same table. Example, the person has two parents (Father and Mother) who are both Person class.