本文整理汇总了C#中System.Reflection.Emit.FieldBuilder类的典型用法代码示例。如果您正苦于以下问题:C# FieldBuilder类的具体用法?C# FieldBuilder怎么用?C# FieldBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FieldBuilder类属于System.Reflection.Emit命名空间,在下文中一共展示了FieldBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MixinType
public static void MixinType (TypeBuilder typeBuilder,Type mixinInterfaceType,FieldBuilder mixinField)
{
MethodInfo[] methods = mixinInterfaceType.GetMethods();
foreach (MethodInfo method in methods)
MixinMethod(typeBuilder,method,mixinField);
}
示例2: Build
public void Build(TypeBuilder typeBuilder, FieldBuilder injectionSetField, Type injectionType)
{
var parameterTypes = new[] { typeof(DbModelBuilder), typeof(DbContext) };
var methods = new Methods(typeBuilder, MethodName, new[] { typeof(DbModelBuilder) });
var injectionMethod = ReflectionHelper.GetMethod(injectionType, MethodName, BindingFlags.Instance | BindingFlags.Public, parameterTypes);
var il = methods.GetILGenerator(injectionType);
DeclareLocals(injectionType, il);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Call, methods.BaseMethod);
il.EmitGetInjections(injectionSetField, injectionType);
il.EmitInjectionLoop(x =>
{
x.Emit(OpCodes.Ldarg_1);
x.Emit(OpCodes.Ldarg_0);
x.Emit(OpCodes.Callvirt, injectionMethod);
ConfigureDbSets(typeBuilder, x);
});
il.Emit(OpCodes.Ret);
}
示例3: DefineRaiseMethod
private MethodBuilder DefineRaiseMethod(TypeBuilder builder, FieldBuilder addPropertyChangedField)
{
var notifyPropertyChangedMethod = builder.DefineMethod("NotifyPropertyChanged", MethodAttributes.Public | MethodAttributes.Virtual, typeof(void),
new [] {typeof (string)});
var il = notifyPropertyChangedMethod.GetILGenerator();
il.DeclareLocal(typeof (bool));
il.Emit(OpCodes.Ldnull);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, addPropertyChangedField);
il.Emit(OpCodes.Ceq);
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
Label ifNullLabel = il.DefineLabel();
il.Emit(OpCodes.Brtrue_S, ifNullLabel);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, addPropertyChangedField);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Newobj, typeof (PropertyChangedEventArgs).GetConstructors().First());
il.Emit(OpCodes.Callvirt, typeof(PropertyChangedEventHandler).GetMethod("Invoke"));
il.MarkLabel(ifNullLabel);
il.Emit(OpCodes.Ret);
return notifyPropertyChangedMethod;
}
示例4: CreateGenerator
public override MethodGenerator CreateGenerator(
FieldMetadataFieldBuilderMap instanceFieldBuilders,
MethodMetadataFieldBuilderMap methodMetadataFieldBuilders,
FieldBuilder dispatcherField)
{
return new DuckTypeMethodGenerator(this);
}
示例5: PropertyEmitter
public PropertyEmitter(TypeBuilder owner, string name, Type propertyType, FieldBuilder propertyChangedField) {
this.owner = owner;
this.propertyChangedField = propertyChangedField;
fieldBuilder = owner.DefineField(String.Format("<{0}>", name), propertyType, FieldAttributes.Private);
getterBuilder = owner.DefineMethod(String.Format("get_{0}", name), MethodAttributes.Public|MethodAttributes.Virtual|MethodAttributes.HideBySig|MethodAttributes.SpecialName, propertyType, Type.EmptyTypes);
ILGenerator getterIl = getterBuilder.GetILGenerator();
getterIl.Emit(OpCodes.Ldarg_0);
getterIl.Emit(OpCodes.Ldfld, fieldBuilder);
getterIl.Emit(OpCodes.Ret);
setterBuilder = owner.DefineMethod(String.Format("set_{0}", name), MethodAttributes.Public|MethodAttributes.Virtual|MethodAttributes.HideBySig|MethodAttributes.SpecialName, typeof(void), new[] {propertyType});
ILGenerator setterIl = setterBuilder.GetILGenerator();
setterIl.Emit(OpCodes.Ldarg_0);
setterIl.Emit(OpCodes.Ldarg_1);
setterIl.Emit(OpCodes.Stfld, fieldBuilder);
if (propertyChangedField != null) {
setterIl.Emit(OpCodes.Ldarg_0);
setterIl.Emit(OpCodes.Dup);
setterIl.Emit(OpCodes.Ldfld, propertyChangedField);
setterIl.Emit(OpCodes.Ldstr, name);
setterIl.Emit(OpCodes.Call, proxyBase_NotifyPropertyChanged);
}
setterIl.Emit(OpCodes.Ret);
propertyBuilder = owner.DefineProperty(name, PropertyAttributes.None, propertyType, null);
propertyBuilder.SetGetMethod(getterBuilder);
propertyBuilder.SetSetMethod(setterBuilder);
}
示例6: InterfaceImplementation
public InterfaceImplementation(
TypeBuilder typeBuilder,
Type @interface,
GenericParameterMapper genericParameterMapper,
FieldBuilder proxyInterceptionPipelineField,
bool explicitImplementation,
FieldBuilder targetField)
{
this.typeBuilder = typeBuilder;
[email protected] = @interface;
this.genericParameterMapper = genericParameterMapper;
this.proxyInterceptionPipelineField = proxyInterceptionPipelineField;
this.explicitImplementation = explicitImplementation;
this.targetField = targetField;
if (@interface.IsGenericType)
{
// when the @interface is generic we need to get references to its methods though it
// in this case, the targetInterface is a constructed version using the generic type parameters
// from the generated type generate type
var definition = @interface.GetGenericTypeDefinition();
var mappedParameters = definition.GetGenericArguments().Select(t => genericParameterMapper.Map(t)).ToArray();
this.targetInterface = definition.MakeGenericType(mappedParameters);
}
else
{
this.targetInterface = @interface;
}
}
示例7: GenerateProperties
private static FieldInfo[] GenerateProperties(TypeBuilder tb, IDictionary<string, Type> properties)
{
const MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
FieldInfo[] fields = new FieldBuilder[properties.Count];
int i = 0;
foreach (string key in properties.Keys)
{
string name = key;
Type type = properties[key];
FieldBuilder fb = tb.DefineField("_" + key, type, FieldAttributes.Public);
PropertyBuilder pb = tb.DefineProperty(key, PropertyAttributes.HasDefault, type, null);
MethodBuilder mbGet = tb.DefineMethod("get_" + name, getSetAttr, type, Type.EmptyTypes);
ILGenerator genGet = mbGet.GetILGenerator(8);
genGet.Emit(OpCodes.Ldarg_0);
genGet.Emit(OpCodes.Ldfld, fb);
genGet.Emit(OpCodes.Ret);
pb.SetGetMethod(mbGet);
MethodBuilder mbSet = tb.DefineMethod("set_" + name, getSetAttr, null, new Type[] { type });
ILGenerator genSet = mbSet.GetILGenerator(8);
genSet.Emit(OpCodes.Ldarg_0);
genSet.Emit(OpCodes.Ldarg_1);
genSet.Emit(OpCodes.Stfld, fb);
genSet.Emit(OpCodes.Ret);
pb.SetSetMethod(mbSet);
fields[i] = fb;
}
return fields;
}
示例8: GenerateProperties
private FieldInfo[] GenerateProperties(TypeBuilder tb, DynamicProperty[] properties)
{
FieldInfo[] array = new FieldBuilder[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
DynamicProperty dynamicProperty = properties[i];
FieldBuilder fieldBuilder = tb.DefineField("_" + dynamicProperty.Name, dynamicProperty.Type, FieldAttributes.Private);
PropertyBuilder propertyBuilder = tb.DefineProperty(dynamicProperty.Name, PropertyAttributes.HasDefault, dynamicProperty.Type, null);
MethodBuilder methodBuilder = tb.DefineMethod("get_" + dynamicProperty.Name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.SpecialName, dynamicProperty.Type, Type.EmptyTypes);
ILGenerator iLGenerator = methodBuilder.GetILGenerator();
iLGenerator.Emit(OpCodes.Ldarg_0);
iLGenerator.Emit(OpCodes.Ldfld, fieldBuilder);
iLGenerator.Emit(OpCodes.Ret);
MethodBuilder methodBuilder2 = tb.DefineMethod("set_" + dynamicProperty.Name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.SpecialName, null, new Type[]
{
dynamicProperty.Type
});
ILGenerator iLGenerator2 = methodBuilder2.GetILGenerator();
iLGenerator2.Emit(OpCodes.Ldarg_0);
iLGenerator2.Emit(OpCodes.Ldarg_1);
iLGenerator2.Emit(OpCodes.Stfld, fieldBuilder);
iLGenerator2.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(methodBuilder);
propertyBuilder.SetSetMethod(methodBuilder2);
array[i] = fieldBuilder;
}
return array;
}
示例9: InterfaceImplementation
public InterfaceImplementation(
TypeBuilder typeBuilder,
Type @interface,
FieldBuilder proxyInterceptionPipelineField,
bool explicitImplementation)
: this(typeBuilder, @interface, proxyInterceptionPipelineField, explicitImplementation, null)
{ }
示例10: TargetedInvocationMethodGenerator
public TargetedInvocationMethodGenerator(
TargetedMethodMetadata methodMetadata,
FieldBuilder targetField)
{
_methodMetadata = methodMetadata;
_targetField = targetField;
}
示例11: BuildConstructor
private static void BuildConstructor(TypeBuilder proxyBuilder, Type proxyType,
FieldBuilder wrappedType, FieldBuilder invokeHandlers, TypeDebugging debug)
{
var arguments = new Type[] { proxyType, typeof(IInvocationHandler[]) };
var constructor = proxyBuilder.DefineConstructor(
MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.RTSpecialName | MethodAttributes.HideBySig,
CallingConventions.Standard, arguments);
using (var generator = debug.GetMethodDebugging(constructor))
{
// Call the base constructor.
generator.Emit(OpCodes.Ldarg_0);
var objectCtor = proxyType.GetConstructor(Type.EmptyTypes);
generator.Emit(OpCodes.Call, objectCtor);
// Store the target object.
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Stfld, wrappedType);
// Store the handlers.
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_2);
generator.Emit(OpCodes.Stfld, invokeHandlers);
generator.Emit(OpCodes.Ret);
}
}
示例12: AbstractProxyBuilder
public AbstractProxyBuilder(ModuleBuilder moduleBuilder, string className, Type interfaceType, Type innerType)
{
Verify.ArgumentNotNull(moduleBuilder, "moduleBuilder");
Verify.ArgumentNotNull(className, "className");
Verify.ArgumentNotNull(interfaceType, "interfaceType");
Verify.ArgumentNotNull(innerType, "innerType");
if (!interfaceType.IsInterface) {
throw new ArgumentException("must be an interface type", "interfaceType");
}
_errorMessages = new List<string>();
_moduleBuilder = moduleBuilder;
_className = className;
_interfaceType = interfaceType;
_innerType = innerType;
_typeBuilder = _moduleBuilder.DefineType(
_className,
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit |
TypeAttributes.AutoLayout,
typeof(object),
new Type[] {_interfaceType});
_innerFieldBuilder = _typeBuilder.DefineField("inner", _innerType, FieldAttributes.Private);
}
示例13: Build
public void Build(TypeBuilder typeBuilder, FieldBuilder injectionSetField, Type injectionType)
{
var parameterTypes = new[] { typeof(string) };
var methods = new Methods(typeBuilder, _methodName, parameterTypes);
var injectionMethod = injectionType.GetMethod(_methodName, new[] { typeof(BaseType), typeof(string) });
var il = methods.GetILGenerator(injectionType);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Call, methods.BaseMethod);
il.EmitGetInjections(injectionSetField, injectionType);
il.EmitInjectionLoop(x =>
{
x.Emit(OpCodes.Ldarg_0);
x.Emit(OpCodes.Ldarg_1);
x.Emit(OpCodes.Callvirt, injectionMethod);
});
il.Emit(OpCodes.Ret);
}
示例14: EmitInitField
private static void EmitInitField(ILGenerator il, FieldBuilder fb, Type type)
{
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldtoken, type);
il.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle"));
il.Emit(OpCodes.Stfld, fb);
}
示例15: SwitchCase
public SwitchCase(TypeContainer elemType, string elemName, object[] discriminatorValues,
FieldBuilder elemField) {
m_elemType = elemType;
m_elemName = elemName;
m_elemField = elemField;
m_discriminatorValues = discriminatorValues;
}