I have defined a class with multiple constructors so that the underlying interfaces are immutable once the object is instantiated. I would like one of the constructors to be the "default" constructor for when a user types the following in Visual Studio:
var obj = new MyClass(
Dim obj As New MyClass(
Currently when I go to instantiate the object, the constructors aren't listed (in Visual Studio Intelli开发者_高级运维Sense) in the order I declared them in my class. Is there a way to mark up my constructors so that their methods appear in a particular order during instantiation in Visual Studio IntelliSense?
There isn't a way to control the ordering within Visual Studio's Intellisense. If you do have multiple constructors (or methods), your only real control in terms of intellisense is to use EditorBrowsable
with the appropriate EditorBrowsableState. This allows you to hide a contructor (or method) in intellisense, or only have it displayed in "advanced" mode, but does not allow you to reorder them.
However, in this situation, if you're targetting .NET 4, I'd recommend considering using a single constructor, and taking advantage of named and optional arguments.
That's an interesting question, but I haven't heard of such capability. One option would be marking the other constructors as advanced or hidden.
ComponentModel.EditorBrowsable( ComponentModel.EditorBrowsableState.Advanced )
ComponentModel.EditorBrowsable( ComponentModel.EditorBrowsableState.Never )
I'm pretty sure what you are asking for is impossible. The best thing to do is to mark up your constructors with XML comments which will be used to populate the intellisense in VS. That will give the user detailed intellisense about which constructors is default etc.
Editorial: I'm pretty sure VB.NET orders the constructors in the order of their present in the class declaration.
You have to remember that Intellisense isn't a feature of the language, but of the editor. You wouldn't have features specific to the IDE built into the language because other editors can be used to write code. Writing code for the purpose of writing code is off the mark.
You could always use a Factory model and make the constructor protected for each method. However I perfer Reed Copsey answer. However if .NET 4.0 is not an option this could be an alternative.
It looks like there still is no way to dictate the order as an application developer. It appears IntelliSense sorts first on the number of arguments, disregarding whether an argument is optional or not (it counts either way).
If you have optional arguments you can rework them to overloads though, thus creating a new method/constructor with less parameters which will then appear earlier. You can also hide constructors that are useless to an application developer by declaring them internal.
Using these methods I could fix my little problem. I had a deserializing constructor that popped up before the more commonly used one.
精彩评论