为什么找不到我的扩展方法?

时间:2012-10-07 16:15:46

标签: c# methods

我想添加一个字符串方法,将空格char转换为下划线(扩展方法),我部署了代码,但为什么它不起作用?

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "moslem";
            string.SpaceToUnderScore(name);
        }

        public static string SpaceToUnderScore(this string source)
        {
            string result = null;
            char[] cArray = source.ToArray();
            foreach (char c in cArray)
            {
                if (char.IsWhiteSpace(c))
                {
                    result += "_";
                }
                else
                {
                    result += c;
                }
            }
            return result;
        }
    }
}

为什么不起作用?

4 个答案:

答案 0 :(得分:3)

首先将扩展方法放到静态类中,然后调用name.SpaceToUnderScore()

var newstr = "a string".SpaceToUnderScore();


public static class SomeExtensions
{
    public static string SpaceToUnderScore(this string source)
    {
        return new string(source.Select(c => char.IsWhiteSpace(c) ? '_' : c).ToArray());
        //or
        //return String.Join("",source.Select(c => char.IsWhiteSpace(c) ? '_' : c));
    }
}

答案 1 :(得分:0)

扩展方法应该用静态类编写。

实施例

public static class StringExtension
{
    public static string SpaceToUnderScore(this string source)
    {
        string result = null;
        char[] cArray = source.ToArray();
        foreach (char c in cArray)
        {
            if (char.IsWhiteSpace(c))
            {
                result += "_";
            }
            else
            {
                result += c;
            }
        }
        return result;
    }
}

答案 2 :(得分:0)

在不更改扩展方法中的代码的情况下,将扩展方法添加到静态类:

public static class MyExtensions // Name this class to whatever you want, but make sure it's static.
{
    public static string SpaceToUnderScore(this string source)
    {
        string result = null;
        char[] cArray = source.ToArray();
        foreach (char c in cArray)
        {
            if (char.IsWhiteSpace(c))
            {
                result += "_";
            }
            else
            {
                result += c;
            }
        }
        return result;
    }
}

然后这样称呼它:

string name = "moslem";
string underscoreName = name.SpaceToUnderScore(); // Omit the name parameter (prefixed with this) when called like this on the string instance. 

// This would work to:
string underscoreName = MyExtentions.SpaceToUnderScore(name);

如果找不到扩展方法,请确保使用静态类的命名空间。

答案 3 :(得分:0)

public static class MyStringExtenstionClass
{

    public static string SpaceToUnderScore(this string source)
    {
        string result = null;
        char[] cArray = source.ToArray();
        foreach (char c in cArray)
        {
            if (char.IsWhiteSpace(c))
            {
                result += "_";
            }
            else
            {
                result += c;
            }
        }
        return result;
    }
}

并且喜欢这个

 string name = "John Doe";
 name = name.SpaceToUnderScore(); //no argument and called on instance of string