当前位置: 首页>>代码示例>>C#>>正文


C# DataContractResolver类代码示例

本文整理汇总了C#中System.Runtime.Serialization.DataContractResolver的典型用法代码示例。如果您正苦于以下问题:C# DataContractResolver类的具体用法?C# DataContractResolver怎么用?C# DataContractResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


DataContractResolver类属于System.Runtime.Serialization命名空间,在下文中一共展示了DataContractResolver类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MyDataContractResolver

class MyDataContractResolver : DataContractResolver
{
    private Dictionary<string, XmlDictionaryString> dictionary = new Dictionary<string, XmlDictionaryString>();
    Assembly assembly;

    // Definition of the DataContractResolver
    public MyDataContractResolver(Assembly assembly)
    {
        this.assembly = assembly;
    }

    // Used at deserialization
    // Allows users to map xsi:type name to any Type 
    public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
    {
        XmlDictionaryString tName;
        XmlDictionaryString tNamespace;
        if (dictionary.TryGetValue(typeName, out tName) && dictionary.TryGetValue(typeNamespace, out tNamespace))
        {
            return this.assembly.GetType(tNamespace.Value + "." + tName.Value);
        }
        else
        {
            return null;
        }
    }
    // Used at serialization
    // Maps any Type to a new xsi:type representation
    public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
    {
        string name = type.Name;
        string namesp = type.Namespace;
        typeName = new XmlDictionaryString(XmlDictionary.Empty, name, 0); 
        typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, namesp, 0);
        if (!dictionary.ContainsKey(type.Name))
        {
            dictionary.Add(name, typeName);
        }
        if (!dictionary.ContainsKey(type.Namespace))
        {
            dictionary.Add(namesp, typeNamespace);
        }
        return true;
    }
}
开发者ID:.NET开发者,项目名称:System.Runtime.Serialization,代码行数:45,代码来源:DataContractResolver


注:本文中的System.Runtime.Serialization.DataContractResolver类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。