簡體   English   中英

在c#中使用變量訪問類屬性

[英]Accessing Class property using variable in c#

我在 c# 窗口應用程序中有以下模型類。 我有表單控件,我想在表單驗證后為模型屬性分配值。 我有更多的控件,許多控件是相同的。 因此,我不想單獨驗證每個控件,而是希望循環相同的控件並在 for 循環中進行驗證。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TShopLibrary
{
    public class CustomerModel
    {
        public int id { get; set; }
        public string Name { get; set; }
        public string Contact1 { get; set; }
        public string Contact2 { get; set; }
        public string RefContact { get; set; }
        public decimal ShirtLength { get; set; }
        public List<ShirtBottomTypeModel> ShirtBottomType { get; set; } = new List<ShirtBottomTypeModel>();
        public decimal Sleeve { get; set; }
        public decimal Shoulder { get; set; }
        public decimal Chest { get; set; }
        public decimal ShirtBottom { get; set; }
        public decimal ShalwarLength { get; set; }
        public decimal ShalwarWidth { get; set; }
        public decimal ShalwarBottom { get; set; }
        public decimal ShalwarBottomOpening { get; set; }
        public List<NeckTypeModel> NeckType { get; set; } = new List<NeckTypeModel>();
        public decimal ChestPlateLength { get; set; }
        public decimal ChestPlateWidth { get; set; }
        public decimal NeckWidth { get; set; }
        public decimal NeckHeight { get; set; }
        public decimal Pocket { get; set; }
        public List<SewingTypeModel> SewingType { get; set; } = new List<SewingTypeModel>();
        public decimal Comments { get; set; }
        public decimal CreatedDate { get; set; }
        public decimal UpdtedDate { get; set; }

        public CustomerModel()
        {

        }

    }
}

下面是我的表單代碼。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TShopLibrary;

namespace TShopUI
{
    public partial class AddCustomerForm : Form
    {
        private CustomerModel customermodel = new CustomerModel();
        public AddCustomerForm()
        {
            InitializeComponent();
        }

       

        private bool ValidateForm()
        {
           
        }

        private void CustomerSaveButton_Click(object sender, EventArgs e)
        {

            if (ValidateForm())
            {

            }
        }

        private void CustomerNameTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

在我的 ValidateForm() 我想要一些像下面這樣的東西。

foreach (Control ctrl in Controls.OfType<TextBox>())
            {
if(ctrl.Text!="")
{
 customermodel[ctrl.Name] = ctrl.text;
}
                
            }

在以上都很好。 問題是在

客戶模型[ctrl.Name] = ctrl.text;

你可以這樣做:

var props = typeof(Control).GetProperties();
foreach (var p in props)
{
    var value = p.GetValue(control_instance);
    if (p.Name == "Text")
        Do stuff....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM