Property Descriptor如何使用相同的代码行获取两个控件的值?

时间:2013-03-15 10:27:42

标签: c# .net gridview webforms

我在Ex-Employee开发的项目之一中找到了这个代码,用于自定义gridview和自定义控件,但是我不确定它到底在做什么,

CODE

public class aBoundField : ImageField
{
    //here I got some get set properties defined
    protected override void OnDataBindField(object sender, EventArgs e)
    {
        Control control = (Control)sender;

        PropertyDescriptor propertyA = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);
        PropertyDescriptor propertyB = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);

        PropertyAFieldValue = this.GetValue(control.NamingContainer, this._PropertyAField, ref propertyA).ToString();
        PropertyBFieldValue = this.GetValue(control.NamingContainer, this._PropertyBField, ref propertyB).ToString();
            base.OnDataBindField(sender, e);
    }

OnDataBindField方法中发生了什么,尤其是在获取PropertyDescriptor时。我做了一点研究,发现它是一个属性包,但如果它是一个属性包,它将如何知道此代码中属性A或属性B的值。

 PropertyDescriptor propertyA = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);
 PropertyDescriptor propertyB = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);

我不太清楚

属性描述符如何使用相同的代码行获取两个控件的值

TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true)

上面的代码行将如何确定它是属性A还是属性B.

我试图从一个属性描述符中获取值,认为它是一个属性包,但它没有正常工作。

2 个答案:

答案 0 :(得分:4)

GetValue(control.NamingContainer, this._PropertyAField, ref propertyA)

ProperyA作为参考,因为该方法内的propertyA内发生的一切都将更新上面定义的propertyA。

使用

PropertyDescriptor propertyA = null;

而不是

PropertyDescriptor propertyA = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);

仍然有效。

进一步阅读
ref method parameter keyword

答案 1 :(得分:0)

鉴于您最近的修改:

  

属性描述符如何使用相同的代码行获取两个控件的值

它不能。出于某种原因,前雇员希望propertyApropertyB都相同或存在拼写错误,实际上这是某种错误。