本文整理汇总了C#中SerializationContext.TraitsIndex方法的典型用法代码示例。如果您正苦于以下问题:C# SerializationContext.TraitsIndex方法的具体用法?C# SerializationContext.TraitsIndex怎么用?C# SerializationContext.TraitsIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SerializationContext
的用法示例。
在下文中一共展示了SerializationContext.TraitsIndex方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteDataContract
/// <summary>
/// Write an object.
/// </summary>
private static void WriteDataContract(XmlWriter writer, object graph, Type type, SerializationContext context, bool isDataContract)
{
var index = context.References.IndexOf(graph);
//Write object reference
if (index != -1)
{
var attributes = new Dictionary<string, string> { { AmfxContent.ReferenceId, index.ToString() } };
WriteEmptyElement(writer, AmfxContent.Reference, attributes);
return;
}
context.References.Add(new AmfReference { Reference = graph, AmfxType = AmfxContent.Object });
Dictionary<string, object> properties;
if (isDataContract)
{
var descriptor = context.GetDescriptor(type);
if (descriptor == null)
{
throw new SerializationException(
string.Format(
"Unable to resolve type '{0}'. Check if type was registered within the serializer.",
type.FullName));
}
var alias = descriptor.Alias;
var traitsindex = context.TraitsIndex(alias);
writer.WriteStartElement(AmfxContent.Object);
if (!string.IsNullOrEmpty(alias))
{
writer.WriteAttributeString(AmfxContent.ObjectType, alias);
if (traitsindex == -1)
{
var typeNameIndex = context.StringReferences.IndexOf(alias);
if (typeNameIndex == -1) context.StringReferences.Add(alias);
}
}
properties = DataContractHelper.GetContractProperties(graph, descriptor.PropertyMap, descriptor.FieldMap);
//Write traits by reference
if (context.AmfVersion == AmfVersion.Amf3 && traitsindex != -1)
{
var attributes = new Dictionary<string, string> { { AmfxContent.TraitsId, traitsindex.ToString() } };
WriteEmptyElement(writer, AmfxContent.Traits, attributes);
}
//Write traits
else
{
var traits = new AmfTypeTraits { TypeName = alias, ClassMembers = properties.Keys.ToArray() };
context.TraitsReferences.Add(traits);
writer.WriteStartElement(AmfxContent.Traits);
foreach (var propertyName in properties.Keys)
{
WriteElement(writer, AmfxContent.String, propertyName);
var memberNameIndex = context.StringReferences.IndexOf(propertyName);
if (memberNameIndex == -1) context.StringReferences.Add(propertyName);
}
writer.WriteEndElement(); //End of traits
}
}
else
{
var map = (IDictionary)graph;
properties = new Dictionary<string, object>();
foreach (var key in map.Keys)
properties[key.ToString()] = map[key];
writer.WriteStartElement(AmfxContent.Object);
writer.WriteStartElement(AmfxContent.Traits);
foreach (var propertyName in properties.Keys)
{
WriteElement(writer, AmfxContent.String, propertyName);
var memberNameIndex = context.StringReferences.IndexOf(propertyName);
if (memberNameIndex == -1) context.StringReferences.Add(propertyName);
}
writer.WriteEndElement(); //End of traits
}
foreach (var value in properties.Values)
Serialize(writer, value, context);
writer.WriteEndElement(); //End of object
//.........这里部分代码省略.........