Automatically insert attribute above C# property in VS2015 - c#

I'd like to create some sort of Visual Studio add on where I could hover over a line like:
public int counter {get; set;}
and upon selecting my menu item (or light bulb), it would insert a line similar to the following above it:
[DataMember Order=1]
public int counter {get; set;}
I found this example:
https://msdn.microsoft.com/en-us/library/dn903708.aspx
but I'm not sure it's taking me in the right direction. I was thinking that adding a menu item might make sense (so I could bind it to a keyboard shortcut), but the tricky part would be intelligently setting the "Order" parameter based on an observation of other instances of that attribute nearby.
Any suggestions?

Your digging in the right direction. You might also be interested in the LightBulb extensibilty sample on GitHub.
Additionally, I'd recommend looking into the Roslyn code analysis APIs. The VS 2015 Roslyn based compilers might make it a bit easier to ID where you want those lightbulbs.

Related

C# Visual Studio 2013 suppress 'Class is never instantiated'

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.

C# rule for public fields

I have problem with public fields, which I use from time to time in my code. I keep forgeting to change them to private and create properties for them- especialy when Im testing some new part of code (and Im used to create public field for testing at first).
I was thinking that it would be fine to see some sort of "warning" if I use public field in my code.
I have found out, that I can create a ruleset (Im using Visual Studio Community 2013) and choose any of the rule I need. I searched for the rules relative to public fields and found these 2: CA2211: Non-constant fields should not be visible and CA1051: Do not declare visible instance fields. I checked these in the ruleset, tried to Run code analysis on whole solution but I cant see any warnings in the outcome.
I even tried to add something like public int i; in one of my classes but still nothing.
Do you know if I have the right rules or whether there is something else I should do to get the warning? Thank you.

Visual Studio: See a MyList class, in Debug, as if it was a DataGrid

I would like to use the DebuggerTypeProxy attribute to show, in Debug, a class using Datatable.
I try to better explain what I mean.
I can tell VS to show a class using another proxy class.
So if I have a list I can tell him to visualize that list after putting all the data's in a Datatable. So I can use the standard DebugVisualizer for datatables.
There are a few ways to provide custom debugging visualization,
Use [DebuggerDisplay] attribute
[DebuggerDisplay("Point {X}:{Y}")]
public class Point
{
public int X {get;set;}
public int Y {get;set;}
}
use DebuggerBrowsableDisplay attribute and set the State property to DebuggerBrowsableState.RootHidden - it will alow you to show collections like you have already pressed +
DebuggerTypeProxy attribute - for any custom visualizer.
But personally i wouldnt bother with writing a custom visualizer for the problem that you are describing - there are already debugging products that can do it for you.
You can download OzCode, VS extencion that is still free in its beta and use its Reveal feature:
http://o.oz-code.com/features#reveal
It seems to be exactly what you need :)

Shortcut for autocompleting properties in visual studio?

I was watching a video about C# and the guy used a shortcut to implement a property.
public decimal Price { get; set; }
He wouldn't write the whole line, but instead let the visual studio editor fill in the get and set. Does anyone know the shortcut for completing the last part of the line above? Or even better, like a pdf quick-reference overview?
In Visual Studio 2010 you can type 'prop' then press tab twice. It's probably the same in earlier versions.
Edit: You'll then have to change the type (defaults to int) and the name (defaults to MyProperty).
Edit2: I've just found this great tool that allows you to edit these code snippets to override their default behaviour, or create new ones: http://snippeteditor.codeplex.com/

ReSharper Code Snippet for Auto-Property

Is there a way to create an auto property via code snippet using ReSharper? Like the prop for VS?
Resharper 5 has a prop snippet which goes to auto-property. Does 4.5 not?
You can create a Live Template
Just go to the Resharper menu, and choos Live Templates. Click the little green + icon and add this:
public $type$ $name$ { get; set; }
Then give the wanted shortcut, and you are good to go.
Yes. You can setup any snippet you wish. These are called "Live Templates" in Resharper, and have a lot more functionality than the VS versions. Just choose "Resharper->Live Templates.." in VS to edit them.

Categories

Resources