Using Visual Studio 2015 Enterprise with Resharper Ultimate.
I find myself often needing to declare a new field, and then have it assigned by a constructor argument. As far as I know, the quickest way is to:
Navigate up to the top of the file where the field declarations are
Declare the field:
private readonly NameOfType _nameOfInstance;
alt-enter on the field name, "Initialize field from constructor"
navigate back to where I needed the object and continue
This isn't too onerous, I just find that I do it so many times a day, I'm left wondering whether there's a faster way. Is there a Resharper / VS function / plugin where I can press a key, specify a type, and have it do all of those steps for me? The _nameOfInstance is nearly always just _nameOfType, and it's always private readonly and constructor-injected, so it would be good to reduce this bit of busy-work from my day.
With ReSharper, you can place the cursor in (or highlight) the parameter in the constructor and use CTRL+R, CTRL+F to create a field from it.
You can type an unused symbol name in your code and hit Alt+Enter on it to automatically create a field (or even better, type the new field name, followed by .field, which will act like a template/snippet and automatically create a field with that name at the top of the class). You can then Alt+Enter on the new field and select "initialise from constructor" to wire it up in the constructor. This will move you out of the current method, though. ReSharper doesn't offer a way around that, but you can use the "Navigate back" command (Ctrl+Minus) to jump back to the method you were working on.
Related
I have a web api project which accepts HttpPost communications.
The controller's methods always accepting a single validated object.
For example:
public sealed class NumbersRequest
{
[NumberOne]
public string Number1 { get; set; }
[NumberTwo]
public string Number2 { get; set; }
}
Since I never declare NumbersRequest req = new NumbersRequest() and they only serve as a request object, Im getting the
class is never instantiated
How can I suppress the warning? (its more like a green underline..)
Maybe something with annontations?
Thanks.
This looks like a ReSharper warning and as such you can ask ReSharper to be silent about these things.
You can either configure ReSharper to stop complaining about this overall, you do this simply by hitting Alt+Enter on the squiggly in question and use the bottom menu item that usually allows you to configure the inspection severity.
You can opt to save this in your global settings, which means it will affect every project you open from now on, or you can save it to a team-shared settings file which you can then check into source control alongside your project, to make it only count for this one solution.
Now, if you want to keep the warning overall but ask it to stop complaining about one or more particular types, methods, properties or the likes, you can use the attributes that ReSharper provides.
You have several ways of bringing these attributes into your project:
Add a reference to the Nuget package "JetBrains ReSharper annotations"
Use the options dialog for ReSharper and find the page where it allows you to grab a copy of the source for those attributes onto the clipboard, then simply paste this into a file in your project.
Define just the one or two attributes you want, even in your own namespace (which you then have to tell ReSharper about)
The recommended way is option 1, use the nuget package.
Assuming you now have the attributes available you can use either PublicAPIAttribute or the UsedImplicitlyAttribute.
Either one should suffice but they may have different connotations. Since you're flagging objects being transferred to or from clients I would go with the PublicAPIAttribute first.
Since you say in a comment that the PublicAPIAttribute didn't work but UsedImplicitlyAttribute did then I guess they do have different meanings.
I started writing a new C# app. I have created a couple of classes that i would like to declare and instantiate in a Form. The thing is, when i write it, the intellisense does not detect it. I will provide a screen shot, as I don't know how to better explain.
DataHelper is defined under the BuildID namespace.
You have 2 options:
The first is to declare it like this:
private BuildID.DataHelper _dataHelper //...;
The second is to add a using BuildID at the top of the page, you can either write it manually or have your cursor on the DataHelper object which is unrecognized (at the moment), press
Ctrl + . and choose the option that the Visual Studio offers you, to add the using statement automatically.
I Think to instantiate it you wanna do
"Private DataHelper data = new DataHelper();
Given a string that represents a specific class/field/property (eg MyNameSpace.MyClass or System.String.Length), how can I programmatically cause Visual Studio to go to that class/field/property (ie, make Visual Studio do the same thing that would happen if I was to type in the reference in the code editor and then hit F12)?
You probably need to do the following.
Get the global IVsObjectManager2 interface (implemented by the SVsObjectManager object)
Call IVsObjectManager2.FindLibrary to get the C# library, and cast the result to IVsSimpleLibrary2.
Call IVsSimpleLibrary2.GetList2 with the correct VSOBSEARCHCRITERIA2 in order to locate the symbol within the projects for your solution.
If the resulting IVsSimpleObjectList2 has GetItemCount()==1, and CanGoToSource with VSOBJGOTOSRCTYPE.GS_DEFINITION returns pfOK==true, use the GoToSource method to jump to the source.
Otherwise, rather than jumping to the source, simply display the possible options to the user. You will be able to use the IVsFindSymbol interface (implemented by the SVsObjectSearch object) to for this.
I've been working on a debugger visualizer for Visual Studio for some time and while the actual visualizer works fine. The problem is that it always places itself at the top of the visualizer list when examining a variable which really annoys some of the users who rather have Text as the top one (since the top one is also default when opening VS).
I can't find any support for this on DialogDebuggerVisualizer or DebuggerVisualizerAttribute which were my first thoughts so I've been scouring SO/MSDN/Google for information on how to affect the sort order of the visualizers (preferably to put mine last in the list) but to no avail.
Below is how I register my visualizer, it then just shows a form based on the value that is being visualized.
using Microsoft.VisualStudio.DebuggerVisualizers;
[assembly: System.Diagnostics.DebuggerVisualizer(
typeof(Shorthand.VSAddins.JsonVisualizer.JsonVisualizer),
typeof(VisualizerObjectSource),
Target = typeof(string),
Description = "Json Visualizer")]
namespace Shorthand.VSAddins.JsonVisualizer
{
public class JsonVisualizer : DialogDebuggerVisualizer
{
protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
{
var json = objectProvider.GetObject() as string;
var form = new VisualizerForm { Json = json };
windowService.ShowDialog(form);
}
}
}
Does anyone know if it is possible to affect the order of the visualizers or should I just let it be?
I don't think there is a solution. But there is a workaround:
Define your own Text Visualizer and put appropriate DebuggerVisualizer attribute before the attribute of your JsonVisualizer. The result will be that string will be readable by default and Json Visualizer can be chosen. A window with a multi-line textbox is not too much work.
It is probably not even necessary to write visualizer. It should be possible to use internal one but I don't know its name (Which class is used for "Text Visualizer"?).
It will always appear first, by design. The under the hood cast has found the best match for the variable it is reflecting on.
however, you could do either of two things. You could make the visualizer only appear when the sting contains ':'
Or you could use reflection to reorder the visualisers by adding them to the end of the collection in the order you want, then removing the originals from the collection.
For the latter you will most likely have to change the collection from readonly to writable. Via reflection.
There is no reliable source to draw on other than your will to succeed.
I guess that VS 'under the hood' can distinguish between type of string and type of xml quite easily, but Xml is just a string too, so a key question here would be, how does VS tell the difference between the two?
Could you dissect the VS XML visualizer to see how it works (even if you have to use reflector on the DLL to do it, you might get to see the method that works it out)
I often find myself remembering the name of a class that I want to use, but not remembering the containing namespace.
Apart from searching the web, i wonder if a good method exists for looking this up.
I think if you press ALT, SHIFT and F10 in Visual Studio - intellisense will drop down an option for you to add the name space of the class you have just typed.
CTRL + '.' will bring up a menu where you can either add a 'using' or fully qualify the class.
You can always hang a big poster on your cube wall like me.
3.5 NameSpace
If you know the name of a class in .Net but have no idea what namespace it is in, it can be hard finding it, especially if you dont have a reference/using to the assembly containing it.
This is where the Object Browser (Ctrl+W,J) comes in handy.
Open it up, type in the name, it will give you all matches, either within your project/solution, or all of the .Net framework.
Edit:
As S.C. Madsen's comment points out, this also helps if you only remember PART of a class name, also if you only remember a method name but not the class.
Use the search function in .NET Reflector by Red Gate Software.
I generally use the offline MSDN reader, with the left panel set to the Index tab.
Another option in Visual Studio is to type the name of the type as if you were declaring a variable, and then see what it suggests. If the name goes to a light blue colour (by default) then it's in one of the namespaces you're already importing - just hover over it to find out which. Otherwise, see what namespaces it offers to add using directives for.
You can right click and select "Go To Definition" in VS and this will either load the class definiftion in your solution or it will show a metadata view of the class definition using reflection. Either of those should have the namespace defined near the top of the page.
If you need to add the namespace with a using decliration right click the unresolved class and mouse over to resolve. It will show you a list of namespaces that contain that class and selecting one will generate the using statement.
Two ways that work in Visual Studio 2013:
Right-click and select "Resolve".
Hover over the class and a 'Options to help bind the selected item' box will appear (same as Ctrl + '.' or Alt+Shift+F10)
Select the namespace and it will insert it for you.