本文整理汇总了C#中System.Xml.Serialization.EnumMapping类的典型用法代码示例。如果您正苦于以下问题:C# EnumMapping类的具体用法?C# EnumMapping怎么用?C# EnumMapping使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EnumMapping类属于System.Xml.Serialization命名空间,在下文中一共展示了EnumMapping类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindChoiceEnumValue
private string FindChoiceEnumValue(ElementAccessor element, EnumMapping choiceMapping, bool useReflection)
{
string ident = null;
for (int i = 0; i < choiceMapping.Constants.Length; i++)
{
string xmlName = choiceMapping.Constants[i].XmlName;
if (element.Any && (element.Name.Length == 0))
{
if (!(xmlName == "##any:"))
{
continue;
}
if (useReflection)
{
ident = choiceMapping.Constants[i].Value.ToString(CultureInfo.InvariantCulture);
}
else
{
ident = choiceMapping.Constants[i].Name;
}
break;
}
int length = xmlName.LastIndexOf(':');
string str3 = (length < 0) ? choiceMapping.Namespace : xmlName.Substring(0, length);
string str4 = (length < 0) ? xmlName : xmlName.Substring(length + 1);
if ((element.Name == str4) && (((element.Form == XmlSchemaForm.Unqualified) && string.IsNullOrEmpty(str3)) || (element.Namespace == str3)))
{
if (useReflection)
{
ident = choiceMapping.Constants[i].Value.ToString(CultureInfo.InvariantCulture);
}
else
{
ident = choiceMapping.Constants[i].Name;
}
break;
}
}
if ((ident == null) || (ident.Length == 0))
{
if (element.Any && (element.Name.Length == 0))
{
throw new InvalidOperationException(Res.GetString("XmlChoiceMissingAnyValue", new object[] { choiceMapping.TypeDesc.FullName }));
}
throw new InvalidOperationException(Res.GetString("XmlChoiceMissingValue", new object[] { choiceMapping.TypeDesc.FullName, element.Namespace + ":" + element.Name, element.Name, element.Namespace }));
}
if (!useReflection)
{
CodeIdentifier.CheckValidIdentifier(ident);
}
return ident;
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:52,代码来源:XmlSerializationWriterCodeGen.cs
示例2: ImportEnumMapping
private EnumMapping ImportEnumMapping(EnumModel model, string ns, bool repeats)
{
XmlAttributes a = this.GetAttributes(model.Type, false);
string str = ns;
if ((a.XmlType != null) && (a.XmlType.Namespace != null))
{
str = a.XmlType.Namespace;
}
string name = IsAnonymousType(a, ns) ? null : this.XsdTypeName(model.Type, a, model.TypeDesc.Name);
name = XmlConvert.EncodeLocalName(name);
EnumMapping mapping = (EnumMapping) this.GetTypeMapping(name, str, model.TypeDesc, this.types, model.Type);
if (mapping == null)
{
mapping = new EnumMapping {
TypeDesc = model.TypeDesc,
TypeName = name,
Namespace = str,
IsFlags = model.Type.IsDefined(typeof(FlagsAttribute), false)
};
if (mapping.IsFlags && repeats)
{
throw new InvalidOperationException(Res.GetString("XmlIllegalAttributeFlagsArray", new object[] { model.TypeDesc.FullName }));
}
mapping.IsList = repeats;
mapping.IncludeInSchema = (a.XmlType == null) || a.XmlType.IncludeInSchema;
if (!mapping.IsAnonymousType)
{
this.types.Add(name, str, mapping);
}
else
{
this.anonymous[model.Type] = mapping;
}
ArrayList list = new ArrayList();
for (int i = 0; i < model.Constants.Length; i++)
{
ConstantMapping mapping2 = this.ImportConstantMapping(model.Constants[i]);
if (mapping2 != null)
{
list.Add(mapping2);
}
}
if (list.Count == 0)
{
throw new InvalidOperationException(Res.GetString("XmlNoSerializableMembers", new object[] { model.TypeDesc.FullName }));
}
mapping.Constants = (ConstantMapping[]) list.ToArray(typeof(ConstantMapping));
this.typeScope.AddTypeMapping(mapping);
}
return mapping;
}
示例3: FindChoiceEnumValue
string FindChoiceEnumValue(ElementAccessor element, EnumMapping choiceMapping, bool useReflection) {
string enumValue = null;
for (int i = 0; i < choiceMapping.Constants.Length; i++) {
string xmlName = choiceMapping.Constants[i].XmlName;
if (element.Any && element.Name.Length == 0) {
if (xmlName == "##any:") {
if (useReflection)
enumValue = choiceMapping.Constants[i].Value.ToString(CultureInfo.InvariantCulture);
else
enumValue = choiceMapping.Constants[i].Name;
break;
}
continue;
}
int colon = xmlName.LastIndexOf(':');
string choiceNs = colon < 0 ? choiceMapping.Namespace : xmlName.Substring(0, colon);
string choiceName = colon < 0 ? xmlName : xmlName.Substring(colon+1);
if (element.Name == choiceName) {
if ((element.Form == XmlSchemaForm.Unqualified && string.IsNullOrEmpty(choiceNs)) || element.Namespace == choiceNs) {
if (useReflection)
enumValue = choiceMapping.Constants[i].Value.ToString(CultureInfo.InvariantCulture);
else
enumValue = choiceMapping.Constants[i].Name;
break;
}
}
}
if (enumValue == null || enumValue.Length == 0) {
if (element.Any && element.Name.Length == 0) {
// Type {0} is missing enumeration value '##any' for XmlAnyElementAttribute.
throw new InvalidOperationException(Res.GetString(Res.XmlChoiceMissingAnyValue, choiceMapping.TypeDesc.FullName));
}
// Type {0} is missing value for '{1}'.
throw new InvalidOperationException(Res.GetString(Res.XmlChoiceMissingValue, choiceMapping.TypeDesc.FullName, element.Namespace + ":" + element.Name, element.Name, element.Namespace));
}
if(!useReflection)
CodeIdentifier.CheckValidIdentifier(enumValue);
return enumValue;
}
示例4: WriteEnumValue
void WriteEnumValue(EnumMapping mapping, string source) {
string methodName = ReferenceMapping(mapping);
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (methodName == null) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorMethod, mapping.TypeDesc.Name) + Environment.StackTrace);
#endif
Writer.Write(methodName);
Writer.Write("(");
Writer.Write(source);
Writer.Write(")");
}
示例5: ExportEnum
internal CodeTypeDeclaration ExportEnum(EnumMapping mapping, Type type) {
CodeTypeDeclaration codeClass = new CodeTypeDeclaration(mapping.TypeDesc.Name);
codeClass.Comments.Add(new CodeCommentStatement(Res.GetString(Res.XmlRemarks), true));
codeClass.IsEnum = true;
if (mapping.IsFlags && mapping.Constants.Length > 31) {
codeClass.BaseTypes.Add(new CodeTypeReference(typeof(long)));
}
codeClass.TypeAttributes |= TypeAttributes.Public;
CodeNamespace.Types.Add(codeClass);
for (int i = 0; i < mapping.Constants.Length; i++) {
ExportConstant(codeClass, mapping.Constants[i], type, mapping.IsFlags, 1L << i);
}
if (mapping.IsFlags) {
// Add [FlagsAttribute]
CodeAttributeDeclaration flags = new CodeAttributeDeclaration(typeof(FlagsAttribute).FullName);
codeClass.CustomAttributes.Add(flags);
}
CodeGenerator.ValidateIdentifiers(codeClass);
return codeClass;
}
示例6: WriteHashtable
string WriteHashtable(EnumMapping mapping, string typeName) {
CodeIdentifier.CheckValidIdentifier(typeName);
string propName = MakeUnique(mapping, typeName + "Values");
if (propName == null) return CodeIdentifier.GetCSharpName(typeName);
string memberName = MakeUnique(mapping, "_" + propName);
propName = CodeIdentifier.GetCSharpName(propName);
Writer.WriteLine();
Writer.Write(typeof(Hashtable).FullName);
Writer.Write(" ");
Writer.Write(memberName);
Writer.WriteLine(";");
Writer.WriteLine();
Writer.Write("internal ");
Writer.Write(typeof(Hashtable).FullName);
Writer.Write(" ");
Writer.Write(propName);
Writer.WriteLine(" {");
Writer.Indent++;
Writer.WriteLine("get {");
Writer.Indent++;
Writer.Write("if ((object)");
Writer.Write(memberName);
Writer.WriteLine(" == null) {");
Writer.Indent++;
Writer.Write(typeof(Hashtable).FullName);
Writer.Write(" h = new ");
Writer.Write(typeof(Hashtable).FullName);
Writer.WriteLine("();");
ConstantMapping[] constants = mapping.Constants;
for (int i = 0; i < constants.Length; i++) {
Writer.Write("h.Add(");
WriteQuotedCSharpString(constants[i].XmlName);
if (!mapping.TypeDesc.UseReflection){
Writer.Write(", (long)");
Writer.Write(mapping.TypeDesc.CSharpName);
Writer.Write("[email protected]");
CodeIdentifier.CheckValidIdentifier(constants[i].Name);
Writer.Write(constants[i].Name);
}
else{
Writer.Write(", ");
Writer.Write(constants[i].Value.ToString(CultureInfo.InvariantCulture)+"L");
}
Writer.WriteLine(");");
}
Writer.Write(memberName);
Writer.WriteLine(" = h;");
Writer.Indent--;
Writer.WriteLine("}");
Writer.Write("return ");
Writer.Write(memberName);
Writer.WriteLine(";");
Writer.Indent--;
Writer.WriteLine("}");
Writer.Indent--;
Writer.WriteLine("}");
return propName;
}
示例7: ImportEnumeratedDataType
TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, bool isList) {
TypeMapping mapping = (TypeMapping)ImportedMappings[dataType];
if (mapping != null)
return mapping;
XmlSchemaSimpleType sourceDataType = FindDataType(dataType.DerivedFrom);
TypeDesc sourceTypeDesc = Scope.GetTypeDesc(sourceDataType);
if (sourceTypeDesc != null && sourceTypeDesc != Scope.GetTypeDesc(typeof(string)))
return ImportPrimitiveDataType(dataType);
identifier = Accessor.UnescapeName(identifier);
string typeName = GenerateUniqueTypeName(identifier);
EnumMapping enumMapping = new EnumMapping();
enumMapping.IsReference = Schemas.IsReference(dataType);
enumMapping.TypeDesc = new TypeDesc(typeName, typeName, TypeKind.Enum, null, 0);
enumMapping.TypeName = identifier;
enumMapping.Namespace = typeNs;
enumMapping.IsFlags = isList;
CodeIdentifiers constants = new CodeIdentifiers();
if (!(dataType.Content is XmlSchemaSimpleTypeRestriction))
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidEnumContent, dataType.Content.GetType().Name, identifier));
XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)dataType.Content;
for (int i = 0; i < restriction.Facets.Count; i++) {
object facet = restriction.Facets[i];
if (!(facet is XmlSchemaEnumerationFacet)) continue;
XmlSchemaEnumerationFacet enumeration = (XmlSchemaEnumerationFacet)facet;
ConstantMapping constant = new ConstantMapping();
string constantName = CodeIdentifier.MakeValid(enumeration.Value);
constant.Name = constants.AddUnique(constantName, constant);
constant.XmlName = enumeration.Value;
constant.Value = i;
}
enumMapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping));
if (isList && enumMapping.Constants.Length > 63) {
// if we have 64+ flag constants we cannot map the type to long enum, we will use string mapping instead.
mapping = new PrimitiveMapping();
mapping.TypeDesc = Scope.GetTypeDesc(typeof(string));
mapping.TypeName = mapping.TypeDesc.DataType.Name;
ImportedMappings.Add(dataType, mapping);
return mapping;
}
ImportedMappings.Add(dataType, enumMapping);
Scope.AddTypeMapping(enumMapping);
return enumMapping;
}
示例8: ExportEnum
void ExportEnum(EnumMapping mapping) {
CodeTypeDeclaration codeClass = new CodeTypeDeclaration(mapping.TypeDesc.Name);
codeClass.IsEnum = true;
codeClass.TypeAttributes |= TypeAttributes.Public;
codeClass.Comments.Add(new CodeCommentStatement("<remarks/>", true));
codeNamespace.Types.Add(codeClass);
codeClass.CustomAttributes = new CodeAttributeDeclarationCollection();
AddTypeMetadata(codeClass.CustomAttributes, mapping.TypeName, mapping.Namespace, mapping.IncludeInSchema);
for (int i = 0; i < mapping.Constants.Length; i++) {
ExportConstant(codeClass, mapping.Constants[i]);
}
if (mapping.IsFlags) {
AddCustomAttribute(codeClass.CustomAttributes, typeof(FlagsAttribute), new CodeAttributeArgument[0]);
}
}
示例9: WriteEnumValue
private void WriteEnumValue(EnumMapping mapping, SourceInfo source, out Type returnType)
{
string methodName = ReferenceMapping(mapping);
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (methodName == null) throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorMethod, mapping.TypeDesc.Name));
#endif
// For enum, its write method (eg. Write1_Gender) could be called multiple times
// prior to its declaration.
MethodBuilder methodBuilder = EnsureMethodBuilder(typeBuilder,
methodName,
CodeGenerator.PrivateMethodAttributes,
typeof(string),
new Type[] { mapping.TypeDesc.Type });
ilg.Ldarg(0);
source.Load(mapping.TypeDesc.Type);
ilg.Call(methodBuilder);
returnType = typeof(string);
}
示例10: ExportEnum
internal CodeTypeDeclaration ExportEnum(EnumMapping mapping, Type type)
{
CodeTypeDeclaration declaration = new CodeTypeDeclaration(mapping.TypeDesc.Name);
declaration.Comments.Add(new CodeCommentStatement(Res.GetString("XmlRemarks"), true));
declaration.IsEnum = true;
if (mapping.IsFlags && (mapping.Constants.Length > 0x1f))
{
declaration.BaseTypes.Add(new CodeTypeReference(typeof(long)));
}
declaration.TypeAttributes |= TypeAttributes.Public;
this.CodeNamespace.Types.Add(declaration);
for (int i = 0; i < mapping.Constants.Length; i++)
{
ExportConstant(declaration, mapping.Constants[i], type, mapping.IsFlags, ((long) 1L) << i);
}
if (mapping.IsFlags)
{
CodeAttributeDeclaration declaration2 = new CodeAttributeDeclaration(typeof(FlagsAttribute).FullName);
declaration.CustomAttributes.Add(declaration2);
}
CodeGenerator.ValidateIdentifiers(declaration);
return declaration;
}
示例11: WriteEnumMethod
void WriteEnumMethod(EnumMapping mapping) {
string tableName = null;
if (mapping.IsFlags)
tableName = WriteHashtable(mapping, mapping.TypeDesc.Name);
string methodName = (string)methodNames[mapping];
writer.WriteLine();
string fullTypeName = CodeIdentifier.EscapeKeywords(mapping.TypeDesc.FullName);
if (mapping.IsSoap) {
writer.Write("object");
writer.Write(" ");
writer.Write(methodName);
writer.WriteLine("() {");
writer.Indent++;
writer.WriteLine("string s = Reader.ReadElementString();");
}
else {
writer.Write(fullTypeName);
writer.Write(" ");
writer.Write(methodName);
writer.WriteLine("(string s) {");
writer.Indent++;
}
ConstantMapping[] constants = mapping.Constants;
if (mapping.IsFlags) {
writer.Write("return (");
writer.Write(fullTypeName);
writer.Write(")ToEnum(s, ");
writer.Write(tableName);
writer.Write(", ");
WriteQuotedCSharpString(fullTypeName);
writer.WriteLine(");");
}
else {
writer.WriteLine("switch (s) {");
writer.Indent++;
for (int i = 0; i < constants.Length; i++) {
ConstantMapping c = constants[i];
CodeIdentifier.CheckValidIdentifier(c.Name);
writer.Write("case ");
WriteQuotedCSharpString(c.XmlName);
writer.Write(": return ");
writer.Write(fullTypeName);
writer.Write("[email protected]");
writer.Write(c.Name);
writer.WriteLine(";");
}
writer.Write("default: throw CreateUnknownConstantException(s, typeof(");
writer.Write(fullTypeName);
writer.WriteLine("));");
writer.Indent--;
writer.WriteLine("}");
}
writer.Indent--;
writer.WriteLine("}");
}
示例12: WriteHashtable
string WriteHashtable(EnumMapping mapping, string typeName) {
CodeIdentifier.CheckValidIdentifier(typeName);
string propName = MakeUnique(mapping, typeName + "Values");
if (propName == null) return CodeIdentifier.EscapeKeywords(typeName);
string memberName = MakeUnique(mapping, "_" + propName);
propName = CodeIdentifier.EscapeKeywords(propName);
writer.WriteLine();
writer.Write(typeof(Hashtable).FullName);
writer.Write(" ");
writer.Write(memberName);
writer.WriteLine(";");
writer.WriteLine();
writer.Write("internal ");
writer.Write(typeof(Hashtable).FullName);
writer.Write(" ");
writer.Write(propName);
writer.WriteLine(" {");
writer.Indent++;
writer.WriteLine("get {");
writer.Indent++;
writer.Write("if ((object)");
writer.Write(memberName);
writer.WriteLine(" == null) {");
writer.Indent++;
writer.Write(typeof(Hashtable).FullName);
writer.Write(" h = new ");
writer.Write(typeof(Hashtable).FullName);
writer.WriteLine("();");
ConstantMapping[] constants = mapping.Constants;
for (int i = 0; i < constants.Length; i++) {
writer.Write("h.Add(");
WriteQuotedCSharpString(constants[i].XmlName);
writer.Write(", (");
writer.Write(typeof(long).FullName);
writer.Write(")");
writer.Write(CodeIdentifier.EscapeKeywords(mapping.TypeDesc.FullName));
writer.Write("[email protected]");
CodeIdentifier.CheckValidIdentifier(constants[i].Name);
writer.Write(constants[i].Name);
writer.WriteLine(");");
}
writer.Write(memberName);
writer.WriteLine(" = h;");
writer.Indent--;
writer.WriteLine("}");
writer.Write("return ");
writer.Write(memberName);
writer.WriteLine(";");
writer.Indent--;
writer.WriteLine("}");
writer.Indent--;
writer.WriteLine("}");
return propName;
}
示例13: ImportEnumeratedDataType
private TypeMapping ImportEnumeratedDataType(XmlSchemaSimpleType dataType, string typeNs, string identifier, TypeFlags flags, bool isList)
{
TypeMapping mapping = (TypeMapping)ImportedMappings[dataType];
if (mapping != null)
return mapping;
XmlSchemaType sourceType = dataType;
while (!sourceType.DerivedFrom.IsEmpty)
{
sourceType = FindType(sourceType.DerivedFrom, TypeFlags.CanBeElementValue | TypeFlags.CanBeAttributeValue);
}
if (sourceType is XmlSchemaComplexType) return null;
TypeDesc sourceTypeDesc = Scope.GetTypeDesc((XmlSchemaSimpleType)sourceType);
if (sourceTypeDesc != null && sourceTypeDesc.FullName != typeof(string).FullName)
return ImportPrimitiveDataType(dataType, flags);
identifier = Accessor.UnescapeName(identifier);
string typeName = GenerateUniqueTypeName(identifier);
EnumMapping enumMapping = new EnumMapping();
enumMapping.IsReference = Schemas.IsReference(dataType);
enumMapping.TypeDesc = new TypeDesc(typeName, typeName, TypeKind.Enum, null, 0);
if (dataType.Name != null && dataType.Name.Length > 0)
enumMapping.TypeName = identifier;
enumMapping.Namespace = typeNs;
enumMapping.IsFlags = isList;
CodeIdentifiers constants = new CodeIdentifiers();
XmlSchemaSimpleTypeContent content = dataType.Content;
if (content is XmlSchemaSimpleTypeRestriction)
{
XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction)content;
for (int i = 0; i < restriction.Facets.Count; i++)
{
object facet = restriction.Facets[i];
if (!(facet is XmlSchemaEnumerationFacet)) continue;
XmlSchemaEnumerationFacet enumeration = (XmlSchemaEnumerationFacet)facet;
// validate the enumeration value
if (sourceTypeDesc != null && sourceTypeDesc.HasCustomFormatter)
{
XmlCustomFormatter.ToDefaultValue(enumeration.Value, sourceTypeDesc.FormatterName);
}
ConstantMapping constant = new ConstantMapping();
string constantName = CodeIdentifier.MakeValid(enumeration.Value);
constant.Name = constants.AddUnique(constantName, constant);
constant.XmlName = enumeration.Value;
constant.Value = i;
}
}
enumMapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping));
if (isList && enumMapping.Constants.Length > 63)
{
// if we have 64+ flag constants we cannot map the type to long enum, we will use string mapping instead.
mapping = GetDefaultMapping(TypeFlags.CanBeElementValue | TypeFlags.CanBeTextValue | TypeFlags.CanBeAttributeValue);
ImportedMappings.Add(dataType, mapping);
return mapping;
}
ImportedMappings.Add(dataType, enumMapping);
Scope.AddTypeMapping(enumMapping);
return enumMapping;
}
示例14: ExportEnumMapping
XmlQualifiedName ExportEnumMapping(EnumMapping mapping, string ns) {
XmlSchemaSimpleType dataType = (XmlSchemaSimpleType)types[mapping];
if (dataType == null) {
CheckForDuplicateType(mapping.TypeName, mapping.Namespace);
dataType = new XmlSchemaSimpleType();
dataType.Name = mapping.TypeName;
types.Add(mapping, dataType);
AddSchemaItem(dataType, mapping.Namespace, ns);
XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
restriction.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace);
for (int i = 0; i < mapping.Constants.Length; i++) {
ConstantMapping constant = mapping.Constants[i];
XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet();
enumeration.Value = constant.XmlName;
restriction.Facets.Add(enumeration);
}
if (!mapping.IsFlags) {
dataType.Content = restriction;
}
else {
XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
XmlSchemaSimpleType enumType = new XmlSchemaSimpleType();
enumType.Content = restriction;
list.ItemType = enumType;
dataType.Content = list;
}
}
else {
AddSchemaImport(mapping.Namespace, ns);
}
return new XmlQualifiedName(mapping.TypeName, mapping.Namespace);
}
示例15: CheckChoiceIdentifierMapping
private void CheckChoiceIdentifierMapping(EnumMapping choiceMapping)
{
System.Xml.Serialization.NameTable table = new System.Xml.Serialization.NameTable();
for (int i = 0; i < choiceMapping.Constants.Length; i++)
{
string xmlName = choiceMapping.Constants[i].XmlName;
int length = xmlName.LastIndexOf(':');
string name = (length < 0) ? xmlName : xmlName.Substring(length + 1);
string ns = (length < 0) ? "" : xmlName.Substring(0, length);
if (table[name, ns] != null)
{
throw new InvalidOperationException(Res.GetString("XmlChoiceIdDuplicate", new object[] { choiceMapping.TypeName, xmlName }));
}
table.Add(name, ns, choiceMapping.Constants[i]);
}
}