Extension Methods in .NET does not work

0

I am trying to create a Extension Methods for class string . It is not appearing.

My class with the extension method:

namespace MEUPROJETO.Extension
{
    public static class StringExtension
    {
        public static string PrimeiraLetraDeCadaPalavraMiuscula(this string frase)
        {
            frase = frase.ToLower();

            System.Globalization.CultureInfo cultureinfo = System.Threading.Thread.CurrentThread.CurrentCulture;

            return cultureinfo.TextInfo.ToTitleCase(frase);

        }
    }
}

Class where I try to use it:

namespace MEUPROJETO.Teste
{
   public static class TesteExt
   {
       public static void teste( )
       {
         string catdescription = "TESTANDO a frase nessa STRING.";

         catdescription = catdescription.PrimeiraLetraDeCadaPalavraMiuscula();
       }
   }
}

He can not even compile, says the method does not exist, how do I make it appear?

Does the namespace influence?

    
asked by anonymous 31.05.2015 / 00:54

2 answers

1

I am putting the answer that the ramaral put in the comments, when he creates his answer here I mark his as the correct answer.

The solution is to add the namespace using MEUPROJETO.Extension where I want to use the methods.

    
31.05.2015 / 22:37
-2

You did not mark the PrimeiraLetraDeCadaPalavraMiuscula method as an extension, add the following attribute in the variable declaration:

    [Runtime.CompilerServices.Extension()]
    public static String PrimeiraLetraDeCadaPalavraMiuscula(this string frase) //não precisa do this
    {
        frase = frase.ToLower();
        Globalization.CultureInfo cultureinfo = Threading.Thread.CurrentThread.CurrentCulture;

        return cultureinfo.TextInfo.ToTitleCase(frase).ToString;

    }
    
31.05.2015 / 01:51