本文整理汇总了C#中CodeGenerator.DefineLabel方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenerator.DefineLabel方法的具体用法?C# CodeGenerator.DefineLabel怎么用?C# CodeGenerator.DefineLabel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGenerator
的用法示例。
在下文中一共展示了CodeGenerator.DefineLabel方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WhileState
public WhileState(CodeGenerator ilg) {
StartLabel = ilg.DefineLabel();
CondLabel = ilg.DefineLabel();
EndLabel = ilg.DefineLabel();
}
示例2: WriteLiteralStructMethod
void WriteLiteralStructMethod(StructMapping structMapping) {
string methodName = (string)MethodNames[structMapping];
string typeName = structMapping.TypeDesc.CSharpName;
ilg = new CodeGenerator(this.typeBuilder);
List<Type> argTypes = new List<Type>();
List<string> argNames = new List<string>();
if (structMapping.TypeDesc.IsNullable) {
argTypes.Add(typeof(Boolean));
argNames.Add("isNullable");
}
argTypes.Add(typeof(Boolean));
argNames.Add("checkType");
ilg.BeginMethod(
structMapping.TypeDesc.Type,
GetMethodBuilder(methodName),
argTypes.ToArray(),
argNames.ToArray(),
CodeGenerator.PrivateMethodAttributes);
LocalBuilder locXsiType = ilg.DeclareLocal(typeof(XmlQualifiedName), "xsiType");
LocalBuilder locIsNull = ilg.DeclareLocal(typeof(Boolean), "isNull");
MethodInfo XmlSerializationReader_GetXsiType = typeof(XmlSerializationReader).GetMethod(
"GetXsiType",
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null
);
MethodInfo XmlSerializationReader_ReadNull = typeof(XmlSerializationReader).GetMethod(
"ReadNull",
CodeGenerator.InstanceBindingFlags,
null,
CodeGenerator.EmptyTypeArray,
null
);
Label labelTrue = ilg.DefineLabel();
Label labelEnd = ilg.DefineLabel();
ilg.Ldarg("checkType");
ilg.Brtrue(labelTrue);
ilg.Load(null);
ilg.Br_S(labelEnd);
ilg.MarkLabel(labelTrue);
ilg.Ldarg(0);
ilg.Call(XmlSerializationReader_GetXsiType);
ilg.MarkLabel(labelEnd);
ilg.Stloc(locXsiType);
ilg.Ldc(false);
ilg.Stloc(locIsNull);
if (structMapping.TypeDesc.IsNullable) {
ilg.Ldarg("isNullable");
ilg.If();
{
ilg.Ldarg(0);
ilg.Call(XmlSerializationReader_ReadNull);
ilg.Stloc(locIsNull);
}
ilg.EndIf();
}
ilg.Ldarg("checkType");
ilg.If(); // if (checkType)
if (structMapping.TypeDesc.IsRoot) {
ilg.Ldloc(locIsNull);
ilg.If();
ilg.Ldloc(locXsiType);
ilg.Load(null);
ilg.If(Cmp.NotEqualTo);
MethodInfo XmlSerializationReader_ReadTypedNull = typeof(XmlSerializationReader).GetMethod(
"ReadTypedNull",
CodeGenerator.InstanceBindingFlags,
null,
new Type[] { locXsiType.LocalType },
null
);
ilg.Ldarg(0);
ilg.Ldloc(locXsiType);
ilg.Call(XmlSerializationReader_ReadTypedNull);
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
ilg.Else();
if (structMapping.TypeDesc.IsValueType) {
throw CodeGenerator.NotSupported("Arg_NeverValueType");
}
else {
ilg.Load(null);
ilg.Stloc(ilg.ReturnLocal);
ilg.Br(ilg.ReturnLabel);
}
ilg.EndIf(); // if (xsiType != null)
ilg.EndIf(); // if (isNull)
}
ilg.Ldloc(typeof(XmlQualifiedName), "xsiType");
ilg.Load(null);
ilg.Ceq();
if (!structMapping.TypeDesc.IsRoot) {
labelTrue = ilg.DefineLabel();
labelEnd = ilg.DefineLabel();
// xsiType == null
ilg.Brtrue(labelTrue);
//.........这里部分代码省略.........
示例3: 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);
//.........这里部分代码省略.........
示例4: 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]);
//.........这里部分代码省略.........
示例5: 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();
}
//.........这里部分代码省略.........
示例6: 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) }
//.........这里部分代码省略.........