1
\$\begingroup\$

My company is converting a VB.NET project to a C# application and I was tasked with writing a script to automate part of the process. In particular, given information about a control name and event in VB.NET, I'm trying to insert the corresponding event into the new XAML file and its code-behind.

My approach is based partially on this Stack Overflow answer.

Basically, I'm trying to insert some text before the last two lines of a file. Specifically, I'm inserting methods into a XAML code-behind file.

The XAML code-behind is pretty typical, but just for reference, here's an example:

using System.Windows;

namespace MyNamespace
{
    /// <summary>
    /// Interaction logic for myWindow.xaml
    /// </summary>
    public partial class myWindow : Window
    {
        public myWindow()
        {
            InitializeComponent();
        }

        private void DO_CANCEL_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        // I want to insert the new method here.
        // It's possible that I'll end up inserting several, but I don't know
        // how many I'll end up inserting "in advance."
    }
}

I do the following:

private static void AddEvent(MethodNameParts parts)
{
    // xamlFile is the full name and path of the .xaml file (the actual XML front-end)
    string windowsCsFile = xamlFile + ".cs";

    var lines = File.ReadAllLines(windowsCsFile);

    // Write the entire file except for the last two closing }
    // I have logic (not shown) to make sure that the file is "trimmed" properly before I run this
    // Thus, the last two lines will definitely be the "}" that closes the
    // class and the "}" that closes the namespace
    File.WriteAllLines(windowsCsFile, lines.Take(lines.Length - 2));

    // parts.CSFullLine is a string containing the C# method I want to insert
    File.AppendAllLines(windowsCsFile, new[] { parts.CSFullLine });
    File.AppendAllLines(windowsCsFile, new[] { "}", "}" });
}

Is this a good solution, or should I do something else to avoid having to do so many writes?

\$\endgroup\$
  • 1
    \$\begingroup\$ 1. What code do you want to "inject" in the file? Actually, I can't imagine a use case for that ;). 2. Do you know T4 Templates? May be a (more robust) alternative for you :). \$\endgroup\$ – JanDotNet Jun 28 '17 at 9:53
  • \$\begingroup\$ @JanDotNet I'm just appending empty event handlers. \$\endgroup\$ – EJoshuaS Jun 28 '17 at 11:47
  • 2
    \$\begingroup\$ I'm inserting methods into a XAML code-behind file. - Why? This sounds like a bad idea. \$\endgroup\$ – t3chb0t Jun 28 '17 at 17:26
  • 1
    \$\begingroup\$ @t3chb0t I edited to mention why I'm trying to do that in the question; in retrospect, the way I originally phrased my question did sound kind of like an instance of the XY problem. \$\endgroup\$ – EJoshuaS Jun 29 '17 at 21:20
  • 1
    \$\begingroup\$ I would warn against doing this. You can easily do Winforms in WPF and doing this will result in exactly that. It may result in a faster conversion, but you'll lose all the benefits of moving to WPF. You'll be better off in the long run to take advantage of WPF's MVVM architecture, but that will require a rewrite. That aside, I'll second investigating T4 templates. I've not used them myself, but seems fit for purpose. \$\endgroup\$ – RubberDuck Jun 30 '17 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.