1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using CodingCannon.Pages.Basic;
- using Microsoft.CodeAnalysis.CSharp;
- using Microsoft.CodeAnalysis.CSharp.Syntax;
- namespace CodingCannon.Pages
- {
- internal partial class PropertyWalkerPage : CcUserControlBase
- {
- public PropertyWalkerPage()
- {
- InitializeComponent();
- Text = "Property Walker for MAPPING";
- }
- protected override void OnHandleCreated(EventArgs e)
- {
- base.OnHandleCreated(e);
- richTextBox1.AllowDrop = true;
- richTextBox1.DragOver += delegate (object? sender, DragEventArgs args)
- {
- args.Effect = args.Data.GetDataPresent(DataFormats.FileDrop)
- ? DragDropEffects.Link
- : DragDropEffects.None;
- };
- richTextBox1.DragDrop += delegate (object? sender, DragEventArgs args)
- {
- string[] files;
- if (args.Data?.GetDataPresent(DataFormats.FileDrop) != true
- || null == (files = (string[])args.Data.GetData(DataFormats.FileDrop)!)
- || files.Length != 1) return;
- var text = File.ReadAllText(files![0]);
- var tree = CSharpSyntaxTree.ParseText(text);
- var walker = new PropertyWalker();
- walker.Visit(tree.GetRoot());
- richTextBox1.Text = string.Join(Environment.NewLine, walker.PropertyNames.Select(p => $"t.{p}=f.{p};"));
- };
- }
- class PropertyWalker : CSharpSyntaxWalker
- {
- private readonly List<string> _properties = new();
- public IReadOnlyCollection<string> PropertyNames => _properties;
- public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
- {
- _properties.Add(node.Identifier.Text);
- base.VisitPropertyDeclaration(node);
- }
- }
- }
- }
|