Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在选项页面中使用ValidationAttribute进行用户输入?_C#_Attributes_Data Annotations_Validation_Options - Fatal编程技术网

C# 如何在选项页面中使用ValidationAttribute进行用户输入?

C# 如何在选项页面中使用ValidationAttribute进行用户输入?,c#,attributes,data-annotations,validation,options,C#,Attributes,Data Annotations,Validation,Options,我正在编写一个Visual Studio扩展。我有一个代表选项页面的C#类。选项由公共属性表示,如下所示: public class OptionsPageGeneral : DialogPage { [Range(1, 100)] [Category("Misc")] [DisplayName("Integer option")] public string IntOption { get; set; } ... } 我正在尝试使用限制用户输入。然而,对于给定的代码,用

我正在编写一个Visual Studio扩展。我有一个代表选项页面的C#类。选项由公共属性表示,如下所示:

public class OptionsPageGeneral : DialogPage
{
  [Range(1, 100)]
  [Category("Misc")]
  [DisplayName("Integer option")]
  public string IntOption { get; set; }
  ...
}

我正在尝试使用限制用户输入。然而,对于给定的代码,用户仍然可以输入任何值,而不仅仅是[1;100]范围内的值

我见过很多ValidationAttribute使用的例子,但都是针对ASP.NET MVC项目的。该属性是否仅适用于该上下文


无论如何,如何验证在选项页中完成的用户输入?我知道我可以简单地重写property
set
方法,但是验证属性需要编写的代码要少得多,并且可以用于类似的属性。

我不知道如何使用选项页,但是如果您希望范围在1-100之间,而不是
范围
,则可以使用正则表达式

  [Category("Misc")]
  [DisplayName("Integer option")]
  [RegularExpression(@"[0-9]{2}")]//This will only allow you to enter only 2 digits
  public string IntOption { get; set; }
文档中的“指定ASP.NET动态数据中的数据字段值必须与指定的正则表达式匹配”。这同样只适用于ASP.NET。