22

I have a fluent interface for an IoC container registration process, and this contains some classes that are used to build up the registrations.

For instance, I can do this:

builder.Register<IFoo>().From.ConcreteType<Foo>();

However, at each step after a dot, intellisense pops up showing the four standard methods from the Object class, GetHashCode, Equals, GetType, and ToString.

I tried overriding these methods in a couple of the classes, attaching the EditorBrowsableAttribute attribute to each, but they still show up. Is there no way for me to hide them?

Note that I'm not trying to hide them from the class itself, just from intellisense.

Basically, I'd like this:

                         +---------------+
builder.Register<IFoo>().|As             |
                         |By             |
                         |Equals         | <-- remove this
                         |From           |
                         |GetHashCode    | <-- and this
                         |GetType        | <-- as well as this
                         |ToString       | <-- and finally this
                         +---------------+

Here's what I tried in the class that is returned from Register<T>:

[EditorBrowsable(EditorBrowsableState.Never)]
public override Boolean Equals(Object obj)
{
    return base.Equals(obj);
}

[EditorBrowsable(EditorBrowsableState.Never)]
public override Int32 GetHashCode()
{
    return base.GetHashCode();
}

[EditorBrowsable(EditorBrowsableState.Never)]
public override String ToString()
{
    return base.ToString();
}

This clearly did not work. Is there anything else I can try?

I also tried adding a new method, not overridden from Object, and applying the same attribute to that, and that too shows up, so clearly the attribute is not doing what I thought it would do.

I noticed the note in the documentation about not hiding things from the same assembly, but I tried creating a new project and they still show up there.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • Interesting, the documentation of the EditorBrowsableState clearly says *For example, the IntelliSense engine in Visual Studio never shows methods or properties that are marked as Never.* I tried in a small test project and I get the same results as you. Even if I check the "Hide advanced members" and use `EditorBrowsableState.Advanced` the member still show up in the list. Odd. – Fredrik Mörk Sep 23 '09 at 08:42
  • I found the answer, I'll edit the question. – Lasse V. Karlsen Sep 23 '09 at 08:51
  • Good finding. Never is not always never. – Fredrik Mörk Sep 23 '09 at 11:13
  • But now, what is the Question? – H H Sep 23 '09 at 11:48
  • The question *was* how to do it, and/or why what I tried didn't work. Now that I found the answer, *I* personally don't need to ask that question any more. That's not to say nobody else won't need it later on. – Lasse V. Karlsen Sep 23 '09 at 12:48
  • I think you know the Faq about answering your own question. Why not follow the rules? I really had to search (and PageDown) to find the question part. – H H Sep 23 '09 at 17:20
  • Ok, noted, have edited out the solution and put it into a separate answer. And you should get a bigger monitor ;) – Lasse V. Karlsen Sep 24 '09 at 19:58
  • There is no way to do this if the returned type is an interface right? – Kristoffer L Jun 17 '10 at 12:24
  • No, I don't think there is, or at least I suspect there is no way that works. – Lasse V. Karlsen Jun 17 '10 at 21:21
  • this CAN be done with interfaces :) create an interface that contains the methods to override (Type GetType(); etc) and add the attributes to the methods ([EditorBrowsable(EditorBrowsableState.Never)]) then get your fluent interfaces to inherit from this Interface. – AaronHS Nov 02 '12 at 12:44
  • look at Ninject's Ninject.Syntax.FluentSyntax for an example (source in in github) – AaronHS Nov 02 '12 at 12:45

2 Answers2

22

Ok, I found the solution, it was partly my fault, and partly incomplete documentation.

My fault was that I had not tagged all the methods in the correct class, but even when correcting that, the methods still showed up.

Turns out the note in the documentation, which reads:

In Visual C#, EditorBrowsableAttribute does not suppress members from a class in the same assembly.

Should actually be (emphasis mine):

In Visual C#, EditorBrowsableAttribute does not suppress members from a class in the same open solution.

I tagged the methods in the appropriate class, did a full rebuild, created a new project outside of the solution file, made file references to the compiled files from my IoC project, and lo and behold, the methods disappeared.

I'll leave the question up in case someone else has this problem.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • This is so annoying! I usually want to hide variables that require `lock`ing and expose a property that does the locking. If I can't do this in the assembly I'm developing then what's the significance? If I don't want to use a variable/property/method outside an assembly, I'd set its accessor to `private` or `internal`! VB.NET is at least more sensible on this issue. `EditorBrowsableState.Never` **always** hides whatever it is applied to. – Alex Essilfie Feb 16 '12 at 18:37
  • @mynkow Just introduce the method `public new Type GetType()` and let it `return base.GetType();` – tm1 Jul 01 '15 at 09:22
18

According to thread, it is by design. The methods/properties decorated with EditorBrowsable(EditorBrowsableState.Never) are only hidden from intellisense if they're part of a class that is in another referenced assembly. The assembly should not be part of the same solution.

Helgi
  • 5,428
  • 1
  • 31
  • 48
Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154