2016-11-24 20 views
1

System.Globalization.CultureInfo类的集合的高速缓存在我的上下文包装类类型,并没有合同可以推断出与.NET预定义类protobuf网

public Collection<System.Globalization.CultureInfo> Cultures 
{ 
    get 
    { 
     // Get the value from Redis cache 
    } 
    set 
    { 
     // Save the value into Redis cache 
    } 
} 

它可以通过访问MyContextWrapper.Current.Cultures

我收到以下错误,而与protobuf-net序列化“收藏文化”的价值:

类型未预期,且没有合同可以推断:System.Globalization.CultureInfo

enter image description here

我知道protobuf-net在类上需要[ProtoContract]和[ProtoMember]装饰,但这只适用于自定义用户定义的类。

我该如何去.NET预定义的类然后例如System.Globalization.CultureInfo在我的情况。

这甚至可能与protobuf网?

+0

你为什么要序列化文化信息? – Maarten

+0

我的回答对你有帮助吗?让我知道如果有什么我应该补充的。 – Measuring

回答

1

你可以去一个代理。在序列化Collection之前通知它的protobuf-net。尽管我现在只能使用内置文化,但您可以自行扩展它以添加附加数据以完全恢复文化。

到的CultureInfo转换成protobuf网支持的类型的替代品。

[ProtoContract] 
public class CultureInfoSurrogate 
{ 
    [ProtoMember(1)] 
    public int CultureId { get; set; } 

    public static implicit operator CultureInfoSurrogate(CultureInfo culture) 
    { 
     if (culture == null) return null; 
     var obj = new CultureInfoSurrogate(); 
     obj.CultureId = culture.LCID; 
     return obj; 
    } 

    public static implicit operator CultureInfo(CultureInfoSurrogate surrogate) 
    { 
     if (surrogate == null) return null; 
     return new CultureInfo(surrogate.CultureId); 
    } 
} 

将这个地方在程序的开始(你是序列化集合至少前):

RuntimeTypeModel.Default.Add(typeof(CultureInfo), false).SetSurrogate(typeof(CultureInfoSurrogate)); 

如果您还有其他问题,让我知道了意见。

相关问题