我需要通过访问自定义属性来获取属性的值

我需要通过访问自定义属性来获取属性的值

本文介绍了我需要通过访问自定义属性来获取属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要通过已分配给属性的自定义属性访问对象属性值。

让我用我的代码解释你



Hi,

I have a requirement to access the object property value through the custom attribute that''s been assigned to the property.
Let me explain you with the code which i have

public class busStudentDetails
   {
       [CustomAtrribute(1, "My Name")]
       public string Name { get; set; }
       [CustomAtrribute(3, "My Age")]
       public decimal Age { get; set; }
       [CustomAtrribute(2, "My DateofBirth")]
       public DateTime DateofBirth { get; set; }
   }

   [AttributeUsage(AttributeTargets.Property)]
public class CustomAtrribute : System.Attribute
{
       public int OrderNumber { get; set; }
       public string HeaderName { get; set; }
       public CustomAtrribute(int aintOrderName, string astrHeaderName)
       {
           OrderNumber = aintOrderName;
           HeaderName = astrHeaderName;
       }
}





现在点击按钮我需要填写学生详细信息的集合excel sheet



以下是代码:





Now on the click of button i need to populate the collection of Student Details in the excel sheet

Below is the code which does that

private void button1_Click(object sender, EventArgs e)
        {
            Collection<busStudentDetails> lclbStudentDetails = new Collection<busStudentDetails>();
            PopulateStudentDetails(lclbStudentDetails);

            Excel.Application oXL;
            Excel.Workbook oWB;
            Excel.Worksheet oSheet;
            Excel.Range oRange;

            // Start Excel and get Application object.
            oXL = new Excel.Application();

            // Set some properties
            oXL.Visible = true;
            oXL.DisplayAlerts = false;

            // Get a new workbook.
            oWB = oXL.Workbooks.Add(Missing.Value);

            // Get the active sheet
            oSheet = (Excel.Worksheet)oWB.ActiveSheet;
            oSheet.Name = "Student Details";

            Type lStudentTypeObject = typeof(busStudentDetails);

            PropertyInfo[] larrProperties = lStudentTypeObject.GetProperties();

            //Setting the Header Name with the custom attribute value
            for (int i = 1; i <= larrProperties.Length;i++ )
            {
                CustomAtrribute[] b = (CustomAtrribute[])(larrProperties[i - 1].GetCustomAttributes(typeof(CustomAtrribute), true));
                oSheet.Cells[1, b[0].OrderNumber] = b[0].HeaderName;
            }

            int RowCount = 2;
            //I need to set the value on the corresponding column without hard coding the column number and accesing the student detail property directly
            for (int i = 0; i < lclbStudentDetails.Count; i++)
            {
                oSheet.Cells[RowCount, 1] = lclbStudentDetails[i].Name;
                oSheet.Cells[RowCount, 2] = lclbStudentDetails[i].Age;
                oSheet.Cells[RowCount, 3] = lclbStudentDetails[i].DateofBirth;
                RowCount++;
            }

            //resize the columns
            oRange = oSheet.Range[oSheet.Cells[1, 1],
                          oSheet.Cells[RowCount - 1, lclbStudentDetails.Count - 1]];
            oRange.EntireColumn.AutoFit();

           oSheet.Range[oSheet.Cells[1, 3], oSheet.Cells[RowCount - 1, 3]].NumberFormat = "MM/DD/YYYY";


            // Save the sheet and close
            oSheet = null;
            oRange = null;
            string path = @"d:\" + "test" + DateTime.Now.ToString("HH-mm-ss") + ".xls";
            oWB.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Excel.XlSaveAsAccessMode.xlExclusive,
                Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value);
            oWB.Close(Missing.Value, Missing.Value, Missing.Value);
            oWB = null;
            oXL.Quit();

            // Clean up
            // NOTE: When in release mode, this does the trick
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        private void PopulateStudentDetails(Collection<busStudentDetails> aclbStudentDetails)
        {
            busStudentDetails lbusStudentDetails1 = new busStudentDetails();
            lbusStudentDetails1.Name = "Srikanth";
            lbusStudentDetails1.Age = 25;
            lbusStudentDetails1.DateofBirth = new DateTime(1984, 12, 26);
            aclbStudentDetails.Add(lbusStudentDetails1);

            busStudentDetails lbusStudentDetails2 = new busStudentDetails();
            lbusStudentDetails2.Name = "Kannan";
            lbusStudentDetails2.Age = 56;
            lbusStudentDetails2.DateofBirth = new DateTime(1954, 11, 11);
            aclbStudentDetails.Add(lbusStudentDetails2);

            busStudentDetails lbusStudentDetails3 = new busStudentDetails();
            lbusStudentDetails3.Name = "Srinivasan";
            lbusStudentDetails3.Age = 32;
            lbusStudentDetails3.DateofBirth = new DateTime(1978, 04, 12);
            aclbStudentDetails.Add(lbusStudentDetails3);

        }





这里在设置相应列中每个属性的值的步骤中我需要自动分配到正确的列。要获取列顺序,我可以使用Custom属性的OrderNumber属性。但是为了获得基于CustomAttribute的属性值,我感到磕磕绊绊。你能帮帮我吗?



谢谢,

Srikanth



Here in the step of setting the value of each property in the corresponding column i need to automate the assignment to the correct column. To get the column order i can use the OrderNumber property of Custom property. But to get the value of the property based on the CustomAttribute i am stumbling. Can you please help me?

Thanks,
Srikanth

推荐答案

for (int i = 0; i < lclbStudentDetails.Count; i++)
{
    for(int j = 0; j < larrProperties.Length; ++j)
    {
        CustomAtrribute[] b = (CustomAtrribute[])(larrProperties[j].GetCustomAttributes(typeof(CustomAtrribute), true));
        oSheet.Cells[RowCount, 1] = larrProperties[j].GetValue(lclbStudentDetails[i], null);
    }
    RowCount++;
}





希望这会有所帮助,

Fredrik Bornander



Hope this helps,
Fredrik Bornander



这篇关于我需要通过访问自定义属性来获取属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:11