System.TypeLoadException:使用MessagePack时拒绝访问

时间:2015-07-08 23:17:48

标签: c# messagepack

我正在尝试使用MessagePack来序列化我的对象,但每当我尝试运行序列化时,我都会收到Unhandled Exception错误。

这是我的代码:

CSCDP_TCPClient.cs

using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO;
using MsgPack;
using MsgPack.Serialization;

namespace CSCDPApp
{
    class CSCDP_TCPClient
    {
        public void Connect(string server, int portno, Script Script)  
        {

            var serializer = MessagePackSerializer.Get<Script>();
            var tempstream = new MemoryStream();
            serializer.Pack(tempstream, script);

            tempstream.Position = 0;

            var deserializedObject = serializer.Unpack(tempstream);
            Debug.WriteLine("Same object? {0}", Object.ReferenceEquals(script, deserializedObject));
        }
    }
}

Script.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSCDPApp
{
    class Script
    {
        public string cardID;
        public string time;
        public string device;
        public string command;

        public Script()
        {
            //Parameterless Constructor for serialization
        }

        public Script(string cardID, string time, string device, string command)
        {
            this.cardID = cardID;
            this.time = time;
            this.device = device;
            this.command = command;
        } 
    }
}

我只是把它称为主要的:

        CSCDP_TCPClient tcpclient = new CSCDP_TCPClient();
        Script tempscript = new Script("test1","test2","test3","test4");
        tcpclient.Connect("0.0.0.0",1,tempscript);

这是我得到的错误:

Unhandled Exception: System.TypeLoadException: Access is denied: 'MsgPack.Serial
ization.MessagePackSerializer`1[CSCDPApp.Script]'.
   at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, I
nt32 tk, ObjectHandleOnStack type)
   at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
   at System.Reflection.Emit.TypeBuilder.CreateType()
   at MsgPack.Serialization.EmittingSerializers.FieldBasedSerializerEmitter.Crea
teConstructor[T]()
   at MsgPack.Serialization.EmittingSerializers.SerializerEmitter.CreateInstance
[T](SerializationContext context)
   at MsgPack.Serialization.EmittingSerializers.ILEmittingSerializerBuilder`2.<>
c__DisplayClass5a.<CreateSerializerConstructor>b__59(SerializationContext contex
t)
   at MsgPack.Serialization.AbstractSerializers.SerializerBuilder`3.BuildSeriali
zerInstance(SerializationContext context)
   at MsgPack.Serialization.AutoMessagePackSerializer`1..ctor(SerializationConte
xt context, ISerializerBuilder`1 builder)
   at MsgPack.Serialization.MessagePackSerializer.CreateInternal[T](Serializatio
nContext context)
   at MsgPack.Serialization.SerializationContext.GetSerializer[T](Object provide
rParameter)
   at MsgPack.Serialization.MessagePackSerializer.Get[T](SerializationContext co
ntext, Object providerParameter)
   at CSCDPApp.CSCDP_TCPClient.Connect(String server, Int32 portno, Script scrip
t) in d:\TFS\MSVC 2013_CTSDev1\CommonComponents\sdnet\tools\CSCDPService\CSCDPAp
p\CSCDP_TCPClient.cs:line 21
   at CSCDPApp.Program.Main(String[] args) in d:\TFS\MSVC 2013_CTSDev1\CommonCom
ponents\sdnet\tools\CSCDPService\CSCDPApp\Program.cs:line 64

我的D:驱动器中有我的visual studio项目,我检查了权限是否正确。我将MessagePack.dll与所有已编译的可执行文件放在同一目录中,并将引用链接起来。有什么我可能会遗失的吗?

1 个答案:

答案 0 :(得分:2)

想出来!我的脚本课程没有被公开,这就是为什么序列化器无法找到它