本文整理汇总了C#中CodeGenerator.LoadMember方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenerator.LoadMember方法的具体用法?C# CodeGenerator.LoadMember怎么用?C# CodeGenerator.LoadMember使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGenerator
的用法示例。
在下文中一共展示了CodeGenerator.LoadMember方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateHashtableGetBegin
internal FieldBuilder GenerateHashtableGetBegin(string privateName, string publicName, TypeBuilder serializerContractTypeBuilder)
{
FieldBuilder fieldBuilder = serializerContractTypeBuilder.DefineField(
privateName,
typeof(Hashtable),
FieldAttributes.Private
);
ilg = new CodeGenerator(serializerContractTypeBuilder);
PropertyBuilder propertyBuilder = serializerContractTypeBuilder.DefineProperty(
publicName,
PropertyAttributes.None,
CallingConventions.HasThis,
typeof(Hashtable),
null, null, null, null, null);
ilg.BeginMethod(
typeof(Hashtable),
"get_" + publicName,
Array.Empty<Type>(),
Array.Empty<string>(),
CodeGenerator.PublicOverrideMethodAttributes | MethodAttributes.SpecialName);
propertyBuilder.SetGetMethod(ilg.MethodBuilder);
ilg.Ldarg(0);
ilg.LoadMember(fieldBuilder);
ilg.Load(null);
// this 'if' ends in GenerateHashtableGetEnd
ilg.If(Cmp.EqualTo);
ConstructorInfo Hashtable_ctor = typeof(Hashtable).GetConstructor(
CodeGenerator.InstanceBindingFlags,
Array.Empty<Type>()
);
LocalBuilder _tmpLoc = ilg.DeclareLocal(typeof(Hashtable), "_tmp");
ilg.New(Hashtable_ctor);
ilg.Stloc(_tmpLoc);
return fieldBuilder;
}
示例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: 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);
}
示例4: 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) }
//.........这里部分代码省略.........
示例5: 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;
}
}
}
MethodInfo Activator_CreateInstance = typeof(Activator).GetMethod(
"CreateInstance",
CodeGenerator.StaticBindingFlags,
new Type[] { typeof(Type) }
);
ilg.Ldc(type);
ilg.Call(Activator_CreateInstance);
if (cast != null)
ilg.ConvertValue(Activator_CreateInstance.ReturnType, cast);
}