发现一个问题,各位兄弟们看看。偶是新手,多多指教啊。
实体类以及相关接口定义如下:
public interface ISerialObject {
int UID { get; }
string Name { get; set; }
}
public interface IChem : ISerialObject {
string RptName { get; set; }
}
public class Chem : IChem {
public Chem() {
id = ++no;
Name = id.ToString();
RptName = Name + Name;
}
private static int no = 0;
private int id;
public int UID {
get{
return id;
}
}
private string name;
public string Name {
get { return name; }
set { name = value; }
}
private string rptName;
public string RptName {
get { return rptName; }
set { rptName = value; }
}
public static IChem Create()
{
return new Chem();
}
}
窗口有2个TextBox用来绑定数据源
private BindingSource bindSrc = new BindingSource();//定义为窗口成员变量
private void Form1_Load(object sender, EventArgs e)
{
BindingList<IChem> curr = new BindingList<IChem>();
curr.Add( Chem.Create() );
curr.Add(Chem.Create());
bindSrc.DataSource = curr;
ISerialObject d = (ISerialObject)curr[0];
IChem f = (IChem)d;//调试时,看内存,居然只有RptName,Name好像不可见
f.Name = "22";//但是可以直接修改Name
Binding nameBinding = this.textBox1.DataBindings.Add("Text", bindSrc, “RptName", true);//正常运行
nameBinding = this.textBox2.DataBindings.Add("Text", bindSrc, "Name", true);//异常 ,报错。
}
如果全换成类,试下缺可以
我整理下代码格式,请大家看看
高手分析下原因:)多谢了