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

标签 c# .net gridview webforms

我在一位前雇员开发的一个项目中找到了这段代码,该项目用于带有自定义控件的自定义 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);

仍然有效。

延伸阅读
ref method parameter keyword

关于c# - 属性描述符如何使用相同的代码行获取两个控件的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15434820/

相关文章:

c# - 安装后 LARGEADDRESSAWARE 关闭了吗?

c# - TextBox 不按代码 C# 顺序显示初始文本

c# - 您会购买哪本 C# 4.0 书籍,为什么?

android - 在 android 的 GridView 中删除额外的填充

c# - gridview findcontrol 返回空 ""

c# - x86 控制台应用程序不能在 64 位计算机上运行(调试器除外)

c# - Rx IObservable 仅在值发生一定幅度变化时才产生值

c# - WP7 Mango - 建筑物内导航

c# - 基于输入数据的自定义返回类型

android - 修改堆栈项目计数并在堆栈 View 中显示 ListView / GridView