将扩展方法添加到字符串类 - C#

Chr*_*nce 14 .net c# string extension-methods string-formatting

不知道我在这里做错了什么.无法识别扩展方法.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using StringExtensions;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            RunTests();
        }

        static void RunTests()
        {
            try
            {
                ///SafeFormat
                SafeFormat("Hi There");

                SafeFormat("test {0}", "value");

                SafeFormat("test missing second value {0} - {1}", "test1");

                SafeFormat("{0}");

                //regular format
                RegularFormat("Hi There");

                RegularFormat("test {0}", "value");

                RegularFormat("test missing second value {0} - {1}", "test1");

                RegularFormat("{0}");

                ///Fails to recognize the extension method here
                string.SafeFormat("Hello");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }

        private static void RegularFormat(string fmt, params object[] args)
        {
            Console.WriteLine(String.Format(fmt, args));
        }

        private static void SafeFormat(string fmt, params object[] args)
        {
            string errorString = fmt;

            try
            {
                errorString = String.Format(fmt, args);
            }
            catch (System.FormatException) { } //logging string arguments were not correct
            Console.WriteLine(errorString);
        }

    }

}

namespace StringExtensions
{
    public static class StringExtensionsClass
    {
        public static string SafeFormat(this string s, string fmt, params object[] args)
        {
            string formattedString = fmt;

            try
            {
                formattedString = String.Format(fmt, args);
            }
            catch (System.FormatException) { } //logging string arguments were not correct
            return formattedString;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 35

你试图在类型字符串上调用它.您需要在字符串实例上调用它,例如

"{0}".SafeFormat("Hello");
Run Code Online (Sandbox Code Playgroud)

不可否认,这不会做你想要的,因为SafeFormat方法实际上完全忽略了第一个参数(s).它应该如下所示:

    public static string SafeFormat(this string fmt, params object[] args)
    {
        string formattedString = fmt;

        try
        {
            formattedString = String.Format(fmt, args);
        }
        catch (FormatException) {} //logging string arguments were not correct
        return formattedString;
    }
Run Code Online (Sandbox Code Playgroud)

然后你可以打电话:

"{0} {1}".SafeFormat("Hi", "there");
Run Code Online (Sandbox Code Playgroud)

扩展方法的意思是它们看起来像扩展类型上的实例方法.您无法在扩展类型上创建看似静态方法的扩展方法.


Nol*_*rin 10

您正在定义实例扩展方法,然后尝试将其用作静态方法.(C#不能定义静态扩展方法,尽管F#就此而言.)

代替:

result = string.SafeFormat("Hello");
Run Code Online (Sandbox Code Playgroud)

你想要的东西:

result = "Hello".SafeFormat();
Run Code Online (Sandbox Code Playgroud)

即您正在对字符串实例进行操作(在本例中为"Hello").