문제

I've written a simple extension method for a web project, it sits in a class file called StringExtensions.cs which contains the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Useful extensions for string
/// </summary>
static class StringExtensions
{
    /// <summary>
    /// Is string empty
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IsEmpty(this string value)
    {
        return value.Trim().Length == 0;
    }
}

I can access this extension method from all the classes I have within the App_Code directory. However I have a web page called JSON.aspx that contains a series of [WebMethods] - within these I cannot see the extension method - I must be missing something very obvious!

도움이 되었습니까?

해결책 2

StringExtensions.cs file needed to have the class declared as public

Previously:

static class StringExtensions{ ... } 

Fixed:

public static class StringExtensions{ ... } 

다른 팁

In order to see the extension method you must have an using directive for the namespace in which the class containing the extension method is declared.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top