本文整理汇总了C#中CodeGenerator.EndMethod方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenerator.EndMethod方法的具体用法?C# CodeGenerator.EndMethod怎么用?C# CodeGenerator.EndMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGenerator
的用法示例。
在下文中一共展示了CodeGenerator.EndMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateCreateXmlSerializableDelegate
internal System.Runtime.Serialization.CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate()
{
Type underlyingType = base.UnderlyingType;
CodeGenerator generator = new CodeGenerator();
bool allowPrivateMemberAccess = this.RequiresMemberAccessForCreate(null);
try
{
generator.BeginMethod("Create" + DataContract.GetClrTypeFullName(underlyingType), typeof(System.Runtime.Serialization.CreateXmlSerializableDelegate), allowPrivateMemberAccess);
}
catch (SecurityException exception)
{
if (!allowPrivateMemberAccess || !exception.PermissionType.Equals(typeof(ReflectionPermission)))
{
throw;
}
this.RequiresMemberAccessForCreate(exception);
}
if (underlyingType.IsValueType)
{
LocalBuilder localBuilder = generator.DeclareLocal(underlyingType, underlyingType.Name + "Value");
generator.Ldloca(localBuilder);
generator.InitObj(underlyingType);
generator.Ldloc(localBuilder);
}
else
{
generator.New(this.GetConstructor());
}
generator.ConvertValue(base.UnderlyingType, Globals.TypeOfIXmlSerializable);
generator.Ret();
return (System.Runtime.Serialization.CreateXmlSerializableDelegate) generator.EndMethod();
}
示例2: GenerateMembersElement
//.........这里部分代码省略.........
ilg.MarkLabel(labelTrue);
ilg.Ldc(true);
ilg.MarkLabel(labelEnd);
ilg.If();
}
WriteMember(source, member.Attribute, member.TypeDesc, "p");
if (specifiedSource != null)
{
ilg.EndIf();
}
ilg.EndIf();
}
}
}
for (int i = 0; i < mapping.Members.Length; i++)
{
MemberMapping member = mapping.Members[i];
if (member.Xmlns != null)
continue;
if (member.Ignore)
continue;
SourceInfo specifiedSource = null;
int specifiedPosition = 0;
if (member.CheckSpecified != SpecifiedAccessor.None)
{
string memberNameSpecified = member.Name + "Specified";
for (int j = 0; j < mapping.Members.Length; j++)
{
if (mapping.Members[j].Name == memberNameSpecified)
{
specifiedSource = new SourceInfo("((bool)p[" + j.ToString(CultureInfo.InvariantCulture) + "])", null, null, typeof(bool), ilg);
specifiedPosition = j;
break;
}
}
}
ilg.Ldloc(pLengthLoc);
ilg.Ldc(i);
ilg.If(Cmp.GreaterThan);
if (specifiedSource != null)
{
Label labelTrue = ilg.DefineLabel();
Label labelEnd = ilg.DefineLabel();
ilg.Ldloc(pLengthLoc);
ilg.Ldc(specifiedPosition);
ilg.Ble(labelTrue);
specifiedSource.Load(typeof(bool));
ilg.Br_S(labelEnd);
ilg.MarkLabel(labelTrue);
ilg.Ldc(true);
ilg.MarkLabel(labelEnd);
ilg.If();
}
string source = "p[" + i.ToString(CultureInfo.InvariantCulture) + "]";
string enumSource = null;
if (member.ChoiceIdentifier != null)
{
for (int j = 0; j < mapping.Members.Length; j++)
{
if (mapping.Members[j].Name == member.ChoiceIdentifier.MemberName)
{
enumSource = "((" + mapping.Members[j].TypeDesc.CSharpName + ")p[" + j.ToString(CultureInfo.InvariantCulture) + "]" + ")";
break;
}
}
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (enumSource == null) throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "Can not find " + member.ChoiceIdentifier.MemberName + " in the members mapping."));
#endif
}
// override writeAccessors choice when we've written a wrapper element
WriteMember(new SourceInfo(source, source, null, null, ilg), enumSource, member.ElementsSortedByDerivation, member.Text, member.ChoiceIdentifier, member.TypeDesc, writeAccessors || hasWrapperElement);
if (specifiedSource != null)
{
ilg.EndIf();
}
ilg.EndIf();
}
if (hasWrapperElement)
{
WriteEndElement();
}
ilg.EndMethod();
return methodName;
}
示例3: GenerateGetOnlyCollectionReader
public XmlFormatGetOnlyCollectionReaderDelegate GenerateGetOnlyCollectionReader(CollectionDataContract collectionContract)
{
ilg = GenerateCollectionReaderHelper(collectionContract, true /*isGetOnlyCollection*/);
ReadGetOnlyCollection(collectionContract);
return (XmlFormatGetOnlyCollectionReaderDelegate)ilg.EndMethod();
}
示例4: GenerateClassReader
public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract)
{
ilg = new CodeGenerator();
bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null);
try
{
ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
{
classContract.RequiresMemberAccessForRead(securityException);
}
else
{
throw;
}
}
InitArgs();
DemandSerializationFormatterPermission(classContract);
DemandMemberAccessPermission(memberAccessFlag);
CreateObject(classContract);
ilg.Call(contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, objectLocal);
InvokeOnDeserializing(classContract);
LocalBuilder objectId = null;
if (HasFactoryMethod(classContract))
{
objectId = ilg.DeclareLocal(Globals.TypeOfString, "objectIdRead");
ilg.Call(contextArg, XmlFormatGeneratorStatics.GetObjectIdMethod);
ilg.Stloc(objectId);
}
if (classContract.IsISerializable)
ReadISerializable(classContract);
else
ReadClass(classContract);
bool isFactoryType = InvokeFactoryMethod(classContract, objectId);
if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType))
ilg.Call(objectLocal, XmlFormatGeneratorStatics.OnDeserializationMethod, null);
InvokeOnDeserialized(classContract);
if (objectId == null || !isFactoryType)
{
ilg.Load(objectLocal);
// Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization.
// DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation
// on DateTimeOffset; which does not work in partial trust.
if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter)
{
ilg.ConvertValue(objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter);
ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod);
ilg.ConvertValue(Globals.TypeOfDateTimeOffset, ilg.CurrentMethod.ReturnType);
}
else
{
ilg.ConvertValue(objectLocal.LocalType, ilg.CurrentMethod.ReturnType);
}
}
return (XmlFormatClassReaderDelegate)ilg.EndMethod();
}
示例5: GenerateSerializerContract
internal void GenerateSerializerContract(string className, XmlMapping[] xmlMappings, Type[] types, string readerType, string[] readMethods, string writerType, string[] writerMethods, Dictionary<string, string> serializers)
{
TypeBuilder serializerContractTypeBuilder = CodeGenerator.CreateTypeBuilder(
_moduleBuilder,
"XmlSerializerContract",
TypeAttributes.Public | TypeAttributes.BeforeFieldInit,
typeof(XmlSerializerImplementation),
Array.Empty<Type>()
);
ilg = new CodeGenerator(serializerContractTypeBuilder);
PropertyBuilder propertyBuilder = serializerContractTypeBuilder.DefineProperty(
"Reader",
PropertyAttributes.None,
typeof(XmlSerializationReader),
null, null, null, null, null);
ilg.BeginMethod(
typeof(XmlSerializationReader),
"get_Reader",
Array.Empty<Type>(),
Array.Empty<string>(),
CodeGenerator.PublicOverrideMethodAttributes | MethodAttributes.SpecialName);
propertyBuilder.SetGetMethod(ilg.MethodBuilder);
ConstructorInfo ctor = CreatedTypes[readerType].GetConstructor(
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ilg.New(ctor);
ilg.EndMethod();
ilg = new CodeGenerator(serializerContractTypeBuilder);
propertyBuilder = serializerContractTypeBuilder.DefineProperty(
"Writer",
PropertyAttributes.None,
typeof(XmlSerializationWriter),
null, null, null, null, null);
ilg.BeginMethod(
typeof(XmlSerializationWriter),
"get_Writer",
Array.Empty<Type>(),
Array.Empty<string>(),
CodeGenerator.PublicOverrideMethodAttributes | MethodAttributes.SpecialName);
propertyBuilder.SetGetMethod(ilg.MethodBuilder);
ctor = CreatedTypes[writerType].GetConstructor(
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ilg.New(ctor);
ilg.EndMethod();
FieldBuilder readMethodsField = GeneratePublicMethods("readMethods", "ReadMethods", readMethods, xmlMappings, serializerContractTypeBuilder);
FieldBuilder writeMethodsField = GeneratePublicMethods("writeMethods", "WriteMethods", writerMethods, xmlMappings, serializerContractTypeBuilder);
FieldBuilder typedSerializersField = GenerateTypedSerializers(serializers, serializerContractTypeBuilder);
GenerateSupportedTypes(types, serializerContractTypeBuilder);
GenerateGetSerializer(serializers, xmlMappings, serializerContractTypeBuilder);
// Default ctor
ConstructorInfo baseCtor = typeof(XmlSerializerImplementation).GetConstructor(
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ilg = new CodeGenerator(serializerContractTypeBuilder);
ilg.BeginMethod(
typeof(void),
".ctor",
Array.Empty<Type>(),
Array.Empty<string>(),
CodeGenerator.PublicMethodAttributes | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName
);
ilg.Ldarg(0);
ilg.Load(null);
ilg.StoreMember(readMethodsField);
ilg.Ldarg(0);
ilg.Load(null);
ilg.StoreMember(writeMethodsField);
ilg.Ldarg(0);
ilg.Load(null);
ilg.StoreMember(typedSerializersField);
ilg.Ldarg(0);
ilg.Call(baseCtor);
ilg.EndMethod();
// Instantiate type
Type serializerContractType = serializerContractTypeBuilder.CreateTypeInfo().AsType();
CreatedTypes.Add(serializerContractType.Name, serializerContractType);
}
示例6: 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);
TypeBuilder typedSerializerTypeBuilder = CodeGenerator.CreateTypeBuilder(
_moduleBuilder,
CodeIdentifier.GetCSharpName(serializerName),
TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
CreatedTypes[baseSerializer],
Array.Empty<Type>()
);
ilg = new CodeGenerator(typedSerializerTypeBuilder);
ilg.BeginMethod(
typeof(Boolean),
"CanDeserialize",
new Type[] { typeof(XmlReader) },
new string[] { "xmlReader" },
CodeGenerator.PublicOverrideMethodAttributes
);
if (mapping.Accessor.Any)
{
ilg.Ldc(true);
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
}
else
{
MethodInfo XmlReader_IsStartElement = typeof(XmlReader).GetMethod(
"IsStartElement",
CodeGenerator.InstanceBindingFlags,
new Type[] { typeof(String), typeof(String) }
);
ilg.Ldarg(ilg.GetArg("xmlReader"));
ilg.Ldstr(GetCSharpString(mapping.Accessor.Name));
ilg.Ldstr(GetCSharpString(mapping.Accessor.Namespace));
ilg.Call(XmlReader_IsStartElement);
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
}
ilg.MarkLabel(ilg.ReturnLabel);
ilg.Ldloc(ilg.ReturnLocal);
ilg.EndMethod();
if (writeMethod != null)
{
ilg = new CodeGenerator(typedSerializerTypeBuilder);
ilg.BeginMethod(
typeof(void),
"Serialize",
new Type[] { typeof(object), typeof(XmlSerializationWriter) },
new string[] { "objectToSerialize", "writer" },
CodeGenerator.ProtectedOverrideMethodAttributes);
MethodInfo writerType_writeMethod = CreatedTypes[writerClass].GetMethod(
writeMethod,
CodeGenerator.InstanceBindingFlags,
new Type[] { (mapping is XmlMembersMapping) ? typeof(object[]) : typeof(object) }
);
ilg.Ldarg("writer");
ilg.Castclass(CreatedTypes[writerClass]);
ilg.Ldarg("objectToSerialize");
if (mapping is XmlMembersMapping)
{
ilg.ConvertValue(typeof(object), typeof(object[]));
}
ilg.Call(writerType_writeMethod);
ilg.EndMethod();
}
if (readMethod != null)
{
ilg = new CodeGenerator(typedSerializerTypeBuilder);
ilg.BeginMethod(
typeof(object),
"Deserialize",
new Type[] { typeof(XmlSerializationReader) },
new string[] { "reader" },
CodeGenerator.ProtectedOverrideMethodAttributes);
MethodInfo readerType_readMethod = CreatedTypes[readerClass].GetMethod(
readMethod,
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ilg.Ldarg("reader");
ilg.Castclass(CreatedTypes[readerClass]);
ilg.Call(readerType_readMethod);
ilg.EndMethod();
}
typedSerializerTypeBuilder.DefineDefaultConstructor(CodeGenerator.PublicMethodAttributes);
Type typedSerializerType = typedSerializerTypeBuilder.CreateTypeInfo().AsType();
CreatedTypes.Add(typedSerializerType.Name, typedSerializerType);
return typedSerializerType.Name;
}
示例7: GenerateSupportedTypes
internal void GenerateSupportedTypes(Type[] types, TypeBuilder serializerContractTypeBuilder)
{
ilg = new CodeGenerator(serializerContractTypeBuilder);
ilg.BeginMethod(
typeof(bool),
"CanSerialize",
new Type[] { typeof(Type) },
new string[] { "type" },
CodeGenerator.PublicOverrideMethodAttributes);
var uniqueTypes = new HashSet<Type>();
for (int i = 0; i < types.Length; i++)
{
Type type = types[i];
if (type == null)
continue;
if (!type.GetTypeInfo().IsPublic && !type.GetTypeInfo().IsNestedPublic)
continue;
if (!uniqueTypes.Add(type))
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);
{
ilg.Ldc(true);
ilg.GotoMethodEnd();
}
ilg.EndIf();
}
ilg.Ldc(false);
ilg.GotoMethodEnd();
ilg.EndMethod();
}
示例8: GenerateClassReader
public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract)
{
_ilg = new CodeGenerator();
bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null);
try
{
_ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag)
{
classContract.RequiresMemberAccessForRead(securityException);
}
else
{
throw;
}
}
InitArgs();
CreateObject(classContract);
_ilg.Call(_contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, _objectLocal);
InvokeOnDeserializing(classContract);
LocalBuilder objectId = null;
ReadClass(classContract);
InvokeOnDeserialized(classContract);
if (objectId == null)
{
_ilg.Load(_objectLocal);
// Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization.
// DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation
// on DateTimeOffset; which does not work in partial trust.
if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter)
{
_ilg.ConvertValue(_objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter);
_ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod);
_ilg.ConvertValue(Globals.TypeOfDateTimeOffset, _ilg.CurrentMethod.ReturnType);
}
//Copy the KeyValuePairAdapter<K,T> to a KeyValuePair<K,T>.
else if (classContract.IsKeyValuePairAdapter)
{
_ilg.Call(classContract.GetKeyValuePairMethodInfo);
_ilg.ConvertValue(Globals.TypeOfKeyValuePair.MakeGenericType(classContract.KeyValuePairGenericArguments), _ilg.CurrentMethod.ReturnType);
}
else
{
_ilg.ConvertValue(_objectLocal.LocalType, _ilg.CurrentMethod.ReturnType);
}
}
return (XmlFormatClassReaderDelegate)_ilg.EndMethod();
}
示例9: WriteNullableMethod
void WriteNullableMethod(NullableMapping nullableMapping) {
string methodName = (string)MethodNames[nullableMapping];
ilg = new CodeGenerator(this.typeBuilder);
ilg.BeginMethod(
nullableMapping.TypeDesc.Type,
GetMethodBuilder(methodName),
new Type[] { typeof(Boolean) },
new string[] { "checkType" },
CodeGenerator.PrivateMethodAttributes);
LocalBuilder oLoc = ilg.DeclareLocal(nullableMapping.TypeDesc.Type, "o");
ilg.LoadAddress(oLoc);
ilg.InitObj(nullableMapping.TypeDesc.Type);
MethodInfo XmlSerializationReader_ReadNull = typeof(XmlSerializationReader).GetMethod(
"ReadNull",
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null);
ilg.Ldarg(0);
ilg.Call(XmlSerializationReader_ReadNull);
ilg.If();
{
ilg.Ldloc(oLoc);
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
}
ilg.EndIf();
ElementAccessor element = new ElementAccessor();
element.Mapping = nullableMapping.BaseMapping;
element.Any = false;
element.IsNullable = nullableMapping.BaseMapping.TypeDesc.IsNullable;
WriteElement("o", null, null, element, null, null, false, false, -1, -1);
ilg.Ldloc(oLoc);
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
ilg.MarkLabel(ilg.ReturnLabel);
ilg.Ldloc(ilg.ReturnLocal);
ilg.EndMethod();
}
示例10: WriteEnumMethod
//.........这里部分代码省略.........
argTypes.Add(typeof(string));
argNames.Add("s");
ilg = new CodeGenerator(this.typeBuilder);
ilg.BeginMethod(
returnType,
GetMethodBuilder(methodName),
argTypes.ToArray(),
argNames.ToArray(),
CodeGenerator.PrivateMethodAttributes);
ConstantMapping[] constants = mapping.Constants;
if (mapping.IsFlags) {
{
MethodInfo XmlSerializationReader_ToEnum = typeof(XmlSerializationReader).GetMethod(
"ToEnum",
CodeGenerator.StaticBindingFlags,
null,
new Type[] { typeof(String), typeof(Hashtable), typeof(String) },
null
);
ilg.Ldarg("s");
ilg.Ldarg(0);
Debug.Assert(get_TableName != null);
ilg.Call(get_TableName);
ilg.Ldstr(fullTypeName);
ilg.Call(XmlSerializationReader_ToEnum);
// XmlSerializationReader_ToEnum return long!
if (underlyingType != typeof(long)) {
ilg.ConvertValue(typeof(long), underlyingType);
}
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
}
}
else {
List<Label> caseLabels = new List<Label>();
List<object> retValues = new List<object>();
Label defaultLabel = ilg.DefineLabel();
Label endSwitchLabel = ilg.DefineLabel();
// This local is necessary; otherwise, it becomes if/else
LocalBuilder localTmp = ilg.GetTempLocal(typeof(string));
ilg.Ldarg("s");
ilg.Stloc(localTmp);
ilg.Ldloc(localTmp);
ilg.Brfalse(defaultLabel);
Hashtable cases = new Hashtable();
for (int i = 0; i < constants.Length; i++) {
ConstantMapping c = constants[i];
CodeIdentifier.CheckValidIdentifier(c.Name);
if (cases[c.XmlName] == null) {
cases[c.XmlName] = c.XmlName;
Label caseLabel = ilg.DefineLabel();
ilg.Ldloc(localTmp);
ilg.Ldstr(c.XmlName);
MethodInfo String_op_Equality = typeof(string).GetMethod(
"op_Equality",
CodeGenerator.StaticBindingFlags,
null,
new Type[] { typeof(string), typeof(string) },
null
);
ilg.Call(String_op_Equality);
ilg.Brtrue(caseLabel);
caseLabels.Add(caseLabel);
retValues.Add(Enum.ToObject(mapping.TypeDesc.Type, c.Value));
}
}
ilg.Br(defaultLabel);
// Case bodies
for (int i = 0; i < caseLabels.Count; i++) {
ilg.MarkLabel(caseLabels[i]);
ilg.Ldc(retValues[i]);
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
}
MethodInfo XmlSerializationReader_CreateUnknownConstantException = typeof(XmlSerializationReader).GetMethod(
"CreateUnknownConstantException",
CodeGenerator.InstanceBindingFlags,
null,
new Type[] { typeof(string), typeof(Type) },
null
);
// Default body
ilg.MarkLabel(defaultLabel);
ilg.Ldarg(0);
ilg.Ldarg("s");
// typeof(..)
ilg.Ldc(mapping.TypeDesc.Type);
ilg.Call(XmlSerializationReader_CreateUnknownConstantException);
ilg.Throw();
// End switch
ilg.MarkLabel(endSwitchLabel);
}
ilg.MarkLabel(ilg.ReturnLabel);
ilg.Ldloc(ilg.ReturnLocal);
ilg.EndMethod();
}
示例11: GenerateClassWriter
internal XmlFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract)
{
if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
{
return new ReflectionXmlFormatWriter().ReflectionWriteClass;
}
#if NET_NATIVE
else if (DataContractSerializer.Option == SerializationOption.ReflectionAsBackup)
{
return new ReflectionXmlFormatWriter().ReflectionWriteClass;
}
#endif
else
{
#if USE_REFEMIT || NET_NATIVE
throw new InvalidOperationException("Cannot generate class writer");
#else
_ilg = new CodeGenerator();
bool memberAccessFlag = classContract.RequiresMemberAccessForWrite(null);
try
{
_ilg.BeginMethod("Write" + classContract.StableName.Name + "ToXml", Globals.TypeOfXmlFormatClassWriterDelegate, memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag)
{
classContract.RequiresMemberAccessForWrite(securityException);
}
else
{
throw;
}
}
InitArgs(classContract.UnderlyingType);
WriteClass(classContract);
return (XmlFormatClassWriterDelegate)_ilg.EndMethod();
#endif
}
}
示例12: WriteStructMethod
//.........这里部分代码省略.........
System.Diagnostics.Debug.Assert(xmlnsSource.StartsWith("[email protected]", StringComparison.Ordinal));
ILGenLoad(xmlnsSource);
}
MethodInfo XmlSerializationWriter_WriteStartElement = typeof(XmlSerializationWriter).GetMethod(
"WriteStartElement",
CodeGenerator.InstanceBindingFlags,
new Type[] { typeof(String), typeof(String), typeof(Object), typeof(Boolean), typeof(XmlSerializerNamespaces) }
);
ilg.Call(XmlSerializationWriter_WriteStartElement);
if (!mapping.TypeDesc.IsRoot)
{
ilg.If(ilg.GetArg("needType"), Cmp.EqualTo, true);
{
MethodInfo XmlSerializationWriter_WriteXsiType = typeof(XmlSerializationWriter).GetMethod(
"WriteXsiType",
CodeGenerator.InstanceBindingFlags,
new Type[] { typeof(String), typeof(String) }
);
ilg.Ldarg(0);
ilg.Ldstr(GetCSharpString(mapping.TypeName));
ilg.Ldstr(GetCSharpString(mapping.Namespace));
ilg.Call(XmlSerializationWriter_WriteXsiType);
}
ilg.EndIf();
}
for (int i = 0; i < members.Length; i++)
{
MemberMapping m = members[i];
if (m.Attribute != null)
{
CodeIdentifier.CheckValidIdentifier(m.Name);
if (m.CheckShouldPersist)
{
ilg.LdargAddress(oArg);
ilg.Call(m.CheckShouldPersistMethodInfo);
ilg.If();
}
if (m.CheckSpecified != SpecifiedAccessor.None)
{
string memberGet = RaCodeGen.GetStringForMember("o", m.Name + "Specified", mapping.TypeDesc);
ILGenLoad(memberGet);
ilg.If();
}
WriteMember(RaCodeGen.GetSourceForMember("o", m, mapping.TypeDesc, ilg), m.Attribute, m.TypeDesc, "o");
if (m.CheckSpecified != SpecifiedAccessor.None)
{
ilg.EndIf();
}
if (m.CheckShouldPersist)
{
ilg.EndIf();
}
}
}
for (int i = 0; i < members.Length; i++)
{
MemberMapping m = members[i];
if (m.Xmlns != null)
continue;
CodeIdentifier.CheckValidIdentifier(m.Name);
bool checkShouldPersist = m.CheckShouldPersist && (m.Elements.Length > 0 || m.Text != null);
if (checkShouldPersist)
{
ilg.LdargAddress(oArg);
ilg.Call(m.CheckShouldPersistMethodInfo);
ilg.If();
}
if (m.CheckSpecified != SpecifiedAccessor.None)
{
string memberGet = RaCodeGen.GetStringForMember("o", m.Name + "Specified", mapping.TypeDesc);
ILGenLoad(memberGet);
ilg.If();
}
string choiceSource = null;
if (m.ChoiceIdentifier != null)
{
CodeIdentifier.CheckValidIdentifier(m.ChoiceIdentifier.MemberName);
choiceSource = RaCodeGen.GetStringForMember("o", m.ChoiceIdentifier.MemberName, mapping.TypeDesc);
}
WriteMember(RaCodeGen.GetSourceForMember("o", m, m.MemberInfo, mapping.TypeDesc, ilg), choiceSource, m.ElementsSortedByDerivation, m.Text, m.ChoiceIdentifier, m.TypeDesc, true);
if (m.CheckSpecified != SpecifiedAccessor.None)
{
ilg.EndIf();
}
if (checkShouldPersist)
{
ilg.EndIf();
}
}
WriteEndElement("o");
}
ilg.EndMethod();
}
示例13: GenerateInitCallbacksMethod
private void GenerateInitCallbacksMethod()
{
ilg = new CodeGenerator(this.typeBuilder);
ilg.BeginMethod(typeof(void), "InitCallbacks", Array.Empty<Type>(), Array.Empty<string>(),
CodeGenerator.ProtectedOverrideMethodAttributes);
ilg.EndMethod();
}
示例14: WriteEnumMethod
//.........这里部分代码省略.........
caseLabels.Add(caseLabel);
retValues.Add(GetCSharpString(c.XmlName));
values.Add(c.Value, c.Value);
}
}
if (mapping.IsFlags)
{
ilg.Br(defaultLabel);
for (int i = 0; i < caseLabels.Count; i++)
{
ilg.MarkLabel(caseLabels[i]);
ilg.Ldc(retValues[i]);
ilg.Stloc(sLoc);
ilg.Br(endSwitchLabel);
}
ilg.MarkLabel(defaultLabel);
RaCodeGen.ILGenForEnumLongValue(ilg, "v");
LocalBuilder strArray = ilg.DeclareLocal(typeof(String[]), "strArray");
ilg.NewArray(typeof(String), constants.Length);
ilg.Stloc(strArray);
for (int i = 0; i < constants.Length; i++)
{
ConstantMapping c = constants[i];
ilg.Ldloc(strArray);
ilg.Ldc(i);
ilg.Ldstr(GetCSharpString(c.XmlName));
ilg.Stelem(typeof(String));
}
ilg.Ldloc(strArray);
LocalBuilder longArray = ilg.DeclareLocal(typeof(long[]), "longArray");
ilg.NewArray(typeof(long), constants.Length);
ilg.Stloc(longArray);
for (int i = 0; i < constants.Length; i++)
{
ConstantMapping c = constants[i];
ilg.Ldloc(longArray);
ilg.Ldc(i);
ilg.Ldc(c.Value);
ilg.Stelem(typeof(long));
}
ilg.Ldloc(longArray);
ilg.Ldstr(GetCSharpString(mapping.TypeDesc.FullName));
MethodInfo XmlSerializationWriter_FromEnum = typeof(XmlSerializationWriter).GetMethod(
"FromEnum",
CodeGenerator.StaticBindingFlags,
new Type[] { typeof(Int64), typeof(String[]), typeof(Int64[]), typeof(String) }
);
ilg.Call(XmlSerializationWriter_FromEnum);
ilg.Stloc(sLoc);
ilg.Br(endSwitchLabel);
}
else
{
ilg.Br(defaultLabel);
// Case bodies
for (int i = 0; i < caseLabels.Count; i++)
{
ilg.MarkLabel(caseLabels[i]);
ilg.Ldc(retValues[i]);
ilg.Stloc(sLoc);
ilg.Br(endSwitchLabel);
}
MethodInfo CultureInfo_get_InvariantCulture = typeof(CultureInfo).GetMethod(
"get_InvariantCulture",
CodeGenerator.StaticBindingFlags,
Array.Empty<Type>()
);
MethodInfo Int64_ToString = typeof(Int64).GetMethod(
"ToString",
CodeGenerator.InstanceBindingFlags,
new Type[] { typeof(IFormatProvider) }
);
MethodInfo XmlSerializationWriter_CreateInvalidEnumValueException = typeof(XmlSerializationWriter).GetMethod(
"CreateInvalidEnumValueException",
CodeGenerator.InstanceBindingFlags,
new Type[] { typeof(object), typeof(string) }
);
// Default body
ilg.MarkLabel(defaultLabel);
ilg.Ldarg(0);
ilg.Ldarg("v");
ilg.ConvertValue(mapping.TypeDesc.Type, typeof(Int64));
LocalBuilder numLoc = ilg.DeclareLocal(typeof(Int64), "num");
ilg.Stloc(numLoc);
// Invoke method on Value type need address
ilg.LdlocAddress(numLoc);
ilg.Call(CultureInfo_get_InvariantCulture);
ilg.Call(Int64_ToString);
ilg.Ldstr(GetCSharpString(mapping.TypeDesc.FullName));
ilg.Call(XmlSerializationWriter_CreateInvalidEnumValueException);
ilg.Throw();
}
ilg.MarkLabel(endSwitchLabel);
}
ilg.Ldloc(sLoc);
ilg.EndMethod();
}
示例15: GenerateTypeElement
private string GenerateTypeElement(XmlTypeMapping xmlTypeMapping)
{
ElementAccessor element = xmlTypeMapping.Accessor;
TypeMapping mapping = element.Mapping;
string methodName = NextMethodName(element.Name);
ilg = new CodeGenerator(this.typeBuilder);
ilg.BeginMethod(
typeof(void),
methodName,
new Type[] { typeof(object) },
new string[] { "o" },
CodeGenerator.PublicMethodAttributes
);
MethodInfo XmlSerializationWriter_WriteStartDocument = typeof(XmlSerializationWriter).GetMethod(
"WriteStartDocument",
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ilg.Ldarg(0);
ilg.Call(XmlSerializationWriter_WriteStartDocument);
ilg.If(ilg.GetArg("o"), Cmp.EqualTo, null);
if (element.IsNullable)
{
WriteLiteralNullTag(element.Name, (element.Form == XmlSchemaForm.Qualified ? element.Namespace : ""));
}
else
WriteEmptyTag(element.Name, (element.Form == XmlSchemaForm.Qualified ? element.Namespace : ""));
ilg.GotoMethodEnd();
ilg.EndIf();
if (!mapping.TypeDesc.IsValueType && !mapping.TypeDesc.Type.GetTypeInfo().IsPrimitive)
{
MethodInfo XmlSerializationWriter_TopLevelElement = typeof(XmlSerializationWriter).GetMethod(
"TopLevelElement",
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ilg.Ldarg(0);
ilg.Call(XmlSerializationWriter_TopLevelElement);
}
WriteMember(new SourceInfo("o", "o", null, typeof(object), ilg), null, new ElementAccessor[] { element }, null, null, mapping.TypeDesc, true);
ilg.EndMethod();
return methodName;
}