Extension Method Weirdness

M

Mike Hofer

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 StringExtensions
{

/// <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 CompressWhiteSpace(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(CarriageReturn,
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 DoSomethingSpectacular(string argument)
{
string result = argument.CompressWhiteSpace();
// Continue processing...
return result;
}

When we compile the library, everything is peachy. But when we attempt
to use the CompressWhiteSpace 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 'CompressWhiteSpace'

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?
 
G

Göran Andersson

Mike said:
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 CompressWhiteSpace 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 'CompressWhiteSpace'

You might be lacking a dependency, so that the library containing the
extension is not compiled before the code that uses it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top