参数减去构造函数错误 [英] parameter less constructor error
问题描述
我正在尝试让客户端订阅服务器上发生的事件.
I'm trying to make my client subscribe to events that happen on my server.
我有一个看起来像这样的界面:
I have an interface that looks like this:
public delegate void RemoteEventHandler(object sender, ClientEventArgs args);
[Serializable]
public class ClientEventArgs : EventArgs
{
public ClientEventArgs()
{ }
public ClientEventArgs(Client _client)
{
MyClient = _client;
}
public Client MyClient { get; set; }
}
public interface IMonitor
{
event RemoteEventHandler RemoteEvent;
}
我的服务器类如下:
public class ConnectionManager : MarshalByRefObject, IMonitor
{
public event RemoteEventHandler RemoteEvent;
// call the below code when th event should fire.
if (RemoteEvent != null)
RemoteEvent(this, new ClientEventArgs(e.MyClient));
}
然后要在服务器上设置我的频道,请执行以下操作:
Then To set my channels up on the server I do this:
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 5001;
TcpChannel channel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ConnectionManager),
ConnectionManager",
WellKnownObjectMode.Singleton);
然后在客户端上设置频道并订阅活动:
And On the client to set the channels up and subscribe to the event:
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel, false);
_monitorObject = (IMonitor)Activator.GetObject(
typeof(IMonitor),
"tcp://localhost:5001/ConnectionManager");
_monitorObject.RemoteEvent += _monitorObject_RemoteEvent;
任何人都可以解释这是怎么回事吗?
Can anyone explain where this is going wrong please?
例外:
未处理System.MissingMethodException HResult = -2146233069消息=未为此对象定义无参数构造函数.
来源= mscorlib
System.MissingMethodException was unhandled HResult=-2146233069 Message=No parameterless constructor defined for this object.
Source=mscorlib
推荐答案
要回答您的最后一个问题:使用Serializable
时,您需要一个没有参数的构造函数.因此,这肯定会失败:
To answer your last question: when using Serializable
you need a constructor without parameters. So this one would definitely fail:
[Serializable]
public class ClientEventArgs : EventArgs
{
public ClientEventArgs(Client _client)
{
MyClient = _client;
}
public Client MyClient { get; set; }
}
您需要添加一个无参数的构造函数:
You need to add a parameterless constructor:
[Serializable]
public class ClientEventArgs : EventArgs
{
public ClientEventArgs()
{ }
public ClientEventArgs(Client _client)
{
MyClient = _client;
}
public Client MyClient { get; set; }
}
这篇关于参数减去构造函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!