草庐IT

c# - Marshal.SizeOf 在枚举上抛出 ArgumentException

考虑这段代码:publicenumMyEnum{V1,V2,V3}intsize=Marshal.SizeOf(typeof(MyEnum));它抛出异常:Anunhandledexceptionoftype'System.ArgumentException'occurredinTestConsole.exeAdditionalinformation:Type'TestConsole.Program+MyEnum'cannotbemarshaledasanunmanagedstructure;nomeaningfulsizeoroffsetcanbecomputed.虽然这段代码没有抛

c# - WinApi - GetLastError 与 Marshal.GetLastWin32Error

我测试了很多。但我没有发现这两个的缺点!但是请参阅已接受的答案。我读了here在托管代码中调用GetLastError是不安全的,因为框架可能会在内部“覆盖”最后一个错误。GetLastError我从来没有遇到过任何明显的问题,而且在我看来,.NETFramework足够聪明,不会覆盖它。因此,我有几个关于该主题的问题:在[DllImport("kernel32.dll",SetLastError=true)]中SetLastError属性是否使框架存储使用的错误代码编码(marshal).GetLastWin32Error()?是否有普通GetLastError无法给出正确结果的示例

C# 性能 - 使用不安全指针代替 IntPtr 和 Marshal

问题我正在将C应用程序移植到C#中。C应用程序从第3方DLL调用许多函数,因此我在C#中为这些函数编写了P/Invoke包装器。其中一些C函数分配我必须在C#应用程序中使用的数据,因此我使用了IntPtr,Marshal.PtrToStructure和Marshal.Copy将native数据(数组和结构)复制到托管变量中。不幸的是,事实证明C#应用程序比C版本慢得多。快速性能分析表明,上述基于编码的数据复制是瓶颈。我正在考虑通过重写C#代码以使用指针来加速它。由于我没有使用C#中的不安全代码和指针的经验,因此我需要有关以下方面的专家意见问题:使用unsafe代码和指针代替IntPtr

go - grpc:服务器无法编码响应:rpc错误:代码=内部desc = grpc:编码时出错:proto:Marshal用nil调用

理想情况下,以下RPC应该接收消息并编码为JSON。但是,遇到以下错误:ERROR:2018/08/1213:43:07grpc:serverfailedtoencoderesponse:rpcerror:code=Internaldesc=grpc:errorwhilemarshaling:proto:Marshalcalledwith无func(s*beaconServer)Transmit(ctxcontext.Context,batch*pb.Batch)(*pb.Empty,error){varempty*pb.EmptyvarmessageJSONbytes.Bufferm

json - 具有导出和未导出字段的 Golang Marshal/Unmarshal JSON

我见过很多编码/取消编码只有未导出字段的结构的方法。但是我怎样才能对混合字段执行此操作?给定一个结构:typeTeststruct{fieldAstring`json:"fieldA"`FieldBint`json:"fieldB"`FieldCstring`json:"fieldC"`}如何编写MarshalJSON/UnmarshalJSON函数,以便将fieldA与FieldB和FieldC一起传输?以下编译,但在我运行时溢出调用堆栈。我的猜测是我正在递归编码对象,但我不确定在编码时如何保留导出和未导出的字段。func(t*Test)MarshalJSON()([]byte,er

json.Marshal(struct) 返回 "{}"

typeTestObjectstruct{kindstring`json:"kind"`idstring`json:"id,omitempty"`namestring`json:"name"`emailstring`json:"email"`}funcTestCreateSingleItemResponse(t*testing.T){testObject:=new(TestObject)testObject.kind="TestObject"testObject.id="f73h5jf8"testObject.name="YuriGagarin"testObject.email="Yu

json - 在 Golang 中的 json.Marshal 之前添加键

我正在创建一个函数来获取对象数组并将其保存到Struct中。然后我想把它转换成JSON。funcGetCountry(msgstring)[]byte{varcountries[]*countryModel.Countrycountries=countryModel.GetAllCountry()jsResult,err:=json.Marshal(countries)iferr!=nil{logger.Error(err,"FailedonGetCountry")}returnjsResult}这是结构typeCountrystruct{Idint`json:"id"`Country

go - 如何使用json marshal获取 map 值

我需要将json字符串转换为map。这是我的go程序。packagemainimport("encoding/json""fmt")funcmain(){str:=`{"Bangalore_City":"35_Temperature","NewYork_City":"31_Temperature","Copenhagen_City":"29_Temperature","hobbies":{"name":"username"}}`varmmap[string]interface{}json.Unmarshal([]byte(str),&m)fmt.Println(m["hobbies"]

xml - Golang XML 编码(marshal)两个相同的属性

我被迫使用一些设计不佳的XML,我试图将此XML读入Go结构。下面是一些示例数据:cpix1isabirdofspeciesx2......我的问题是我可以读取节点或者因为它们都包含属性“word”:main.NLWordfield"Word"withtag"word,attr"conflictswithfield"Valsi"withtag"word,attr"我开始认为解码可能是错误的方法,因为理想情况下我会以不同的方式构建数据。我应该使用其他方法读取XML并手动构建数据结构吗?typeValsistruct{Wordstring`xml:"word,attr"`Typestrin

json - 自定义 Json 编码(marshal)处理

我有一个第三方jsonapi可以在go中使用。它有一些端点以键值对的形式返回数据。例如,这里是状态的json:{"result":{"0":"done","1":"incomplete","2":"completed",....}}所以正如你所看到的,它不是一个数组,而是一个对象。是否可以将这种json编码为对象数组,如typeStatusstruct{IdintStatusstring}不使用额外的结构,比如typeStatusReposnestruct{Resultmap[string]string`json:"result"`}和提取值的代码? 最佳答