Dynamic code snippet c# visual studio

前端 未结 3 1245
我在风中等你
我在风中等你 2021-02-19 19:12

I am working on a WinForms project with some repetitive tasks everyday. So I thought creating code a snippet will help me out, but it works for fixed code only.

I want t

相关标签:
3条回答
  • 2021-02-19 19:54

    I think you can use the System.Reflection namespace combined with the System.CodeDom namespace to achieve what you want.

    You can use the classes in the System.Reflection namespace and the System.Type class to discover the assembly name, the namespace, the properties, the methods, the base class, and plenty of other metadata about a class or variable. You'll then use these metadata aquired from your starting/source classes (eg.your Form Classes) to generate code of your target classes (your business and data classes) using CodeDOM.

    Check this msdn link on How to: Create a Class using CodeDOM as a starting point.

    0 讨论(0)
  • 2021-02-19 20:09

    You can try code generators based on scripts like CodeSmith (there is also something free).
    You can write your script based on variables you define (also based on database items) then generate the code.
    Generating code is a good approach becouse saves time but also standardize the code.

    0 讨论(0)
  • 2021-02-19 20:12

    I want to add the code once the design part is done. I define the control names like intTextboxAge. The snippet should add auto validation for all textboxes, using the function defined below.

    It would be better to have CustomControls with their own Validation, this way you don't need to add code after the design part is done:

    enter image description here

    //Integer input CustomControl with validation label (pictured above)
    public partial class intTextBox : UserControl
    {
        public bool IsValid {get;set;}
        public intTextBox()
        {
            InitializeComponent();
            this.textBox1.TextChanged += this.intTextBox_TextChanged;
        }
    
        private void intTextBox_TextChanged(object sender, EventArgs e)
        {
            int n;
            IsValid = int.TryParse(this.textBox1.Text, out n);
            label1.Visible = !IsValid;
        }
    }
    

    There have to be different controls based on the control's name prefix (int, str, dou, dec)

    While you can use control name prefix's I recommend creating your own UserControls by deriving off base controls and simply testing the control type:

    //Decimal input UserControl
    public class decTextBox : TextBox
    {
        public string Text
        {
            get {
                return this.Text;
            }
        }
    
        public bool IsValid()
        {
            decimal n;
            bool isDecimal = decimal.TryParse(this.Text, out n);
            return isDecimal;
        }
    }
    

    ....

    public Control AutoCode(Control forEgTheForm)
    {
        //Tip: You may need to recursively call this function to check children controls are valid
        foreach(var ctrl in forEgTheForm.Controls) { 
            if(ctrl is TextBoxBase) {
                 if(ctrl is decTextBox) {
                     decTextBox txt = ((decTextBox)ctrl);
                     //If its not a valid value then set a[i]=true/false or in this example return the control...
                     if (!txt.IsValid()) return ctrl;
                 }
            }
        }
    }
    

    I want to dynamically create a code snippet, according to control names and some condition.

    If you're going to create code Snippets do it according to control types, not Control name prefix's so you dont need them to be dynamic.

    ...


    It would be even more elegant not to have to write any Snippets or validation code. This is the ideal solution. I recommend doing this by using the right type of controls.

    Textboxes are good for string, however for int's, dec's and dbl's you're better off using the NumericUpDown control. This way users will intuitively know they need to enter numbers (and wont be able to enter alphanumeric characters). After setting a couple of NumericUpDown control properties (either at design time or run time) you wont need to code in validation:

    enter image description here


    I'm not sure about the cause of the performance degradation you're encountering? Though I suggest you bind controls to a class in a class library with all the business logic so that you can Unit Test. For the front-end validation though simply validating inputs are correct as shown above is the way to go.

    0 讨论(0)
提交回复
热议问题