开发者

Why is this extension method not working?

开发者 https://www.devze.com 2023-02-19 02:56 出处:网络
public static string ToTrimmedString(this DataRow row, string columnName) { return row[columnName].ToString().Trim();
public static string ToTrimmedString(this DataRow row, string columnName)
{
    return row[columnName].ToString().Trim();
}
开发者_运维技巧

Compiles fine but it doesn't show up in intellisense of the DataRow...


My guess is you haven't included the namespace.


Ensure this method is in a static class of its own, separate class from the consuming DataRow.

namespace MyProject.Extensions
{
   public static class DataRowExtensions
   {
      //your extension methods
   }
}

In your consumer, ensure you're:

using MyProject.Extensions


I had this same issue. My mistake wasn't that I missed the static class or static method but that the class my extensions were on was not public.


In addition to a missing using, following case with the same symptom may occur: If you are inside a method of the class itself (or one if its implementors / inheritors), you need to make use of this.

File extension.cs:

namespace a 
{
    public static void AExt(this A a) {}
}

File user.cs

namespace a 
{

    class A {}

    class B : A 
    {
        this.AExt();
        // AExt() will not work without this.
    }
}


If you are using different namespaces try this code.

namespace Extensions
{
    public static class StringExtensions
    {
        public static bool IsNumeric(this string inputString)
        {
            return decimal.TryParse(inputString, out decimal result);
        }
    }
}

namespace Business
{

    // add here other namespaces
    using Extensions;
    public static class Tools
    {
        public static bool Check(string inputString)
        {
            return inputString.IsNumeric();
        }
    }
}


I had this same issue. My mistake is the argument type is not same: defined one is Session, revokded one is ISession.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号