接口中的C#数据注释 [英] C# Data Annotations in Interface

查看:170
本文介绍了接口中的C#数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问...

如果我在界面中添加了符号...

If I put a notation in the Interface...

说[必需]

我可以忽略该属性在C#类中的表示法吗?

can I ommit that notation in the C# class for the property?

ie我可以...

Interface IFoo
{
   [Required]
   string Bar {get; set;}
}

Class Foo : IFoo
{
   string Bar {get; set;}
}

还是我只需要不在界面中放置符号并执行此操作...

or do I need to just not put the notation in the interface and do this...

Interface IFoo
{
   string Bar {get; set;}
}

Class Foo : IFoo
{
   [Required]
   string Bar {get; set;}
}


推荐答案

放置数据界面中的注释不起作用。在下面的链接中有关于原因的解释:
> http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/1748587a-f13c-4dd7-9fec-c8d57014632c/

Placing the Data Annotation in the interface won't work. In the following link there is an explanation as to why: http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/1748587a-f13c-4dd7-9fec-c8d57014632c/

可以通过如下修改代码来找到简单的解释:

A simple explanation can be found by modifying your code as follows:

interface IFoo
{
   [Required]
   string Bar { get; set; }
}

interface IBar
{
   string Bar { get; set; }
}

class Foo : IFoo, IBar
{
   public string Bar { get; set; }
}

然后不清楚是否需要输入Bar字符串,因为实现多个接口是有效的。

Then it is not clear as to whether the Bar string is required or not, since it is valid to implement more than one interface.

这篇关于接口中的C#数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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