添加一个扩展方法的字符串类 - C#字符串、方法

2023-09-04 02:07:53 作者:钻进千玺怀里

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

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Text.RegularEx pressions;
使用StringExtensions;


命名空间ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            RunTests();
        }

        静态无效RunTests()
        {
            尝试
            {
                /// SafeFormat
                SafeFormat(你好);

                SafeFormat(测试{0},价值);

                SafeFormat(测试缺少第二个值{0}  -  {1},测试1);

                SafeFormat({0});

                //常规格式
                RegularFormat(你好);

                RegularFormat(测试{0},价值);

                RegularFormat(测试缺少第二个值{0}  -  {1},测试1);

                RegularFormat({0});

                ///无法在这里认识的扩展方法
                string.SafeFormat(你好);

            }
            赶上(例外前)
            {
                Console.WriteLine(ex.ToString());
            }
            到Console.ReadLine();
        }

        私有静态无效RegularFormat(字符串格式化,params对象[]参数)
        {
            Console.WriteLine(的String.Format(FMT,参数));
        }

        私有静态无效SafeFormat(字符串格式化,params对象[]参数)
        {
            字符串为errorString = FMT;

            尝试
            {
                为errorString =的String.Format(FMT,参数);
            }
            赶上(System.FormatException){} //记录字符串参数不正确
            Console.WriteLine(为errorString);
        }

    }

}

命名空间StringExtensions
{
    公共静态类StringExtensionsClass
    {
        公共静态字符串SafeFormat(这个字符串s,字符串格式化,params对象[]参数)
        {
            字符串formattedString = FMT;

            尝试
            {
                formattedString =的String.Format(FMT,参数);
            }
            赶上(System.FormatException){} //记录字符串参数不正确
            返回formattedString;
        }
    }
}
 

解决方案

您正试图调用它的键入的字符串。你需要把它在一个字符串的实例的,例如:

 {0}SafeFormat(你好)。
 
microsoft visual studio2008与Access数据库的连接问题

诚然,不会做你想要它,因为SafeFormat方法其实是完全无视一个参数(取值)反正。它应该是这样的:

 公共静态字符串SafeFormat(这个字符串格式化,params对象[]参数)
    {
        字符串formattedString = FMT;

        尝试
        {
            formattedString =的String.Format(FMT,参数);
        }
        赶上(出现FormatException){} //记录字符串参数不正确
        返回formattedString;
    }
 

然后就可以调用:

 {0} {1}SafeFormat(你好,有)。
 

扩展方法的一点是,他们喜欢看的实例的在扩展类型的方法。您不能创建这似乎是扩展方法的静态的方法上的扩展类型。

Not sure what I'm doing wrong here. The extension method is not recognized.

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

解决方案

You're trying to call it on the type string. You need to call it on a string instance, e.g.

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

Admittedly that won't do what you want it to, because the SafeFormat method is actually completely ignoring the first parameter (s) anyway. It should look like this:

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

Then you can call:

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

The point of extension methods is that they look like instance methods on the extended type. You can't create extension methods which appear to be static methods on the extended type.