我有两个对象:User和Client,都实现了接口IMember
interface IMember
{
int Id { get; set; }
string Name { get; set; }
}
在表单中我设置了ListBox数据源:
myListBox.DisplayMember = "Name";
myListBox.ValueMember = "Id";
myListBox.DataSource = membersList; // List<IMember>
并且发生了奇怪的事情;)当我运行程序时,第一个对象(正是这种类型的用户)正确显示(Adrian Serafin)和其他对象,这些类型的联系人显示如下:
MyProject.Client#20
MyProject.Client#40
至于显示是对它们的ToString()调用。
我不能使用与ListBox中的数据源相同的接口实现相同接口的不同对象列表,或者我在这里犯了一些错误?
答案 0 :(得分:1)
你确定它是List<IMember>
,而不是ArrayList
?这会产生很大的不同,因为非object
公共索引器(public SomeType this[int] {get;set;}
)的存在会产生大差异。以下工作正常:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
interface IMember
{
int Id { get; set; }
string Name { get; set; }
}
class Foo : IMember
{
public int Id { get; set; }
public string Name { get; set; }
}
class Bar : IMember
{ // explicit, why not...
int IMember.Id { get; set; }
string IMember.Name { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
IMember bar = new Bar();
bar.Id = 2;
bar.Name = "def";
var list = new List<IMember> {
new Foo { Id = 1, Name = "abc"},
bar,
};
Application.Run(new Form
{
Controls = {
new ListBox {
Dock = DockStyle.Fill,
DisplayMember = "Name",
ValueMember = "Id",
DataSource = list
}
}
});
}
}
答案 1 :(得分:0)
成员是班级活动的财产:
[ActiveRecord("cpa_events")]
public class Event : ARBase<Event>
{
private IList<Interfaces.IMember> _members = new List<Interfaces.IMember>();
[HasManyToAny(typeof(Interfaces.IMember),"event_id","cpa_events_members", typeof(int), "member_type", "member_id", MetaType=typeof(string))]
[Any.MetaValue("User", typeof(User))]
[Any.MetaValue("Client", typeof(Client))]
public IList<Interfaces.IMember> Members
{
get { return _members; }
set { _members = value; }
}
}
我正在使用AR,因此成员列表必须是IList,而下面不是List而是NHibernate.Collection.Generic.PersistentGenericBag [Interfaces.IMember]。这有什么不同吗?