remoting返回实现了IList接口的集合对象,无法在客户端的combobox.datasource中绑定的问题
我的解决方案的结构:
CommonInterface : 客户端和服务端公用的接口定义
BusinessRules : 服务端具体实现
WinUI : 客户端
公用接口CommonInterface.dll 中代码
public interface IEmployeeOnlineManage
{
IEmployeeOnlineCollection getEmployeeList();
}
public interface IEmployeeOnlineCollection : IList
{
new IOnlineEmployee this[int index] { get;set;}
}
public interface IOnlineEmployee
{
string employeeId{ get; }
string employeeName{ get; }
}
具体实现BusinessRules.dll 中代码()
public class EmployeeOnlineManage : MarshalByRefObject, IEmployeeOnlineManage
{
private static IEmployeeOnlineCollection employeeOnlineList = new EmployeeOnlineCollection();
public IEmployeeOnlineCollection getEmployeeList()
{
for (int i = 0; i < 5; i++)
employeeOnlineList.Add( new OnlineEmployee(i.ToString(), "name" + i.ToString()) );
return employeeOnlineList;
}
}
public class OnlineEmployee : MarshalByRefObject, IOnlineEmployee
{
protected string _employeeId = string.Empty;
protected string _employeeName = string.Empty;
public string employeeId
{
get { return _employeeId; }
}
public string employeeName
{
get { return _employeeName; }
}
public OnlineEmployee(string _employeeId, string _employeeName)
{
this._employeeId = _employeeId;
this._employeeName = _employeeName;
}
}
public class EmployeeOnlineCollection : MarshalByRefObject, IEmployeeOnlineCollection
{
protected ArrayList _arrCollection = new ArrayList();
protected int count = 0;
public IOnlineEmployee this[int index]
{
get
{
return (IOnlineEmployee)this._arrCollection[index];
}
set
{
this._arrCollection[index] = (IOnlineEmployee)value;
}
}
public EmployeeOnlineCollection()
{
;
}
#region IList 成员
public bool IsReadOnly
{
get
{
return this._arrCollection.IsReadOnly;
}
}
object System.Collections.IList.this[int index]
{
get
{
return (IOnlineEmployee)this._arrCollection[index];
}
set
{
this._arrCollection[index] = (IOnlineEmployee)value;
}
}
public void RemoveAt(int index)
{
this._arrCollection.Remove(index);
}
public void Insert(int index, object value)
{
this._arrCollection.Insert(index, (IOnlineEmployee)value);
}
public void Remove(object value)
{
this._arrCollection.Remove((IOnlineEmployee)value);
}
public bool Contains(object value)
{
return this._arrCollection.Contains((IOnlineEmployee)value);
}
public void Clear()
{
this._arrCollection.Clear();
}
public int IndexOf(object value)
{
return this._arrCollection.IndexOf((IOnlineEmployee)value);
}
public int Add(object value)
{
IOnlineEmployee iOnlineEmployee = (IOnlineEmployee)value;
return this._arrCollection.Add(iOnlineEmployee);
}
public int Add(IOnlineEmployee iOnlineEmployee)
{
return this._arrCollection.Add(iOnlineEmployee);
}
public bool IsFixedSize
{
get
{
return this._arrCollection.IsFixedSize;
}
}
#endregion
#region ICollection 成员
public bool IsSynchronized
{
get
{
return this._arrCollection.IsSynchronized;
}
}
public int Count
{
get
{
this.count = this._arrCollection.Count;
return this.count;
}
}
public void CopyTo(Array array, int index)
{
foreach (IOnlineEmployee iOnlineEmployee in _arrCollection)
{
array.SetValue(iOnlineEmployee, index);
index = index + 1;
}
}
public object SyncRoot
{
get
{
return this;
}
}
#endregion
#region IEnumerable 成员
public System.Collections.IEnumerator GetEnumerator()
{
return new EmployeeOnlineEnumerator(_arrCollection);
}
#endregion
}
public class EmployeeOnlineEnumerator : MarshalByRefObject, IEnumerator
{
protected ArrayList array = new ArrayList();
protected int cursor = -1;
public EmployeeOnlineEnumerator()
{
;
}
public EmployeeOnlineEnumerator(ArrayList arr)
{
this.array = arr;
this.cursor = -1;
}
#region IEnumerator 成员
public void Reset()
{
this.cursor = -1;
}
public object Current
{
get
{
if ((this.cursor < 0) || (this.cursor == array.Count))
throw new InvalidOperationException();
return array[this.cursor];
}
}
public bool MoveNext()
{
if (this.cursor < array.Count)
this.cursor++;
return (!(this.cursor == array.Count));
}
#endregion
}
服务端用windows服务承载
服务端配置文件
<configuration>
<system.runtime.remoting>
<application name="RemoteService">
<service>
<wellknown type="BusinessRules.EmployeeOnlineManage, BusinessRules"
objectUri="BusinessRules.EmployeeOnlineManage" mode="Singleton" />
</service>
<channels>
<channel ref="tcp" port="8085">
<serverProviders>
<formatter ref="binary" />
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
客户端 WinUI.exe 代码:
private static readonly TcpChannel _tcpChannel = new TcpChannel();
private void Form_Load(object sender, EventArgs e)
{
ChannelServices.RegisterChannel(_tcpChannel,false);
IEmployeeOnlineManage iEmployeeOnlineManage = (IEmployeeOnlineManage)Activator.GetObject(
typeof(CommonInterface.IEmployeeOnlineManage),
"tcp://localhost:8085/BusinessRules.EmployeeOnlineManage");
IEmployeeOnlineCollection iEmployeeOnlineCollection = iEmployeeOnlineManage.getEmployeeList();
comboBox1.DataSource = iEmployeeOnlineCollection;
comboBox1.ValueMember = "employeeId"; // 运行到这里报错: System.ArgumentException:
// 无法绑定到新的显示成员。参数名: newDisplayMember
// 然后界面combobox中显示的全是BusinessRules.OnlineEmployee
comboBox1.DisplayMember = "employeeName";
}
看了两天的remoting,第一次做东西就出了大问题,忙活了一天了,没有头绪.
我想利用接口来分离元数据,不让具体实现暴露在客户端,但是出现了上面的情况,这个具体怎么解决?
客户端怎样才能认出iEmployeeOnlineCollection中的元素?