admin管理员组文章数量:1123701
I have a legacy application with lots of cases where classes use an old-style (Java-like) property pattern with getXXX() and setXXX() method pairs and a backing private variable, for example:
class Example
{
private int x;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
}
Some classes and properties have been amended with a C# property wrapping the get/set methods pair as below:
public int X
{
get { return getX(); }
set { setX(value); }
}
Most to all of the code however, uses the get/set pairs.
I am looking for options how to refactor the code involving 100+ classes and 500+ properties so it uses standard C# properties instead and auto-properties where possible. For example, the result would be the following for the class above:
class Example
{
public int X { get; set; }
}
and obviously any code accessing the class's get/set methods would now be using the property instead.
While Visual Studio has a Quick Actions and Refactorings > Convert between auto property and full property option, I have not seen a way how to get it applied across the entire project/solution and also how to make it remove the property implementation that wraps the get/set methods.
Any help what tools could do this would be greatly appreciated.
I have a legacy application with lots of cases where classes use an old-style (Java-like) property pattern with getXXX() and setXXX() method pairs and a backing private variable, for example:
class Example
{
private int x;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
}
Some classes and properties have been amended with a C# property wrapping the get/set methods pair as below:
public int X
{
get { return getX(); }
set { setX(value); }
}
Most to all of the code however, uses the get/set pairs.
I am looking for options how to refactor the code involving 100+ classes and 500+ properties so it uses standard C# properties instead and auto-properties where possible. For example, the result would be the following for the class above:
class Example
{
public int X { get; set; }
}
and obviously any code accessing the class's get/set methods would now be using the property instead.
While Visual Studio has a Quick Actions and Refactorings > Convert between auto property and full property option, I have not seen a way how to get it applied across the entire project/solution and also how to make it remove the property implementation that wraps the get/set methods.
Any help what tools could do this would be greatly appreciated.
Share Improve this question asked yesterday user1211286user1211286 7475 silver badges19 bronze badges 11 | Show 6 more comments1 Answer
Reset to default 0Visual Studio has such a refactoring (only for single get- set-pairs so, not for the whole solution):
It also adapts the call sites:
After having done this, you can convert these properties to auto properties in your whole project:
本文标签:
版权声明:本文标题:Automated refactoring of old-style getset method pairs to true C# properties for entire solution - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736586831a1945024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get { return getX(); }
would generate a duplicateX
. Ironically, it's easier to detect with a regex – Panagiotis Kanavos Commented 23 hours ago