我在一位前雇员开发的一个项目中找到了这段代码,该项目用于带有自定义控件的自定义 gridview,它工作正常,但我不确定它到底在做什么,
代码:
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);
我不太明白的是
Property Descriptor如何使用相同的代码行获取两个控件的值
TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true)
上面的代码行如何判断它是针对属性 A 还是属性 B。
我试图从一个属性描述符中获取值,认为它是一个属性包,但它没有正常工作。
最佳答案
GetValue(control.NamingContainer, this._PropertyAField, ref propertyA)
ProperyA 作为引用给出,因此在该方法内发生在 propertyA 上的所有事情都将更新上面定义的 propertyA。
使用
PropertyDescriptor propertyA = null;
代替
PropertyDescriptor propertyA = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);
仍然有效。
关于c# - 属性描述符如何使用相同的代码行获取两个控件的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15434820/