简体   繁体   English

元帅/非元帅体现

[英]Marshal/unMarshal reflect.Type

I created map[string]interface{} and i want to pass multiple types through the map between 2 reset services. 我创建了map [string] interface {},我想在2个重置服务之间通过地图传递多种类型。

every time i marshal i get empty map in the field that should contain reflect.Type. 每次我编组时,我都会在应该包含reflect.Type的字段中得到空地图。

ServiceType:map[]

is it even possible? 有可能吗?

code for testing: (the KeyValuePair will represent a single value of the map) and the MessageService can be replaced with any type of course 测试代码:(KeyValuePair将代表地图的单个值),MessageService可以替换为任何类型的课程

data := GenericHandlers.KeyValuePair{Key:"ServiceType",Value:reflect.TypeOf(MessageService.MessageService{})}
Json , _ := json.Marshal(data)
resKvp := GenericHandlers.KeyValuePair{}
err := json.Unmarshal(Json,&resKvp)
if(err != nil){
     log.Println(err.Error())
}
fmt.Println(resKvp)


type KeyValuePair struct{
        Key string
        Value interface{}
}

Short answer: No, it's not possible. 简短的回答:不,这不可能。

Slightly longer answer: Since relfect.Type , as returned by TypeOf , is an interface type implemented by the unexported type rtype , none of whose fields are exported and whose method set does not contain the MarshalJSON/UnmarshalJSON methods, you cannot marshal or unmarshal a reflect.Type value as is. 稍长一点的答案:由于TypeOf返回的TypeOf是由未导出的类型rtype实现的接口类型 ,没有导出其字段且其方法集不包含MarshalJSON/UnmarshalJSON方法的类,因此您无法编组或MarshalJSON/UnmarshalJSON编组a reflect.Type值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM