当前位置: 首页 > 编程语言 > C#

Protobuf-net-Unabletocreateinstanceofabstractclassshare

时间:2023-04-11 11:23:37 C#

Protobuf-net:Unabletocreateinstanceofabstractclass我创建了一个具有值类型ProtoObject的通用“ProtoDictionary”。这是我的ProtoDictionary代码:publicclassProtoDictionary:Dictionary{publicvoidAdd(TKeykey,stringvalue){base.Add(key,newProtoObject<(value));}publicvoidAdd(TKeykey,Listvalue){base.Add(key,newProtoObject(value));}publicvoidAdd(TKeykey,Listvalue){base.Add(key,newProtoObject(value));}publicvoidAdd(TKeykey,Dictionaryvalue){base.Add(key,newProtoObject(value));}publicvoidAdd(TKeykey,Dictionaryvalue){base.Add(key,newProtoObject(value));}publicvoidAdd(TKeykey,Listvalue){base.Add(key,newProtoObject(value));}publicProtoDictionary(){//什么都不做}//注意:无论出于何种原因,如果没有此方法,此类将无法正确反序列化,即使//基类Dictionary具有SerializableAttribute。它受到保护,因此只有框架可以访问它。受保护的ProtoDictionary(SerializationInfo信息,StreamingContextcontext):base(info,context){}}对于ProtoObject:[ProtoContract][ProtoInclude(1,typeof(ProtoObject))][ProtoInclude(2,typeof(ProtoObject))][ProtoInclude(3,typeof(ProtoObject<列表>))][ProtoInclude(4,typeof(ProtoObject))][ProtoInclude(5,typeof(ProtoObject))][ProtoInclude(6,typeof(ProtoObject))][ProtoInclude(7、typeof(ProtoObject))][ProtoInclude(8,typeof(ProtoObject))][ProtoInclude(9,typeof(ProtoObject))][ProtoInclude(10,typeof(ProtoObject))][ProtoInclude(11,typeof(ProtoObject)))][ProtoInclude(12,typeof(ProtoObject))][ProtoInclude(13,typeof(ProtoObject))][Serializable]publicabstractclassProtoObject{publicstaticProtoObjectCreate(Tvalue){returnnewProtoObject(value);}publicobjectValue{get{returnValueImpl;}设置{ValueImpl=值;}}受保护的抽象对象ValueImpl{get;放;}protectedProtoObject(){}}[ProtoContract][Serializable]publicsealedclassProtoObject:ProtoObject{publicProtoObject(){}publicProtoObject(Tvalue){Value=value;}[ProtoMember(1)]publicnewTValue{get;放;}protectedoverrideobjectValueImpl{get{返回值;}设置{值=(T)值;}}publicoverridestringToString(){returnValue.ToString();}}问题是,当我尝试使用以下代码从SQL反序列化ProtoDictionary时:publicTDeserialize(IDataReaderreader,stringcolumnName){MemoryStreamstream=newMemoryStream();字节[]缓冲区=新字节[256];长启动索引=0;longbytesRead=reader.GetBytes(reader.GetOrdinal(columnName),startIndex,buffer,0,buffer.Length);while(bytesRead==buffer.Length){stream.Write(buffer,0,(int)bytesRead);startIndex+=bytesRead;bytesRead=reader.GetBytes(reader.GetOrdinal(columnName),startIndex,buffer,0,buffer.Length);}stream.Write(buffer,0,(int)bytesRead);stream.Seek(0,SeekOrigin.Begin);返回(T)Utilities.Deserialize(stre是);我收到错误“InvalidOperationException:无法创建抽象类的实例”我的StackTrace如下:在ctorWrapper()在ProtoBuf.ObjectFactory`1.Create()在c:protobuf-net_fixedtrunkprotobuf-netObjectFactory.cs:第82行在ProtoBuf.Serializer`1.Deserialize[TCreation](T&instance,SerializationContext上下文)c:protobuf-net_fixedtrunkprotobuf-netSerializerT.cs:第568行在ProtoBuf.Property.PropertyMessageString`4.DeserializeImpl(TSource来源,SerializationContext上下文)c:protobuf-net_fixedtrunkprotobuf-netPropertyPropertyMessageString.cs:第53行在ProtoBuf.Property.PropertyPairString`3.DeserializeImpl(TSourcesource,SerializationContextcontext)在c:protobuf-net_fixedtrunkprotobuf-netPropertyPropertyPairString.cs:第53行在ProtoBuf.Property.PropertyList`3.DeserializeImpl(TSourcesource,SerializationContextcontext,BooleancanSetValue)inc:protobuf-net_fixedtrunkprotobuf-netPropertyPropertyList.cs:ProtoBuf.Property.PropertyList`3.Deserialize(TSourcesource,Serializa中的第64行)tionContext上下文)在c:protobuf-net_fixedtrunkprotobuf-net属性PropertyList.cs:第52行在ProtoBuf.Serializer`1.Deserialize[TCreation](T&instance,SerializationContext上下文)c:protobuf-net_fixedtrunkprotobuf-netSerializerT.cs:第568行在ProtoBuf.Serializer`1.DeserializeChecked[TCreation](T&instance,SerializationContext源)在c:protobuf-net_fixedtrunkprotobuf-netSerializerT.cs:第400行在ProtoBuf.SerializerSimpleProxy`1.Deserialize(TValue&value,SerializationContext源))在c:protobuf-net_fixedtrunkprotobuf-netSerializerProxy.cs:第100行在ProtoBuf.Serializer.Deserialize[T](SerializationContextsource)在c:protobuf-net_fixedtrunkprotobuf-netSerializer.cs:第302行在ProtoBuf.Serializer.Deserialize[T](流源)c:protobuf-net_fixedtrunkprotobuf-netSerializer.cs:第289行在C:QAtrunkTestFrameworkCoreUtilities.cs中的Demand.TestFramework.Core.Utilities.Deserialize[T](MemoryStream流):第312行在C:QADemand.TestFramework.Core.Reports.CrawlerReport.Deserialize[T](IDataReaderreader,StringcolumnName)intrunkTestFrameworkCoreReportsCrawlerReport.cs:line145inC:QAtrunkTestFrameworkCoreReportsCrawlerReport.csDemand.TestFramework.Core.Reports中的第145行.CrawlerReport.FormatSuite(Int32parentSuiteId,GuidrunId):第70行Demand.TestFramework.Core.Reports.CrawlerReport.Format(GuidrunId)在C:QAtrunkTestFrameworkCoreReportsCrawlerReport.cs:第150行ServiceLauncher.Form1.btnStart_Click(Objectsender,EventArgse)inC:\QAtrunkTestFrameworkServiceLauncherForm1.cs:line24inSystem.Windows.Forms.Control.OnClick(EventArgse)inSystem.Windows.Forms.Button.OnClick(EventArgse)atSystem.Windows.Forms.Button.OnMouseUp(MouseEventArgsmevent)在System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtonsbutton,Int32click)在System.Windows.Forms.Control.WndProc(Message&m)在System.Windows。Sy处的Forms.ButtonBase.WndProc(Message&m)stem.Windows.Forms.Button.WndProc(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtrdwComponentID,Int32原因,Int32pvLoopData)在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)在System.Windows.Forms.Application.Run(FormmainForm)在C:QA主干TestFrameworkServiceLauncherProgram.cs中的ServiceLauncher.Program.Main():第16行在System.AppDomain._nExecuteAs在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.AppDomain.ExecuteAssembly(StringassemblyFile,EvidenceassemblySecurity,String[]args)在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback回调,对象状态,布尔值ignoreSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback回调,对象状态))我不确定我做错了什么任何帮助将不胜感激。谢谢,Dan这可能只是“v1”中的一个限制。我将其添加为“v2”的测试并且它通过了(我必须创建UrlStatus和TrafficEntry才能编译):publicvoidTestBasicRoundTrip(){varitem=newProtoDictionary();物品。添加(“abc”,“def”);物品。Add("ghi",newList{UrlStatus.A,UrlStatus.B});varclone=Serializer.DeepClone(item);Assert.AreEqual(2,clone.Keys.Count);对象o=克隆["abc"];Assert.AreEqual("def",clone["abc"].Value);varlist=(IList)clone["ghi"].Value;Assert.AreEqual(2,list.Count);Assert.AreEqual(UrlStatus.A,list[0]);Assert.AreEqual(UrlStatus.B,list[1]);使用“v1”,也许根本不抽象它?(解决方法,未修复)另外;不需要SerializationInfoctor;protobuf-net不使用它(虽然你可以通过提供这个方法并调用Merge在BinaryFormatter中实现protobuf-net)你需要分离ProtoObject和ProtoObject吗?protobuf-net似乎在ObjectFactory中创建了一个T类型的对象(以环形方式),我假设T将是抽象的ProtoObject。抱歉,我无法提供更多帮助-我现在没有太多时间进一步挖掘。以上就是C#学习教程的全部内容:Protobuf-net:Unabletocreateaninstanceofanabstractclass.如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。本文来自网络收藏,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢