17

ここで何が間違っているのかわかりません。拡張方式は認識されません。

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;
        }
    }
}
4

4 に答える 4

37

タイプ文字列でそれを呼び出そうとしています。文字列インスタンスで呼び出す必要があります。例:

"{0}".SafeFormat("Hello");

確かに、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;
    }

次に、電話をかけることができます:

"{0} {1}".SafeFormat("Hi", "there");

拡張メソッドのポイントは、拡張型のインスタンスメソッドのように見えることです。拡張型で静的メソッドのように見える拡張メソッドを作成することはできません。

于 2009-11-04T19:57:37.790 に答える
10

インスタンス拡張メソッドを定義し、それを静的メソッドとして使用しようとしています。(C#は静的拡張メソッドを定義できませんが、F#はその問題です。)

それ以外の:

result = string.SafeFormat("Hello");

あなたは次のようなものが欲しいです:

result = "Hello".SafeFormat();

つまり、文字列インスタンス(この場合は「Hello」)を操作しています。

于 2009-11-04T19:57:57.917 に答える
4

拡張メソッドは、型自体ではなく、型のインスタンスに表示されます(静的メンバーなど)。

于 2009-11-04T19:58:30.497 に答える
2

試す

"Hello".SafeFormat("{0} {1}", "two", "words")
于 2009-11-04T19:57:05.390 に答える