English 中文(简体)
Visual Studio Intellisense not showing methods on generic overload
原标题:

Given the following two interfaces (these are small examples, not my actual implementation):

public interface IAssertion<T> {
     IAssertion<T> IsNotNull();
     IAssertion<T> Evaluate(Predicate<T> predicate)
}

public interface IStringAssertion : IAssertion<string> {
     IStringAssertion IsNotNullOrEmpty();
}

and a static factory that will return the appropriate interface, for example:

public static class Require {
     public static IAssertion<T> That<T>(T value) {
          ...
     }

     public static IStringAssertion That(string value) {
          ...
     }
}

I should be able to do the following:

public void TestMethod(SomeClass a, string b) {
    Require.That(a).IsNotNull();
    Require.That(b).IsNotNullOrEmpty().Evaluate(SomeMethodThatAcceptsString);
}

This code compiles and will actually run. I can even set up tests that pass, such as:

Assert.IsInstanceOf<IStringAssertion>(Require.That(string.Empty));
Assert.IsNotInstanceOf<IStringAssertion>(Require.That(new object());

The problem I am running into and the whole point of this question, is that Visual Studio 2005 intellisense is not resolving the differences between the two.

When I type Require.That("..."). I should expect to see a list of

Evaluate(Predicate predicate)
IsNull()
IsNotNullOrEmpty()

but instead I see nothing.

I would really like to keep the same method name for the overloads. I want to keep the generic overload because of the predicate in the Evaluate method of the IAssertion interface.

Also, I know I can do something close to this using extension methods, but that is not an option because I still want to support .Net 2.0 and would like to keep the fluent api.

Updated:

There have been some good answers that involve third party add-ons to Visual Studio. Unfortunately I am not in a position to either install or purchase add-on tools for Visual Studio due to the corporate red tape that I am developing under. (I hate politics!)

I am looking for a code only option that will work in both Visual Studio 2005 and Visual Studio 2008.

Updated:

This works in Visual Studio 2008. Thank you, Luke. That only leaves Visual Studio 2005.

最佳回答

I believe this is because Intellisense sees the item with the same name as a Generic Type and then doesn t bother to look for the intellisense properties until you give it a type. For example, if you mouse over the "Require.That(string)" Visual Studios will report it as an IAssertion, instead of an IStringAssertion.

Anyway, you could just rename the "That" for your IStringAssertion to "ThatString". I.e.

public static class Require
{

    public static IStringAssertion ThatString(string value)
    {
        return null;
    }

    public static IAssertion<T> That<T>(T value)
    {
        return null;
    }

}

public class RAR
{
    public void TestMethod(StringComparer a, string b)
    {
        Require.That<StringComparer>(a).IsNotNull();
        Require.ThatString(b).IsNotNullOrEmpty();
    }
}

Probably not ideal, but I don t believe there s any way around it on the code front apart from using different method names.

问题回答

You should install Resharper then. It s a whole lot better than VS 2008 intellisense.

Same problem here - Intellisense is not that great.

I started using Visual Assist X, and switched to Visual Assist X.

edit: (saw your answer to the resharper suggestion) I think resharper has a free version. In any case, VAX is really worth it for you to buy it for yourself.

Check if you re referencing a DLL built in RELEASE mode and are adding that Assembly to an Assembly run in DEBUG mode.

Change both to DEBUG mode to confirm.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签