PropertyWalkerPage.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using CodingCannon.Pages.Basic;
  12. using Microsoft.CodeAnalysis.CSharp;
  13. using Microsoft.CodeAnalysis.CSharp.Syntax;
  14. namespace CodingCannon.Pages
  15. {
  16. internal partial class PropertyWalkerPage : CcUserControlBase
  17. {
  18. public PropertyWalkerPage()
  19. {
  20. InitializeComponent();
  21. Text = "Property Walker for MAPPING";
  22. }
  23. protected override void OnHandleCreated(EventArgs e)
  24. {
  25. base.OnHandleCreated(e);
  26. richTextBox1.AllowDrop = true;
  27. richTextBox1.DragOver += delegate (object? sender, DragEventArgs args)
  28. {
  29. args.Effect = args.Data.GetDataPresent(DataFormats.FileDrop)
  30. ? DragDropEffects.Link
  31. : DragDropEffects.None;
  32. };
  33. richTextBox1.DragDrop += delegate (object? sender, DragEventArgs args)
  34. {
  35. string[] files;
  36. if (args.Data?.GetDataPresent(DataFormats.FileDrop) != true
  37. || null == (files = (string[])args.Data.GetData(DataFormats.FileDrop)!)
  38. || files.Length != 1) return;
  39. var text = File.ReadAllText(files![0]);
  40. var tree = CSharpSyntaxTree.ParseText(text);
  41. var walker = new PropertyWalker();
  42. walker.Visit(tree.GetRoot());
  43. richTextBox1.Text = string.Join(Environment.NewLine, walker.PropertyNames.Select(p => $"t.{p}=f.{p};"));
  44. };
  45. }
  46. class PropertyWalker : CSharpSyntaxWalker
  47. {
  48. private readonly List<string> _properties = new();
  49. public IReadOnlyCollection<string> PropertyNames => _properties;
  50. public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
  51. {
  52. _properties.Add(node.Identifier.Text);
  53. base.VisitPropertyDeclaration(node);
  54. }
  55. }
  56. }
  57. }