Resharper provides a feature to create and initialize an autoproperty from a constructor parameter.
By default, the generated property is of the form:
public int Foo { get; set; }
Is it possible to modify the accessors to have this instead?
protected int Foo { get; private set; }
I checked with JetBrains and this is what they said:
Hi Jesse,
No, it is not possible in the currently. You are welcome, however, to log a feature request in our
issue tracker
So the answer is: No.
To generate this property I guess you are using the prop template
So you can either change it, or create a new one of your own
Go to ReSharper Menu -> Templates Explorer
Choose C# and look for prop , you can click edit and change it
But maybe the best way is to create a new one if you sometimes want default properties
Click new template and write that
protected $TYPE$ $NAME$ { get; private set; }
In the shortcut field type what you want, like prprop for example and save it.
Now open a file and type prprod, tab twice and you got the protected propertiy with private setter
If you are not using templates or snippets but the "Create auto-property from constructor" it seems it's impossible to change the access modifier, the only way to change it is when you use
Resharper -> Edit -> Generate Code ...
Then from there what you choose to generate you can change access modifiers or choose if it's read only.
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 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 :)
how can i get a list of all Style properties available ?
like in the visual studio desingner mode there's an IntelliSense properties list for each element supported,
so as soon as you type style= you get offerd by all available properties.
i would like to have it available as a collection or list in code behind too.
shouldn't it be available in a well known .net built in public class ?
i was searching the System.Web.UI.CssStyleCollection but couldn't get it through any method yet. i am sure it is (should be) very simple .
thanks in advance !
as explained in msdn :
CssStyleCollection for the specified HTML server control
i was trying to have all available properties not only those applied on a given element or control in a given page.
thanks for your comment #Abody97
found only source to be HtmlTextWriterStyle Enumeration so to avoid use of long name
HtmlTextWriterStyle.SomeProperty.ToString()
public sealed class StlProps
{
// in visual studio you can just mark the HtmlTextWriterStyle
// hit "F12" to its definition to have a list of properties
// just copy it as const strings
public const string BgColor = "BackgroundColor",
BackgroundImage = "BackgroundImage",
BorderCollapse = "BorderCollapse",
...etc'
}
this will let you set control style as follows
controlID.Style.Add(stlProps.BgColor, "value from Color Class");
Is there some shortcut way of handling multiple properties on a class (say 50 spanning string, int, datetime, etc). They will all have the same simple declaration such as
private int myInt;
public int MyInt
{ get { return myInt; }
set { myInt = value; }
}
private datetime someDate;
public datetime SomeDate
{ get { return someDate; }
set { someDate = value; }
}
The reason, is I have a class that will be "bound" to data entry textbox type fields and such. By just making them "public" doesn't work as it wont bind to a field, but will if it's a property with applicable get/set. I just think it's a pain to go through such efforts when it's the same repetition over and over, and believe there is a shorter / more simplified method. I just don't have another mentor to learn from and know that S/O has plenty to help out.
For the current situation I'm in, requires me to only work with .Net 2.0 max... Some restrictions based on handheld devices not yet capable of running 3.0, 3.5, etc.
In C# 3 or higher, you can use auto-implemented properties:
public int MyInt { get; set; }
In VS2010 & 2008 you can right click on the private field, select Refactor->Encapsulate Field.
You will still have to do it field by field, but it has got some smarts in it (with regards to choosing a publicly viewable name), and you can do it all with no typing.
Follow up: i see that the answer from Josh M shows you the keyboard shortcut to do the same thing.
Instead of using fields use properties to begin with:
public int MyInt { get; set }
public DateTime SomeDate { get; set; }
Try CTRL+R+E while on the field.
See more great shortcuts in this blog post.
I don't think there is any shortcut to create fields (other than manually typing it), though it is possible to create properties for "existing" fields in a class. So, in this case you will have write 50 fields, and then you can ask VS to auto-generate the properties for you. Even better if you have Resharper (i think, alt+insert will do the job).
If you have a list of columns/fields and their type, then you can use CodeDom. And then auto-generate the whole class, with all the fields and properties based on the list of columns you have provided.
You said you're stuck with .NET 2.0. Please note that you can use some C# 3.0 features but still target .NET 2.0 Framework. So as long as you use VS2008 and set the target to .NET 2.0 you can use autoprops (and a few other cool features of C# 3.0). Here is a bunch of links on this topic:
http://weblogs.asp.net/shahar/archive/2008/01/23/use-c-3-features-from-c-2-and-net-2-0-code.aspx
http://www.danielmoth.com/Blog/Using-Extension-Methods-In-Fx-20-Projects.aspx
http://www.developer.com/net/csharp/article.php/3598381/The-New-Lambda-Expressions-Feature-in-C-30.htm
type propfull then press TAB twice
In Visual Studio 2010, is it possible to change the default template used when implementing an interface?
I would like to change the implementation of properties from
public int MyProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
to
public int MyProperty { get; set; }
Edit
so i tried to Edit PropertyStub.snippet but to no avail, it didn't change anything...
I found this question Changing property stubs for interface refactoring which says that ReSharper is the only way.
Anyone able to make this work, with example, cause the change i made did not seem to have any effects
Yes, it is possible to change this template. The C# IDE uses templates for the majority of it's code generation and the IDE. You can update these templates to control the code generation process. They are located in the following directory
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Refactoring
Note: The " (X86)" portion will be absent on 32 bit machines.
For this particular situation you want to change the PropertyStub.snippet file.