2

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:

ScreenShot

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.

SimpleVar
  • 14,044
  • 4
  • 38
  • 60
  • 1
    I don't get that intellisense, do you have any VS extensions installed that might provide this functionality? – Jon May 14 '12 at 02:48

6 Answers6

4

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.

Community
  • 1
  • 1
Guillaume
  • 1,782
  • 1
  • 25
  • 42
4

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.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
3

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

Community
  • 1
  • 1
Tilak
  • 30,108
  • 19
  • 83
  • 131
0

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.

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
  • It was a good idea, trying to derive from the exception, but it doesn't work even if deriving from ArgumentOutOfRangeException itself, and delegating to all of it's ctors with the exact same naming. You were right about the extensions - updating the question. – SimpleVar May 14 '12 at 02:55
-1

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

-2

It's pretty obviously looking at the variables you just used in the conditional that decided to throw it.

Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45
  • I can lose the `if` statement - it has nothing to do with the intellisense. Even if it does, your answer doesn't help me understand how such intellisense is done. Logically, I can tell by myself that `num` should be thrown. I don't need "Logically". – SimpleVar May 14 '12 at 02:47