本文整理汇总了C#中System.Xml.Serialization.XmlMapping类的典型用法代码示例。如果您正苦于以下问题:C# XmlMapping类的具体用法?C# XmlMapping怎么用?C# XmlMapping使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XmlMapping类属于System.Xml.Serialization命名空间,在下文中一共展示了XmlMapping类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateGetSerializer
private void GenerateGetSerializer(Hashtable serializers, XmlMapping[] xmlMappings)
{
this.writer.Write("public override ");
this.writer.Write(typeof(XmlSerializer).FullName);
this.writer.Write(" GetSerializer(");
this.writer.Write(typeof(Type).FullName);
this.writer.WriteLine(" type) {");
this.writer.Indent++;
for (int i = 0; i < xmlMappings.Length; i++)
{
if (xmlMappings[i] is XmlTypeMapping)
{
Type type = xmlMappings[i].Accessor.Mapping.TypeDesc.Type;
if (((type != null) && (type.IsPublic || type.IsNestedPublic)) && ((!DynamicAssemblies.IsTypeDynamic(type) && !type.IsGenericType) && (!type.ContainsGenericParameters || !DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments()))))
{
this.writer.Write("if (type == typeof(");
this.writer.Write(CodeIdentifier.GetCSharpName(type));
this.writer.Write(")) return new ");
this.writer.Write((string) serializers[xmlMappings[i].Key]);
this.writer.WriteLine("();");
}
}
}
this.writer.WriteLine("return null;");
this.writer.Indent--;
this.writer.WriteLine("}");
}
示例2: TempAssembly
internal TempAssembly(XmlMapping[] xmlMappings, Assembly assembly, XmlSerializerImplementation contract)
{
this.assemblies = new Hashtable();
this.assembly = assembly;
this.InitAssemblyMethods(xmlMappings);
this.contract = contract;
this.pregeneratedAssmbly = true;
}
示例3: IsShallow
internal static bool IsShallow(XmlMapping[] mappings)
{
for (int i = 0; i < mappings.Length; i++)
{
if ((mappings[i] == null) || mappings[i].shallow)
{
return true;
}
}
return false;
}
示例4: TempAssembly
internal TempAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, string location, Evidence evidence) {
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
TempFileCollection tempFiles = new TempFileCollection(location);
parameters.TempFiles = tempFiles;
assembly = GenerateAssembly(xmlMappings, types, defaultNamespace, evidence, parameters, null, assemblies);
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (assembly == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Failed to generate XmlSerializer assembly, but did not throw"));
#endif
InitAssemblyMethods(xmlMappings);
}
示例5: GenerateElement
internal string GenerateElement(XmlMapping xmlMapping) {
if (!xmlMapping.IsWriteable)
return null;
if (!xmlMapping.GenerateSerializer)
throw new ArgumentException(Res.GetString(Res.XmlInternalError), "xmlMapping");
if (xmlMapping is XmlTypeMapping)
return GenerateTypeElement((XmlTypeMapping)xmlMapping);
else if (xmlMapping is XmlMembersMapping)
return GenerateMembersElement((XmlMembersMapping)xmlMapping);
else
throw new ArgumentException(Res.GetString(Res.XmlInternalError), "xmlMapping");
}
示例6: CanRead
internal bool CanRead(XmlMapping mapping, XmlReader xmlReader)
{
if (mapping == null)
{
return false;
}
if (mapping.Accessor.Any)
{
return true;
}
TempMethod method = this.methods[mapping.Key];
return xmlReader.IsStartElement(method.name, method.ns);
}
示例7: ReflectionXmlSerializationWriter
public ReflectionXmlSerializationWriter(XmlMapping xmlMapping, XmlWriter xmlWriter, XmlSerializerNamespaces namespaces, string encodingStyle, string id)
{
Init(xmlWriter, namespaces, encodingStyle, id, null);
if (!xmlMapping.IsWriteable || !xmlMapping.GenerateSerializer)
{
throw new ArgumentException(SR.Format(SR.XmlInternalError, "xmlMapping"));
}
if (xmlMapping is XmlTypeMapping || xmlMapping is XmlMembersMapping)
{
_mapping = xmlMapping;
}
else
{
throw new ArgumentException(SR.Format(SR.XmlInternalError, "xmlMapping"));
}
}
示例8: GetInitializers
public override object[] GetInitializers (LogicalMethodInfo[] methodInfos)
{
XmlReflectionImporter importer = new XmlReflectionImporter ();
XmlMapping[] sers = new XmlMapping [methodInfos.Length];
for (int n=0; n<sers.Length; n++)
{
LogicalMethodInfo metinfo = methodInfos[n];
if (metinfo.IsVoid)
sers[n] = null;
else
{
LogicalTypeInfo sti = TypeStubManager.GetLogicalTypeInfo (metinfo.DeclaringType);
object[] ats = methodInfos[n].ReturnTypeCustomAttributeProvider.GetCustomAttributes (typeof(XmlRootAttribute), true);
XmlRootAttribute root = ats.Length > 0 ? ats[0] as XmlRootAttribute : null;
sers[n] = importer.ImportTypeMapping (methodInfos[n].ReturnType, root, sti.GetWebServiceLiteralNamespace (sti.WebServiceNamespace));
}
}
return XmlSerializer.FromMappings (sers);
}
示例9: TempAssembly
internal TempAssembly(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, string location, Evidence evidence) {
bool containsSoapMapping = false;
for (int i = 0; i < xmlMappings.Length; i++) {
xmlMappings[i].CheckShallow();
if (xmlMappings[i].IsSoap) {
containsSoapMapping = true;
}
}
// We will make best effort to use RefEmit for assembly generation
bool fallbackToCSharpAssemblyGeneration = false;
if (!containsSoapMapping && !TempAssembly.UseLegacySerializerGeneration) {
try {
assembly = GenerateRefEmitAssembly(xmlMappings, types, defaultNamespace, evidence);
}
// Only catch and handle known failures with RefEmit
catch (CodeGeneratorConversionException) {
fallbackToCSharpAssemblyGeneration = true;
}
// Add other known exceptions here...
//
}
else {
fallbackToCSharpAssemblyGeneration = true;
}
if (fallbackToCSharpAssemblyGeneration) {
assembly = GenerateAssembly(xmlMappings, types, defaultNamespace, evidence, XmlSerializerCompilerParameters.Create(location), null, assemblies);
}
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (assembly == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Failed to generate XmlSerializer assembly, but did not throw"));
#endif
InitAssemblyMethods(xmlMappings);
}
示例10: GenerateTypedSerializer
internal string GenerateTypedSerializer(string readMethod, string writeMethod, XmlMapping mapping, CodeIdentifiers classes, string baseSerializer, string readerClass, string writerClass) {
string serializerName = CodeIdentifier.MakeValid(Accessor.UnescapeName(mapping.Accessor.Mapping.TypeDesc.Name));
serializerName = classes.AddUnique(serializerName + "Serializer", mapping);
writer.WriteLine();
writer.Write("public sealed class ");
writer.Write(CodeIdentifier.GetCSharpName(serializerName));
writer.Write(" : ");
writer.Write(baseSerializer);
writer.WriteLine(" {");
writer.Indent++;
writer.WriteLine();
writer.Write("public override ");
writer.Write(typeof(bool).FullName);
writer.Write(" CanDeserialize(");
writer.Write(typeof(XmlReader).FullName);
writer.WriteLine(" xmlReader) {");
writer.Indent++;
if (mapping.Accessor.Any) {
writer.WriteLine("return true;");
}
else {
writer.Write("return xmlReader.IsStartElement(");
WriteQuotedCSharpString(mapping.Accessor.Name);
writer.Write(", ");
WriteQuotedCSharpString(mapping.Accessor.Namespace);
writer.WriteLine(");");
}
writer.Indent--;
writer.WriteLine("}");
if (writeMethod != null) {
writer.WriteLine();
writer.Write("protected override void Serialize(object objectToSerialize, ");
writer.Write(typeof(XmlSerializationWriter).FullName);
writer.WriteLine(" writer) {");
writer.Indent++;
writer.Write("((");
writer.Write(writerClass);
writer.Write(")writer).");
writer.Write(writeMethod);
writer.Write("(");
if (mapping is XmlMembersMapping) {
writer.Write("(object[])");
}
writer.WriteLine("objectToSerialize);");
writer.Indent--;
writer.WriteLine("}");
}
if (readMethod != null) {
writer.WriteLine();
writer.Write("protected override object Deserialize(");
writer.Write(typeof(XmlSerializationReader).FullName);
writer.WriteLine(" reader) {");
writer.Indent++;
writer.Write("return ((");
writer.Write(readerClass);
writer.Write(")reader).");
writer.Write(readMethod);
writer.WriteLine("();");
writer.Indent--;
writer.WriteLine("}");
}
writer.Indent--;
writer.WriteLine("}");
return serializerName;
}
示例11: GenerateEnd
internal void GenerateEnd(string[] methods, XmlMapping[] xmlMappings, Type[] types) {
GenerateReferencedMethods();
GenerateInitCallbacksMethod();
foreach (CreateCollectionInfo c in createMethods.Values) {
WriteCreateCollectionMethod(c);
}
Writer.WriteLine();
foreach (string idName in idNames.Values) {
Writer.Write("string ");
Writer.Write(idName);
Writer.WriteLine(";");
}
Writer.WriteLine();
Writer.WriteLine("protected override void InitIDs() {");
Writer.Indent++;
foreach (string id in idNames.Keys) {
//
string idName = (string)idNames[id];
Writer.Write(idName);
Writer.Write(" = Reader.NameTable.Add(");
WriteQuotedCSharpString(id);
Writer.WriteLine(");");
}
Writer.Indent--;
Writer.WriteLine("}");
Writer.Indent--;
Writer.WriteLine("}");
}
示例12: GenerateGetSerializer
//GenerateGetSerializer(serializers, xmlMappings);
private void GenerateGetSerializer(Dictionary<string, string> serializers, XmlMapping[] xmlMappings, TypeBuilder serializerContractTypeBuilder)
{
ilg = new CodeGenerator(serializerContractTypeBuilder);
ilg.BeginMethod(
typeof(XmlSerializer),
"GetSerializer",
new Type[] { typeof(Type) },
new string[] { "type" },
CodeGenerator.PublicOverrideMethodAttributes);
for (int i = 0; i < xmlMappings.Length; i++)
{
if (xmlMappings[i] is XmlTypeMapping)
{
Type type = xmlMappings[i].Accessor.Mapping.TypeDesc.Type;
if (type == null)
continue;
if (!type.GetTypeInfo().IsPublic && !type.GetTypeInfo().IsNestedPublic)
continue;
// DDB172141: Wrong generated CS for serializer of List<string> type
if (type.GetTypeInfo().IsGenericType || type.GetTypeInfo().ContainsGenericParameters)
continue;
ilg.Ldarg("type");
ilg.Ldc(type);
ilg.If(Cmp.EqualTo);
{
ConstructorInfo ctor = CreatedTypes[(string)serializers[xmlMappings[i].Key]].GetConstructor(
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ilg.New(ctor);
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
}
ilg.EndIf();
}
}
ilg.Load(null);
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
ilg.MarkLabel(ilg.ReturnLabel);
ilg.Ldloc(ilg.ReturnLocal);
ilg.EndMethod();
}
示例13: GeneratePublicMethods
internal FieldBuilder GeneratePublicMethods(string privateName, string publicName, string[] methods, XmlMapping[] xmlMappings, TypeBuilder serializerContractTypeBuilder)
{
FieldBuilder fieldBuilder = GenerateHashtableGetBegin(privateName, publicName, serializerContractTypeBuilder);
if (methods != null && methods.Length != 0 && xmlMappings != null && xmlMappings.Length == methods.Length)
{
MethodInfo Hashtable_set_Item = typeof(Hashtable).GetMethod(
"set_Item",
new Type[] { typeof(Object), typeof(Object) }
);
for (int i = 0; i < methods.Length; i++)
{
if (methods[i] == null)
continue;
ilg.Ldloc(typeof(Hashtable), "_tmp");
ilg.Ldstr(GetCSharpString(xmlMappings[i].Key));
ilg.Ldstr(GetCSharpString(methods[i]));
ilg.Call(Hashtable_set_Item);
}
}
GenerateHashtableGetEnd(fieldBuilder);
return fieldBuilder;
}
示例14: SerializationCodeGenerator
public SerializationCodeGenerator (XmlMapping xmlMap, SerializerInfo config)
{
_xmlMaps = new XmlMapping [] {xmlMap};
_config = config;
}
示例15: GetFixupCallbackName
string GetFixupCallbackName (XmlMapping typeMap)
{
if (!_mapsToGenerate.Contains (typeMap)) _mapsToGenerate.Add (typeMap);
if (typeMap is XmlTypeMapping)
return GetUniqueName ("fc", typeMap, "FixupCallback_" + ((XmlTypeMapping)typeMap).XmlType);
else
return GetUniqueName ("fc", typeMap, "FixupCallback__Message");
}