验证方法参数集 [英] Validate set for method argument

查看:51
本文介绍了验证方法参数集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#的第一步,我很挣扎,我创建了一个类:

my first steps with C# and I'm struggling quite a bit, I have created a class:

        public class LogFiles
        {
            public string[] LogPaths;
            public LogFiles(string path)
            {
                LogPaths = Directory.GetFiles(path);
            }
            public LogFiles(string path, string stringtocheck="cut", string value="exclude")
            {
                switch (value.ToLower())
                {
                    case "include":
                    default:
                        LogPaths = Directory.GetFiles(path).Where(x => x.IndexOf(stringtocheck, StringComparison.CurrentCultureIgnoreCase) >= 0).ToArray();
                        return;
                    case "exclude":
                        LogPaths = Directory.GetFiles(path).Where(x => x.IndexOf(stringtocheck, StringComparison.CurrentCultureIgnoreCase) == -1).ToArray();
                        return;
                }
            }
        }

我想了解的是,对于覆盖的构造函数,将针对"include"集合对第三个参数进行验证.术语排除"和排除",从而仅接受这两个值,并且智能感知将说明这一点.

What I want to acvhieve is that for overriden constructor the third argument would be validated against the set of "include" and "exclude", so that only those two values are accepted and intellisense would tell about that.

对于powershell函数参数,我会使用[ValidateSet("include","exclude")],但是我无法找到如何在C#中执行此操作的方法,实际上我已经找到了一些示例网络,但是他们对我来说却很复杂,而我却没有 能够以非常有限的C#知识使他们适应我的班级.

For powershell function parameters I would have used [ValidateSet("include","exclude")], however I'm not able to find how to do that in C#, in fact I've found some examples on the web, however they look to complicated for me and I'm not able to adapt them to my class with my very limited knowledge of C#.

推荐答案

不确定我是否遵循您的问题?但是,似乎您想将第三个参数限制为一组值.如果是这样,那么我倾向于使用一个枚举或一个结构,该结构允许您配置所需的选项并在编译时检测更多问题 时间.

Not really sure I follow the issue you're having but it seems like you want to limit the 3rd parameter to a set of values. If so then I'd lean toward an enum or a struct that allows you to configure the options you want and detect more issues at compile time.

//Use a struct if you want to set more options then just this
public enum SearchOptions
{
   Include = 1,
   Exclude = 2
}

public class LogFiles
{
   public LogFiles ( string path, string stringtocheck = "cut", SearchOptions options = SearchOptions.Include )
   { ... }
}

//Usage
var log = new LogFiles("", "", SearchOptions.Include);

或者,对于这种特定情况,您实际上只使用布尔指示器,那么布尔参数也可以.

Alternatively, for this specific scenario where you're really just using a boolean indicator then a boolean parameter would also be fine.

如果您要使用字符串但具有更大的可用列表,则您拥有的开关代码很好,但可以检测到错误"的密码.默认值的语句应引发ArgumentOutOfRangeException.

If you want to use strings but have a larger available list then the switch code you have is fine but to detect the "bad" values you're default statement should throw an ArgumentOutOfRangeException.

switch (value.ToLower())
{
   case "include": ...
   case "exclude": ...
   default: throw new ArgumentOutOfRangeException("Value is invalid.", "value");
}

最后,我考虑完全不将其作为参数,但允许将其设置为属性.这也意味着您应该将枚举推迟到需要列表(这是奖励功能)之前.

Finally, I'd consider not having it as a parameter at all but allow it to be set as a property. This would also mean you should defer enumeration until the point you need the list which is a bonus feature.

//Use a property to expose the features
public class LogFiles
{
   public bool Exclude { get; set; }

   public IEnumerable<string> GetFiles ( string pattern )
   {
      return Directory.EnumerateFiles(...);
   }
}

//Alternative: No reason to store anything beyond the request
public class LogFiles
{
   public IEnumerable<string> GetFiles ( string pattern, bool include )
   {
      return Directory.EnumerateFiles(...);
   }
}

迈克尔·泰勒
http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于验证方法参数集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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