相关疑难解决方法(0)

反映基类的私有字段

这是结构:

MyClass:SuperClass2

SuperClass2:SuperClass1

superClass2位于Product.Web中,SuperClass1位于.NET System.Web程序集中

我试图强制一个值进入SuperClass1上的私有bool字段.但无论我尝试什么,我都无法让领域从反思回来.

我使用以下代码与不同的BindingFlag组合,但到目前为止没有任何工作.SuperClass1是一个抽象类.

((SuperClass1)this).GetType().GetFields(System.Reflection.BindingFlags.NonPublic);
Run Code Online (Sandbox Code Playgroud)

注意: 当我使用GetProperties()时,我得到了一个很好的大清单,但是当我指定任何绑定标志时,即使有匹配的属性,我什么也得不到.这是怎么回事?

此外,该字段未标记为内部

显然我会使用GetField(字符串名称,BindingFlags),但我甚至无法使GetFlags()工作.

更新:我已经尝试按照建议添加BindingFlags.Instance,但它不起作用(无论如何都是预期的).我找回了来自SuperClass1类继承自的2个字段.与GetField一起使用时返回null(字符串名称,标志)

这是我试图获取该字段的基类的代码

public abstract class BaseValidator : Label, IValidator
  {
    private bool propertiesChecked;
...
}
Run Code Online (Sandbox Code Playgroud)

.net c# reflection

24
推荐指数
3
解决办法
3万
查看次数

DataGridViewComboBoxColumn - 值滞后一瞬间

我有一个带有一些DataGridViewComboBoxColumns的DataGridView.DataGridView上有一个CellEnter事件处理程序,用于单击下拉组合框.

该列绑定到KeyValuePairs列表,ValueMember为"Key",DisplayMember为"Value".

当我点击组合框列时,它工作正常.但是,如果单元格处于"下拉"状态并且我单击另一个组合框(相同的列,不同的行),它会正确地取消选择旧单元格,选择并下拉新单元格,但是顶部的选定值会更改为在更改回正确的值之前,从旧单元格中获取值一瞬间.

例如,假设列表是A,B,C.在row1中,选择A,在row2中选择B. 我单击row1中的单元格,一切都是应该的.然后,当这个单元格下拉时,我点击第2行中的单元格.它正确地下降,但顶部的选定值变为A,然后立即切换回B(正确的值).

如果在单击第二个组合框单元格之前单击其他列中的单元格,则不会发生这种情况.

有没有办法防止这种情况发生?

重现问题的示例代码(事件处理程序连接到明显的事件):

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;

namespace PDGV
{
    public partial class Form1 : Form
    {
        List<KeyValuePair<string, string>> bindingList = new List<KeyValuePair<string, string>>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(10);
            bindingList.Add(new KeyValuePair<string,string>("aaa", "111"));
            bindingList.Add(new KeyValuePair<string,string>("bbb", "222"));
            bindingList.Add(new KeyValuePair<string,string>("ccc", "333"));
            bindingList.Add(new KeyValuePair<string,string>("ddd", "444"));
            bindingList.Add(new KeyValuePair<string,string>("eee", "555"));
            BindComboList(2, bindingList);

        }

        private void BindComboList(int columnIndex, object list)
        {
            var …
Run Code Online (Sandbox Code Playgroud)

c# datagridview

5
推荐指数
1
解决办法
1528
查看次数

标签 统计

c# ×2

.net ×1

datagridview ×1

reflection ×1