添加EventHandler时出现SerializationException:
public interface IPlugin
{
...
event EventHandler<Event1Args> Event1;
event EventHandler<Event2Args> Event2;
}
[Serializable()]
public class Plugin : MarshalByRefObject, IPlugin
{
[field:NonSerialized]
public event EventHandler<Event1Args> Event1;
[field:NonSerialized]
public event EventHandler<Event2Args> Event2;
}
public class Test
{
public void Method()
{
IPlugin p = Plugins.GetPlugin("test");
try
{
p.Event1 += (sender,args) => Console.Writeline("Test Event");
}
catch (Exception e)
{
/* here comes the SerializationException: the type
TestNamespace.Test in Assembly "TestNamespace.Test,Version,..."
is not marked serializable.
*/
}
}
}
在运行时,IPlugin
是类Plugin
的一个实例,我可以在该类中添加[field:NonSerialized]
,但不能在界面中添加AppDomain
。但这并没有改变任何事情。
我没有英文VisualStudio所以也许异常消息不是100%正确。
我的原始异常消息是:
System.Runtime.Serialization.SerializationException:Der Typ 程序集“TestNamespace”中的“TestNamespace.Test +&lt;&gt; c__DisplayClass5_0”, Version = 2.1.6376.11505,Culture = neutral,PublicKeyToken = null“ist nicht als serialisierbar gekennzeichnet。
服务器堆栈跟踪:bei System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType 类型)bei System.Runtime.Serialization.FormatterServices.GetSerializableMembers(类型 type,StreamingContext context)bei System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() 北 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(对象 obj,ISurrogateSelector surrogateSelector,StreamingContext context, SerObjectInfoInit serObjectInfoInit,IFormatterConverter转换器, ObjectWriter objectWriter,SerializationBinder binder)bei System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo,NameInfo memberNameInfo,NameInfo typeNameInfo)bei System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(对象 graph,Header [] inHeaders,__BinaryWerer serWriter,Boolean fCheck)
北 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流 serializationStream,Object graph,Header [] headers,Boolean fCheck)
北 System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeMessageParts(ArrayList的 argsToSerialize)bei System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage..ctor(IMethodCallMessage mcm)bei System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage.SmuggleIfPossible(即时聊天 msg)bei System.Runtime.Remoting.Channels.CrossAppDomainSink.SyncProcessMessage(即时聊天 reqMsg)在[0]重新抛出异常:bei System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(即时聊天 reqMsg,IMessage retMsg)bei System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&安培; msgData,Int32类型)bei TestNamespace.Plugins.IPlugin.add_Event1(EventHandler`1 value)bei TestNamespace.Test.d__5.MoveNext()in C:\ xxx \ Test.cs:Zeile 14。
插件是在另一个AggregateCatalog
以及CompositionContainer
和[Serializable()]
中创建的,因此在调试时我看到IPlugin实例是一个远程代理实例。我不知道这是不是问题。
我已经尝试将Test类标记为{{1}},但这并未改变任何内容。