Created
June 6, 2017 18:12
-
-
Save rquackenbush/efe66a6f2977d0b1af8f51790ef6b49e to your computer and use it in GitHub Desktop.
Resharper hasn't seen fit to implement the "encapsulate multiple fields" feature. This is a hacktastic way to encapsulate read only properties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Text; | |
using System.Windows; | |
namespace Encapsulate | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void EncapsluateButton_Click(object sender, RoutedEventArgs e) | |
{ | |
CodeTextBox.Clear(); | |
CodeTextBox.Paste(); | |
int lineCount = CodeTextBox.LineCount; | |
StringBuilder result = new StringBuilder(); | |
for (int index = 0; index < lineCount; index++) | |
{ | |
string rawLine = CodeTextBox.GetLineText(index); | |
if (!string.IsNullOrWhiteSpace(rawLine)) | |
{ | |
Process(rawLine, result); | |
} | |
} | |
Clipboard.SetText(result.ToString()); | |
} | |
private void Process(string line, StringBuilder result) | |
{ | |
if (line == null) | |
return; | |
line = line.Trim(); | |
if (line.StartsWith("//")) | |
return; | |
line = line.Replace(";", " "); | |
string[] split = line.Split(new char[] {' '}); | |
string[] populatedSplits = split | |
.Where(s => !string.IsNullOrWhiteSpace(s)) | |
.ToArray(); | |
string type = populatedSplits[2]; | |
string fieldName = populatedSplits[3]; | |
string propertyName = GetPropertyName(fieldName); | |
result.AppendLine($" public {type} {propertyName}"); | |
result.AppendLine(" {"); | |
result.AppendLine(" get { return " + fieldName + " ; } "); | |
result.AppendLine(" }"); | |
} | |
private string GetPropertyName(string fieldName) | |
{ | |
string temp = fieldName; | |
if (temp.StartsWith("_")) | |
{ | |
temp = temp.Substring(1); | |
} | |
return temp.Substring(0, 1).ToUpper() + temp.Substring(1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment