Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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# 我应该扩展系统类型,还是仅仅创建一个静态实用程序类?我怎么知道什么时候做?_C#_.net - Fatal编程技术网

C# 我应该扩展系统类型,还是仅仅创建一个静态实用程序类?我怎么知道什么时候做?

C# 我应该扩展系统类型,还是仅仅创建一个静态实用程序类?我怎么知道什么时候做?,c#,.net,C#,.net,所以我的问题更多的是关于我应该做什么,而不一定是“如何”实现我正在做的事情。我有一个静态类,看起来像这样: public static class CanImportFileType { static Predicate<string> isSTQFile = f => f.ToLower().EndsWith(".stq") || f.ToLower().EndsWith(".stqt"); static Predicate<string> isAPH

所以我的问题更多的是关于我应该做什么,而不一定是“如何”实现我正在做的事情。我有一个静态类,看起来像这样:

public static class CanImportFileType
{
   static Predicate<string> isSTQFile = f => f.ToLower().EndsWith(".stq") || f.ToLower().EndsWith(".stqt");
   static Predicate<string> isAPHFile = f => f.ToLower().EndsWith(".aph");


    public static bool IsValidQuoteFilePath(string[] FilePath)
    {
        bool IsValidQuoteFilePath = false;
        if(HasFile(FilePath))
        {
            if(isSTQFile(FilePath[0]))//we just look at the first index...we could extend this to look through all of the indices and find the first appropriate 
            {
                IsValidQuoteFilePath = true;
            }
        }
        return IsValidQuoteFilePath;
    }
    public static bool IsValidQuoteFilePath(string FilePath)
    {
        bool IsValidQuoteFilePath = false;
            if (isSTQFile(FilePath))
            {
                IsValidQuoteFilePath = true;
            }
        return IsValidQuoteFilePath;
    }


    public static bool IsValidAPHFilePath(string[] FilePath)
    {
        bool IsValidQuoteFilePath = false;
        if (HasFile(FilePath))
        {
            if(isAPHFile(FilePath[0]))
            {
                IsValidQuoteFilePath = true;
            }
        }
        return IsValidQuoteFilePath;

    }
    public static bool IsValidAPHFilePath(string FilePath)
    {
        bool IsValidQuoteFilePath = false;

        if (isAPHFile(FilePath))
        {
            IsValidQuoteFilePath = true;
        }

        return IsValidQuoteFilePath;
    }

    /// <summary>
    /// used only to determine if the string array being passed around is not null or void of any strings (count == 0)
    /// </summary>
    /// <param name="fileLocation"></param>
    /// <returns> whether or not there is a file type in the first index of a string array</returns>
    private static bool HasFile(string[] fileLocation)
    {
        bool hasfile = false;
        if (fileLocation != null)
        {
            if (fileLocation.Count() > 0)
            {
                hasfile = true;
            }
        }
        return hasfile;
    }
}
      public static bool IsValidQuoteFilePath(this string[] FilePath)
    {
        bool IsValidQuoteFilePath = false;
        if(HasFile(FilePath))
        {
            if(isSTQFile(FilePath[0]))//we just look at the first index...we could extend this to look through all of the indices and find the first appropriate 
            {
                IsValidQuoteFilePath = true;
            }
        }
        return IsValidQuoteFilePath;
    }
或者,我可以扩展string[]和string类型,在方法参数的开头添加一个“this”关键字,如下所示:

public static class CanImportFileType
{
   static Predicate<string> isSTQFile = f => f.ToLower().EndsWith(".stq") || f.ToLower().EndsWith(".stqt");
   static Predicate<string> isAPHFile = f => f.ToLower().EndsWith(".aph");


    public static bool IsValidQuoteFilePath(string[] FilePath)
    {
        bool IsValidQuoteFilePath = false;
        if(HasFile(FilePath))
        {
            if(isSTQFile(FilePath[0]))//we just look at the first index...we could extend this to look through all of the indices and find the first appropriate 
            {
                IsValidQuoteFilePath = true;
            }
        }
        return IsValidQuoteFilePath;
    }
    public static bool IsValidQuoteFilePath(string FilePath)
    {
        bool IsValidQuoteFilePath = false;
            if (isSTQFile(FilePath))
            {
                IsValidQuoteFilePath = true;
            }
        return IsValidQuoteFilePath;
    }


    public static bool IsValidAPHFilePath(string[] FilePath)
    {
        bool IsValidQuoteFilePath = false;
        if (HasFile(FilePath))
        {
            if(isAPHFile(FilePath[0]))
            {
                IsValidQuoteFilePath = true;
            }
        }
        return IsValidQuoteFilePath;

    }
    public static bool IsValidAPHFilePath(string FilePath)
    {
        bool IsValidQuoteFilePath = false;

        if (isAPHFile(FilePath))
        {
            IsValidQuoteFilePath = true;
        }

        return IsValidQuoteFilePath;
    }

    /// <summary>
    /// used only to determine if the string array being passed around is not null or void of any strings (count == 0)
    /// </summary>
    /// <param name="fileLocation"></param>
    /// <returns> whether or not there is a file type in the first index of a string array</returns>
    private static bool HasFile(string[] fileLocation)
    {
        bool hasfile = false;
        if (fileLocation != null)
        {
            if (fileLocation.Count() > 0)
            {
                hasfile = true;
            }
        }
        return hasfile;
    }
}
      public static bool IsValidQuoteFilePath(this string[] FilePath)
    {
        bool IsValidQuoteFilePath = false;
        if(HasFile(FilePath))
        {
            if(isSTQFile(FilePath[0]))//we just look at the first index...we could extend this to look through all of the indices and find the first appropriate 
            {
                IsValidQuoteFilePath = true;
            }
        }
        return IsValidQuoteFilePath;
    }
然后,要实现它,我所要做的就是访问strings IsValidQuoteFilePath方法,如下所示:

    string[] foo = {"hello", "world"};
    foo.IsValidQuoteFilePath() //returns false
我想总结一下我的问题是:您如何知道什么时候只扩展一个方法而不是创建一个静态助手类

您如何知道什么时候只扩展一个方法而不是创建一个静态助手类

这两者基本上是一样的——更多的是你期望和想要如何使用这些物品

我的一般经验法则是:这种方法是不是每个开发人员(可能)都希望在每个
string
string[]
实例上使用?如果是的话,考虑一个扩展方法。如果不是,则使用常规静态方法

在这种情况下,用例似乎非常狭窄,不适合于任意的
string
实例,这将建议使用常规的静态辅助方法。核心内置类型(object/string/etc)几乎总是这样,因为很少有方法真正适合该类型的所有用途

另外:请注意,这并没有扩展类型,它只是为访问静态方法提供了不同的语法。

在第二种情况下,您没有扩展类型。您的代码绝不会修改
字符串[]
类型。您只是提供了语法糖,使其看起来好像您的方法是一个实例方法。事实并非如此;这仍然是一种静态方法


两者之间没有真正的功能区别;后者被编译成与前者一样。使用数组完全是个人喜好的问题。

听起来你根本不应该使用数组。这是为了实现拖放功能。您可能正在将多个文件拖放到我们的应用程序中。在这种情况下,我们可以让它查看文件路径数组并找到正确的文件类型,但现在我只想查看第一个文件类型,然后再保留它。等等,那么上面的第二个选项不是为string[]类型创建扩展方法吗?这是我的参考:[link]()@Strohlaj它正在生成一个扩展方法,但从技术上讲,扩展方法并不是在扩展类型。它创造了一些语法糖,使它看起来像是,但它不是。这个命名有点不幸,因为它暗示它确实如此。