C# ArgumentOutOfRangeException parameter intellisense
-
08-06-2021 - |
Question
When I'm writting:
throw new ArgumentOutOfRangeException("")
Placing the caret between the quotes, and pressing Ctrl+Space to open up intellisense actually does something!
The strange and beautiful thing that dazzles me is that it actually suggests parameter names from the method:
Can someone please explain to me how it can be achieved?
How can I add custom intellisense in such level?
UPDATE: As some pointed out - this intellisense doesn't popup normally.
I am using ReSharper 6.1, and I can control whether that intellisense will popup or not in ReSharper > Options > Intellisense > Autopopup > [C#] In string literals.
Still, I would like to know how I can create such intellisense myself.
Solution
I think you should take a look at this article. Plus there is another Stack Overflow question quite similar to yours that may give you some hints too.
How to implement concretelly I don't know, but I don't think you even need to use reflection as even "normal" IntelliSense of Visual Studio works without any need to build your project first. Just adding a new class to your project for example makes it available for IntelliSense. I think ReSharper uses the same kind of mechanism behind.
OTHER TIPS
Resharper adds a number of helpful features that go far beyond what Visual Studio gives you natively through Intellisense. For example, in ASP.NET MVC, Resharper will suggest controller action names automatically:
// Resharper will give suggestions based on controller action names
@Html.Action("Show
In your question, Resharper has marked the argument to the ArgumentOutOfRangeException
constructor as needing to be the name of an argument to the current method. So when you go to enter the string, it suggests names of the current method's parameters.
To write your own intellisense like this takes a lot of effort. You'd basically be duplicating what the folks at Jetbrains have spent a lot of resources on to make Resharper what it is.
If you want to plug into Resharper's API to create your own plugin, it takes less effort, but it can still be pretty tedious.
However, if you just want to write your own method that requires a string parameter to be the name of one of the calling method's arguments, Jetbrains allows you to annotate your method arguments using their External Annotations. In this case you'd use the InvokerParameterNameAttribute
.
Seems like it is related with Resharper intellisense options
How to do it
1. Using Resharper plugin
2. Using CSharpIntellisense library
3. Excellent blog post and another
I can't see anything special about the ArgumentOutOfRangeException or ArgumentException types in Reflector, so I would guess it's something hard coded into Visual Studio. At a guess, I'd play around with having an Exception parameter called String paramName, inheriting from ArgumentException, or some combination.
Edit:
I don't get this intellisense prompt either, in VS 2010 SP1. I would look through your extensions, and maybe look for documentation on them. If they're open source you may be able to find out how it's implemented.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connect()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.Filter = "image file(*.jpg *.bmp *.png)|*.jpg; *.bmp; *.png| all files (*.*)|*.*"
If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
If OpenFileDialog1.FileName <> Nothing Or OpenFileDialog1.FileName <> "" Then
txtname.Text = OpenFileDialog1.FileName.Substring( _
OpenFileDialog1.FileName.LastIndexOf("\") + 1, _
(OpenFileDialog1.FileName.LastIndexOf(".", 0) - (OpenFileDialog1.FileName.LastIndexOf("\") + 1)))
End If
End Sub
End Class
It's pretty obviously looking at the variables you just used in the conditional that decided to throw it.