Extension Method Weirdness

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Hofer

    Extension Method Weirdness

    We have an extension method defined that is causing all sorts of
    compiler grumpiness in one of our projects, and we don't know why.

    The method in question isn't rocket science, so here it is, in all
    it's gory details:

    using System;

    namespace Company.Project
    {
    /// <summary>
    /// Provides extension mthods for the String class.
    /// </summary>
    /// <remarks>
    /// This class requires .NET 3.5.
    /// </remarks>
    public static class StringExtension s
    {

    /// <summary>
    /// Replaces whitespace with spaces, and replaces double spaces
    /// with single spaces.
    /// </summary>
    /// <param name="value">
    /// The string to be compressed.
    /// </param>
    /// <returns>
    /// A string.
    /// </returns>
    /// <remarks>
    /// This is an extension method to the <see cref="String"/class.
    /// As such, it requires .NET 3.5.
    /// </remarks>
    public static string CompressWhiteSp ace(this string value)
    {
    const string CarriageReturn = "\r";
    const string LineFeed = "\n";
    const string Tab = "\t";
    const string Space = " ";
    const string DoubleSpace = " ";

    string result = value.Replace(C arriageReturn,
    Space).Replace( LineFeed, Space).Replace( Tab, Space);
    while (result.IndexOf (DoubleSpace) != -1)
    result = result.Replace( DoubleSpace, Space);
    return result;

    }
    }
    }

    This class is part of a larger class library. Dependent libraries use
    it like so:

    protected string DoSomethingSpec tacular(string argument)
    {
    string result = argument.Compre ssWhiteSpace();
    // Continue processing...
    return result;
    }

    When we compile the library, everything is peachy. But when we attempt
    to use the CompressWhiteSp ace method, we get the following spiffy
    error message from the compiler, and the dependent client fails to
    build:

    'string' does not contain a definition for 'CompressWhiteS pace'

    This is Compiler Error CS0117. According to online help, it means
    "This error occurs in some situations when a reference is made to a
    member that does not exist for the data type." This is not to be
    confused with the error that occurs when *a suitable extension method
    cannot be found* (and no, I don't have that compiler error number on
    hand, but trust me, it's different).

    So far, I've determined the following:

    - The DLL is appropriately referenced.
    - The namespace is properly imported.
    - All assemblies are targeting .NET 3.5 when they are built.
    - Nothing in Web.Config looks awry. (That is, we're targeting the
    appropriate 3.5 and 2.0 assemblies.)
    - The class and the invoking clients are syntactically correct. To
    prove this, I took the class itself and copied it directly into a
    clean solution consisting of a server dll, a client dll, and a new
    blank web site. I invoked the extension method from both the DLL and
    the Web site. Both worked fine and built fine. So the class is
    syntactically fine, and there's nothing wrong with the way I'm
    invoking it.
    - Cleaning and rebuilding the solution has no efffect on the outcome.

    So. Any ideas?
  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Extension Method Weirdness

    Mike Hofer wrote:
    We have an extension method defined that is causing all sorts of
    compiler grumpiness in one of our projects, and we don't know why.
    8< snip
    When we compile the library, everything is peachy. But when we attempt
    to use the CompressWhiteSp ace method, we get the following spiffy
    error message from the compiler, and the dependent client fails to
    build:
    >
    'string' does not contain a definition for 'CompressWhiteS pace'
    >
    You might be lacking a dependency, so that the library containing the
    extension is not compiled before the code that uses it.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    Working...