本文整理汇总了C#中CodeGenerator.Call方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenerator.Call方法的具体用法?C# CodeGenerator.Call怎么用?C# CodeGenerator.Call使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGenerator
的用法示例。
在下文中一共展示了CodeGenerator.Call方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateTypeElement
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(object),
methodName,
CodeGenerator.EmptyTypeArray,
CodeGenerator.EmptyStringArray,
CodeGenerator.PublicMethodAttributes
);
LocalBuilder oLoc = ilg.DeclareLocal(typeof(object), "o");
ilg.Load(null);
ilg.Stloc(oLoc);
MemberMapping member = new MemberMapping();
member.TypeDesc = mapping.TypeDesc;
//member.ReadOnly = !mapping.TypeDesc.HasDefaultConstructor;
member.Elements = new ElementAccessor[] { element };
Member[] members = new Member[] { new Member(this, "o", "o", "a", 0, member) };
MethodInfo XmlSerializationReader_get_Reader = typeof(XmlSerializationReader).GetMethod(
"get_Reader",
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null
);
MethodInfo XmlReader_MoveToContent = typeof(XmlReader).GetMethod(
"MoveToContent",
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null
);
ilg.Ldarg(0);
ilg.Call(XmlSerializationReader_get_Reader);
ilg.Call(XmlReader_MoveToContent);
ilg.Pop();
string unknownNode = "UnknownNode(null, " + ExpectedElements(members) + ");";
WriteMemberElements(members, "throw CreateUnknownNodeException();", unknownNode, element.Any ? members[0] : null, null);
ilg.Ldloc(oLoc);
// for code compat as compiler does
ilg.Stloc(ilg.ReturnLocal);
ilg.Ldloc(ilg.ReturnLocal);
ilg.EndMethod();
return methodName;
}
示例2: WriteHashtable
string WriteHashtable(EnumMapping mapping, string typeName, out MethodBuilder get_TableName) {
get_TableName = null;
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);
FieldBuilder fieldBuilder = this.typeBuilder.DefineField(
memberName,
typeof(Hashtable),
FieldAttributes.Private
);
PropertyBuilder propertyBuilder = this.typeBuilder.DefineProperty(
propName,
PropertyAttributes.None,
CallingConventions.HasThis,
typeof(Hashtable),
null, null, null, null, null);
ilg = new CodeGenerator(this.typeBuilder);
ilg.BeginMethod(
typeof(Hashtable),
"get_" + propName,
CodeGenerator.EmptyTypeArray,
CodeGenerator.EmptyStringArray,
MethodAttributes.Assembly | MethodAttributes.HideBySig | MethodAttributes.SpecialName);
ilg.Ldarg(0);
ilg.LoadMember(fieldBuilder);
ilg.Load(null);
ilg.If(Cmp.EqualTo);
ConstructorInfo Hashtable_ctor = typeof(Hashtable).GetConstructor(
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null
);
LocalBuilder hLoc = ilg.DeclareLocal(typeof(Hashtable), "h");
ilg.New(Hashtable_ctor);
ilg.Stloc(hLoc);
ConstantMapping[] constants = mapping.Constants;
MethodInfo Hashtable_Add = typeof(Hashtable).GetMethod(
"Add",
CodeGenerator.InstanceBindingFlags,
null,
new Type[] { typeof(Object), typeof(Object) },
null
);
for (int i = 0; i < constants.Length; i++) {
ilg.Ldloc(hLoc);
ilg.Ldstr(constants[i].XmlName);
ilg.Ldc(Enum.ToObject(mapping.TypeDesc.Type, constants[i].Value));
ilg.ConvertValue(mapping.TypeDesc.Type, typeof(long));
ilg.ConvertValue(typeof(long), typeof(Object));
ilg.Call(Hashtable_Add);
}
ilg.Ldarg(0);
ilg.Ldloc(hLoc);
ilg.StoreMember(fieldBuilder);
ilg.EndIf();
ilg.Ldarg(0);
ilg.LoadMember(fieldBuilder);
get_TableName = ilg.EndMethod();
propertyBuilder.SetGetMethod(get_TableName);
return propName;
}
示例3: GenerateInitCallbacksMethod
void GenerateInitCallbacksMethod() {
ilg = new CodeGenerator(this.typeBuilder);
ilg.BeginMethod(typeof(void), "InitCallbacks", CodeGenerator.EmptyTypeArray, CodeGenerator.EmptyStringArray,
CodeGenerator.ProtectedOverrideMethodAttributes);
string dummyArrayMethodName = NextMethodName("Array");
bool needDummyArrayMethod = false;
ilg.EndMethod();
if (needDummyArrayMethod) {
ilg.BeginMethod(
typeof(object),
GetMethodBuilder(dummyArrayMethodName),
CodeGenerator.EmptyTypeArray,
CodeGenerator.EmptyStringArray,
CodeGenerator.PrivateMethodAttributes);
MethodInfo XmlSerializationReader_UnknownNode1 = typeof(XmlSerializationReader).GetMethod(
"UnknownNode",
CodeGenerator.InstanceBindingFlags,
null,
new Type[] { typeof(object) },
null
);
ilg.Ldarg(0);
ilg.Load(null);
ilg.Call(XmlSerializationReader_UnknownNode1);
ilg.Load(null);
ilg.EndMethod();
}
}
示例4: GenerateLiteralMembersElement
string GenerateLiteralMembersElement(XmlMembersMapping xmlMembersMapping) {
ElementAccessor element = xmlMembersMapping.Accessor;
MemberMapping[] mappings = ((MembersMapping)element.Mapping).Members;
bool hasWrapperElement = ((MembersMapping)element.Mapping).HasWrapperElement;
string methodName = NextMethodName(element.Name);
ilg = new CodeGenerator(this.typeBuilder);
ilg.BeginMethod(
typeof(object[]),
methodName,
CodeGenerator.EmptyTypeArray,
CodeGenerator.EmptyStringArray,
CodeGenerator.PublicMethodAttributes
);
ilg.Load(null);
ilg.Stloc(ilg.ReturnLocal);
MethodInfo XmlSerializationReader_get_Reader = typeof(XmlSerializationReader).GetMethod(
"get_Reader",
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null
);
MethodInfo XmlReader_MoveToContent = typeof(XmlReader).GetMethod(
"MoveToContent",
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null
);
ilg.Ldarg(0);
ilg.Call(XmlSerializationReader_get_Reader);
ilg.Call(XmlReader_MoveToContent);
ilg.Pop();
LocalBuilder localP = ilg.DeclareLocal(typeof(object[]), "p");
ilg.NewArray(typeof(object), mappings.Length);
ilg.Stloc(localP);
InitializeValueTypes("p", mappings);
int wrapperLoopIndex = 0;
if (hasWrapperElement) {
wrapperLoopIndex = WriteWhileNotLoopStart();
WriteIsStartTag(element.Name, element.Form == XmlSchemaForm.Qualified ? element.Namespace : "");
}
Member anyText = null;
Member anyElement = null;
Member anyAttribute = null;
ArrayList membersList = new ArrayList();
ArrayList textOrArrayMembersList = new ArrayList();
ArrayList attributeMembersList = new ArrayList();
for (int i = 0; i < mappings.Length; i++) {
MemberMapping mapping = mappings[i];
string source = "p[" + i.ToString(CultureInfo.InvariantCulture) + "]";
string arraySource = source;
if (mapping.Xmlns != null) {
arraySource = "((" + mapping.TypeDesc.CSharpName + ")" + source + ")";
}
string choiceSource = GetChoiceIdentifierSource(mappings, mapping);
Member member = new Member(this, source, arraySource, "a", i, mapping, choiceSource);
Member anyMember = new Member(this, source, null, "a", i, mapping, choiceSource);
if (!mapping.IsSequence)
member.ParamsReadSource = "paramsRead[" + i.ToString(CultureInfo.InvariantCulture) + "]";
if (mapping.CheckSpecified == SpecifiedAccessor.ReadWrite) {
string nameSpecified = mapping.Name + "Specified";
for (int j = 0; j < mappings.Length; j++) {
if (mappings[j].Name == nameSpecified) {
member.CheckSpecifiedSource = "p[" + j.ToString(CultureInfo.InvariantCulture) + "]";
break;
}
}
}
bool foundAnyElement = false;
if (mapping.Text != null) anyText = anyMember;
if (mapping.Attribute != null && mapping.Attribute.Any)
anyAttribute = anyMember;
if (mapping.Attribute != null || mapping.Xmlns != null)
attributeMembersList.Add(member);
else if (mapping.Text != null)
textOrArrayMembersList.Add(member);
if (!mapping.IsSequence) {
for (int j = 0; j < mapping.Elements.Length; j++) {
if (mapping.Elements[j].Any && mapping.Elements[j].Name.Length == 0) {
anyElement = anyMember;
if (mapping.Attribute == null && mapping.Text == null)
textOrArrayMembersList.Add(anyMember);
foundAnyElement = true;
break;
}
}
}
if (mapping.Attribute != null || mapping.Text != null || foundAnyElement)
membersList.Add(anyMember);
else if (mapping.TypeDesc.IsArrayLike && !(mapping.Elements.Length == 1 && mapping.Elements[0].Mapping is ArrayMapping)) {
membersList.Add(anyMember);
textOrArrayMembersList.Add(anyMember);
}
//.........这里部分代码省略.........
示例5: 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();
}
示例6: ILGenForCreateInstance
internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bool nonPublic) {
// Special case DBNull
if (type == typeof(DBNull)) {
FieldInfo DBNull_Value = typeof(DBNull).GetField("Value", CodeGenerator.StaticBindingFlags);
ilg.LoadMember(DBNull_Value);
return;
}
// Special case XElement
// codegen the same as 'internal XElement : this("default") { }'
if (type.FullName == "System.Xml.Linq.XElement") {
Type xName = type.Assembly.GetType("System.Xml.Linq.XName");
if (xName != null) {
MethodInfo XName_op_Implicit = xName.GetMethod(
"op_Implicit",
CodeGenerator.StaticBindingFlags,
null,
new Type[] { typeof(String) },
null
);
ConstructorInfo XElement_ctor = type.GetConstructor(
CodeGenerator.InstanceBindingFlags,
null,
new Type[] { xName },
null
);
if (XName_op_Implicit != null && XElement_ctor != null) {
ilg.Ldstr("default");
ilg.Call(XName_op_Implicit);
ilg.New(XElement_ctor);
return;
}
}
}
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
if (nonPublic) {
bindingFlags |= BindingFlags.NonPublic;
}
MethodInfo Activator_CreateInstance = typeof(Activator).GetMethod(
"CreateInstance",
CodeGenerator.StaticBindingFlags,
null,
new Type[] { typeof(Type), typeof(BindingFlags), typeof(Binder), typeof(Object[]), typeof(CultureInfo) },
null
);
ilg.Ldc(type);
ilg.Load((int)bindingFlags);
ilg.Load(null);
ilg.NewArray(typeof(Object), 0);
ilg.Load(null);
ilg.Call(Activator_CreateInstance);
if (cast != null)
ilg.ConvertValue(Activator_CreateInstance.ReturnType, cast);
}
示例7: WriteEnumMethod
void WriteEnumMethod(EnumMapping mapping) {
MethodBuilder get_TableName = null;
if (mapping.IsFlags)
WriteHashtable(mapping, mapping.TypeDesc.Name, out get_TableName);
string methodName = (string)MethodNames[mapping];
string fullTypeName = mapping.TypeDesc.CSharpName;
List<Type> argTypes = new List<Type>();
List<string> argNames = new List<string>();
Type returnType;
Type underlyingType;
returnType = mapping.TypeDesc.Type;
underlyingType = Enum.GetUnderlyingType(returnType);
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);
//.........这里部分代码省略.........
示例8: 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);
}
示例9: WriteEnumMethod
private void WriteEnumMethod(EnumMapping mapping)
{
string methodName = (string)MethodNames[mapping];
List<Type> argTypes = new List<Type>();
List<string> argNames = new List<string>();
argTypes.Add(mapping.TypeDesc.Type);
argNames.Add("v");
ilg = new CodeGenerator(this.typeBuilder);
ilg.BeginMethod(
typeof(string),
GetMethodBuilder(methodName),
argTypes.ToArray(),
argNames.ToArray(),
CodeGenerator.PrivateMethodAttributes);
LocalBuilder sLoc = ilg.DeclareLocal(typeof(string), "s");
ilg.Load(null);
ilg.Stloc(sLoc);
ConstantMapping[] constants = mapping.Constants;
if (constants.Length > 0)
{
InternalHashtable values = new InternalHashtable();
List<Label> caseLabels = new List<Label>();
List<string> retValues = new List<string>();
Label defaultLabel = ilg.DefineLabel();
Label endSwitchLabel = ilg.DefineLabel();
// This local is necessary; otherwise, it becomes if/else
LocalBuilder localTmp = ilg.DeclareLocal(mapping.TypeDesc.Type, "localTmp");
ilg.Ldarg("v");
ilg.Stloc(localTmp);
for (int i = 0; i < constants.Length; i++)
{
ConstantMapping c = constants[i];
if (values[c.Value] == null)
{
Label caseLabel = ilg.DefineLabel();
ilg.Ldloc(localTmp);
ilg.Ldc(Enum.ToObject(mapping.TypeDesc.Type, c.Value));
ilg.Beq(caseLabel);
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]);
//.........这里部分代码省略.........
示例10: WriteStructMethod
private void WriteStructMethod(StructMapping mapping)
{
string methodName = (string)MethodNames[mapping];
ilg = new CodeGenerator(this.typeBuilder);
List<Type> argTypes = new List<Type>(5);
List<string> argNames = new List<string>(5);
argTypes.Add(typeof(string));
argNames.Add("n");
argTypes.Add(typeof(string));
argNames.Add("ns");
argTypes.Add(mapping.TypeDesc.Type);
argNames.Add("o");
if (mapping.TypeDesc.IsNullable)
{
argTypes.Add(typeof(Boolean));
argNames.Add("isNullable");
}
argTypes.Add(typeof(Boolean));
argNames.Add("needType");
ilg.BeginMethod(typeof(void),
GetMethodBuilder(methodName),
argTypes.ToArray(),
argNames.ToArray(),
CodeGenerator.PrivateMethodAttributes);
if (mapping.TypeDesc.IsNullable)
{
ilg.If(ilg.GetArg("o"), Cmp.EqualTo, null);
{
ilg.If(ilg.GetArg("isNullable"), Cmp.EqualTo, true);
{
MethodInfo XmlSerializationWriter_WriteNullTagLiteral = typeof(XmlSerializationWriter).GetMethod(
"WriteNullTagLiteral",
CodeGenerator.InstanceBindingFlags,
new Type[] { typeof(String), typeof(String) }
);
ilg.Ldarg(0);
ilg.Ldarg("n");
ilg.Ldarg("ns");
ilg.Call(XmlSerializationWriter_WriteNullTagLiteral);
}
ilg.EndIf();
ilg.GotoMethodEnd();
}
ilg.EndIf();
}
ilg.If(ilg.GetArg("needType"), Cmp.NotEqualTo, true); // if (!needType)
LocalBuilder tLoc = ilg.DeclareLocal(typeof(Type), "t");
MethodInfo Object_GetType = typeof(object).GetMethod(
"GetType",
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ArgBuilder oArg = ilg.GetArg("o");
ilg.LdargAddress(oArg);
ilg.ConvertAddress(oArg.ArgType, typeof(object));
ilg.Call(Object_GetType);
ilg.Stloc(tLoc);
WriteTypeCompare("t", mapping.TypeDesc.Type);
// Bool on the stack from WriteTypeCompare.
ilg.If(); // if (t == typeof(...))
WriteDerivedTypes(mapping);
if (mapping.TypeDesc.IsRoot)
WriteEnumAndArrayTypes();
ilg.Else();
if (mapping.TypeDesc.IsRoot)
{
MethodInfo XmlSerializationWriter_WriteTypedPrimitive = typeof(XmlSerializationWriter).GetMethod(
"WriteTypedPrimitive",
CodeGenerator.InstanceBindingFlags,
new Type[] { typeof(String), typeof(String), typeof(Object), typeof(Boolean) }
);
ilg.Ldarg(0);
ilg.Ldarg("n");
ilg.Ldarg("ns");
ilg.Ldarg("o");
ilg.Ldc(true);
ilg.Call(XmlSerializationWriter_WriteTypedPrimitive);
ilg.GotoMethodEnd();
}
else
{
MethodInfo XmlSerializationWriter_CreateUnknownTypeException = typeof(XmlSerializationWriter).GetMethod(
"CreateUnknownTypeException",
CodeGenerator.InstanceBindingFlags,
new Type[] { typeof(Object) }
);
ilg.Ldarg(0);
ilg.Ldarg(oArg);
ilg.ConvertValue(oArg.ArgType, typeof(Object));
ilg.Call(XmlSerializationWriter_CreateUnknownTypeException);
ilg.Throw();
}
ilg.EndIf(); // if (t == typeof(...))
ilg.EndIf(); // if (!needType)
if (!mapping.TypeDesc.IsAbstract)
{
if (mapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(mapping.TypeDesc.Type))
//.........这里部分代码省略.........
示例11: 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;
}
示例12: GenerateMembersElement
private string GenerateMembersElement(XmlMembersMapping xmlMembersMapping)
{
ElementAccessor element = xmlMembersMapping.Accessor;
MembersMapping mapping = (MembersMapping)element.Mapping;
bool hasWrapperElement = mapping.HasWrapperElement;
bool writeAccessors = mapping.WriteAccessors;
string methodName = NextMethodName(element.Name);
ilg = new CodeGenerator(this.typeBuilder);
ilg.BeginMethod(
typeof(void),
methodName,
new Type[] { typeof(object[]) },
new string[] { "p" },
CodeGenerator.PublicMethodAttributes
);
MethodInfo XmlSerializationWriter_WriteStartDocument = typeof(XmlSerializationWriter).GetMethod(
"WriteStartDocument",
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ilg.Ldarg(0);
ilg.Call(XmlSerializationWriter_WriteStartDocument);
MethodInfo XmlSerializationWriter_TopLevelElement = typeof(XmlSerializationWriter).GetMethod(
"TopLevelElement",
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
ilg.Ldarg(0);
ilg.Call(XmlSerializationWriter_TopLevelElement);
// in the top-level method add check for the parameters length,
// because visual basic does not have a concept of an <out> parameter it uses <ByRef> instead
// so sometime we think that we have more parameters then supplied
LocalBuilder pLengthLoc = ilg.DeclareLocal(typeof(int), "pLength");
ilg.Ldarg("p");
ilg.Ldlen();
ilg.Stloc(pLengthLoc);
if (hasWrapperElement)
{
WriteStartElement(element.Name, (element.Form == XmlSchemaForm.Qualified ? element.Namespace : ""), false);
int xmlnsMember = FindXmlnsIndex(mapping.Members);
if (xmlnsMember >= 0)
{
MemberMapping member = mapping.Members[xmlnsMember];
string source = "((" + typeof(XmlSerializerNamespaces).FullName + ")p[" + xmlnsMember.ToString(CultureInfo.InvariantCulture) + "])";
ilg.Ldloc(pLengthLoc);
ilg.Ldc(xmlnsMember);
ilg.If(Cmp.GreaterThan);
WriteNamespaces(source);
ilg.EndIf();
}
for (int i = 0; i < mapping.Members.Length; i++)
{
MemberMapping member = mapping.Members[i];
if (member.Attribute != null && !member.Ignore)
{
SourceInfo source = new SourceInfo("p[" + i.ToString(CultureInfo.InvariantCulture) + "]", null, null, pLengthLoc.LocalType.GetElementType(), ilg);
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();
}
//.........这里部分代码省略.........
示例13: ILGenForCreateInstance
internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bool nonPublic)
{
// Special case DBNull
if (type == Globals.TypeOfDBNull)
{
FieldInfo DBNull_Value = Globals.TypeOfDBNull.GetField("Value", CodeGenerator.StaticBindingFlags);
ilg.LoadMember(DBNull_Value);
return;
}
// Special case XElement
// codegen the same as 'internal XElement : this("default") { }'
if (type.FullName == "System.Xml.Linq.XElement")
{
Type xName = type.GetTypeInfo().Assembly.GetType("System.Xml.Linq.XName");
if (xName != null)
{
MethodInfo XName_op_Implicit = xName.GetMethod(
"op_Implicit",
CodeGenerator.StaticBindingFlags,
new Type[] { typeof(String) }
);
ConstructorInfo XElement_ctor = type.GetConstructor(
CodeGenerator.InstanceBindingFlags,
new Type[] { xName }
);
if (XName_op_Implicit != null && XElement_ctor != null)
{
ilg.Ldstr("default");
ilg.Call(XName_op_Implicit);
ilg.New(XElement_ctor);
return;
}
}
}
Label labelReturn = ilg.DefineLabel();
Label labelEndIf = ilg.DefineLabel();
// TypeInfo typeInfo = type.GetTypeInfo();
// typeInfo not declared explicitly
ilg.Ldc(type);
MethodInfo getTypeInfoMehod = typeof(IntrospectionExtensions).GetMethod(
"GetTypeInfo",
CodeGenerator.StaticBindingFlags,
new[] { typeof(Type) }
);
ilg.Call(getTypeInfoMehod);
// IEnumerator<ConstructorInfo> e = typeInfo.DeclaredConstructors.GetEnumerator();
LocalBuilder enumerator = ilg.DeclareLocal(typeof(IEnumerator<>).MakeGenericType(typeof(ConstructorInfo)), "e");
MethodInfo getDeclaredConstructors = typeof(TypeInfo).GetMethod("get_DeclaredConstructors");
MethodInfo getEnumerator = typeof(IEnumerable<>).MakeGenericType(typeof(ConstructorInfo)).GetMethod("GetEnumerator");
ilg.Call(getDeclaredConstructors);
ilg.Call(getEnumerator);
ilg.Stloc(enumerator);
ilg.WhileBegin();
// ConstructorInfo constructorInfo = e.Current();
MethodInfo enumeratorCurrent = typeof(IEnumerator).GetMethod("get_Current");
ilg.Ldloc(enumerator);
ilg.Call(enumeratorCurrent);
LocalBuilder constructorInfo = ilg.DeclareLocal(typeof(ConstructorInfo), "constructorInfo");
ilg.Stloc(constructorInfo);
// if (!constructorInfo.IsStatic && constructorInfo.GetParameters.Length() == 0)
ilg.Ldloc(constructorInfo);
MethodInfo constructorIsStatic = typeof(ConstructorInfo).GetMethod("get_IsStatic");
ilg.Call(constructorIsStatic);
ilg.Brtrue(labelEndIf);
ilg.Ldloc(constructorInfo);
MethodInfo constructorGetParameters = typeof(ConstructorInfo).GetMethod("GetParameters");
ilg.Call(constructorGetParameters);
ilg.Ldlen();
ilg.Ldc(0);
ilg.Cne();
ilg.Brtrue(labelEndIf);
// constructorInfo.Invoke(null);
MethodInfo constructorInvoke = typeof(ConstructorInfo).GetMethod("Invoke", new Type[] { typeof(object[]) });
ilg.Ldloc(constructorInfo);
ilg.Load(null);
ilg.Call(constructorInvoke);
ilg.Br(labelReturn);
ilg.MarkLabel(labelEndIf);
ilg.WhileBeginCondition(); // while (e.MoveNext())
MethodInfo IEnumeratorMoveNext = typeof(IEnumerator).GetMethod(
"MoveNext",
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>());
ilg.Ldloc(enumerator);
ilg.Call(IEnumeratorMoveNext);
ilg.WhileEndCondition();
ilg.WhileEnd();
MethodInfo Activator_CreateInstance = typeof(Activator).GetMethod(
"CreateInstance",
CodeGenerator.StaticBindingFlags,
new Type[] { typeof(Type) }
//.........这里部分代码省略.........
示例14: 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();
}
示例15: 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();
}