C #, WinForms and Extension Methods
Question
Apart from all the obvious answers, what will call the extension methods to generate compiler errors such as:
'DataType' does not contain a definition for 'YourExtensionMethodName'
I have a real stone here, and this is detailed below. I have exhausted every possible reason I can think of.
Scenario
- I have several extension methods defined in various static classes in a DLL that are used by a WinForms application.
- Extension signatures do not conflict with class I method signatures that are extended (
String
in this case). - Both DLL and WinForms applications are written in C #.
- Both DLL and WinForms applications are configured to install on .NET 3.5.
- The consumption classes include a reference to the namespace that defines the extension method. Its spelling has been checked.
- If I reference the extension class directly, the extension methods appear. For example, if I type
StringExtensions.
, Intellisense looks like normal and all my extension methods are listed. - EDIT . Errors occur in a WinForms application, but only for some extension methods, not all.
Code (or excerpt)
(Yes, this is the violation code)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Roswell.Framework
{
public static class StringBuilderExtensions
{
public static string ToSentenceCase(this string value)
{
return value.Substring(0, 1).ToUpper() + value.Substring(1).ToLower();
}
public static string ToTitleCase(this string value)
{
string[] parts = value.Split(new string[] {" "}, StringSplitOptions.None);
System.Text.StringBuilder builder = new System.Text.StringBuilder();
foreach (string part in parts)
{
builder.Append(part.ToSentenceCase());
builder.Append(" ");
}
return builder.ToString();
}
}
}
And this is the code that consumes it:
using Roswell.Framework;
namespace Roswell.Windows.Command
{
/// <summary>
/// Views the SQL for an object in the database window.
/// </summary>
internal class ViewObjectDdlCommand
: MainWindowCommand
{
public override void Execute()
{
// ...
OpenCodeWindow(
string.Format("{0} - {1} - {2}",
dsn.Name,
objectName,
info.ToTitleCase()),
schemaItemType,
objectName);
}
}
}
source to share
From your code snippet, I can see that you are calling ToTitleCase
on what is called info
. But I don't see the type of this variable, and that's what will determine what happens here.
Obviously, it must be a string (if the string was not a private class, it could be something derived from a string, but this is not possible for a private class).
So the only thing that makes sense (other than a very unlikely compiler error) is that is info
not a string.
source to share
The error suggests the answer:
'DataType' does not contain a definition for 'YourExtensionMethodName'
In this case, I am assuming that "info" ( ViewObjectDdlCommand.info
) is not a string, but rather a DataType. Try changing it to:
OpenCodeWindow(
string.Format("{0} - {1} - {2}",
dsn.Name,
objectName,
info.ToString().ToTitleCase()),
schemaItemType,
objectName);
source to share