Question

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!

Was it helpful?

Solution 2

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

Previously:

static class StringExtensions{ ... } 

Fixed:

public static class StringExtensions{ ... } 

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top