本文整理汇总了C#中System.Reflection.Emit.TypeBuilder.DefineField方法的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder.DefineField方法的具体用法?C# TypeBuilder.DefineField怎么用?C# TypeBuilder.DefineField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefineField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BFMemory
public BFMemory(TypeBuilder typeBuilder)
{
PointerFieldBuilder = typeBuilder.DefineField("pointer", typeof(short), FieldAttributes.Static | FieldAttributes.Private);
MemoryFieldBuilder = typeBuilder.DefineField("memory", typeof(byte[]), FieldAttributes.Static | FieldAttributes.Private);
var staticConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Static,CallingConventions.Standard, null);
GenerateStaticConstructorBody(staticConstructorBuilder.GetILGenerator());
}
示例2: ProxyClassFieldCache
public ProxyClassFieldCache(TypeBuilder typeBuilder)
{
this.typeBuilder = typeBuilder;
manualCodecFields = new Dictionary<Type, FieldBuilder>();
Processor = typeBuilder.DefineField("methodCallProcessor", typeof(IOutgoingRequestProcessor), FieldAttributes.Private | FieldAttributes.InitOnly);
Scope = typeBuilder.DefineField("scope", typeof(string), FieldAttributes.Private | FieldAttributes.InitOnly);
TimeoutSettings = typeBuilder.DefineField("timeoutSettings", typeof(TimeoutSettings), FieldAttributes.Private | FieldAttributes.InitOnly);
CodecContainer = typeBuilder.DefineField("codecContainer", typeof(ICodecContainer), FieldAttributes.Private | FieldAttributes.InitOnly);
}
示例3: ImplementProxy
public void ImplementProxy(TypeBuilder typeBuilder)
{
// Implement the IProxy interface
typeBuilder.AddInterfaceImplementation(typeof (IProxy));
field = typeBuilder.DefineField("__interceptor", typeof (IInterceptor), FieldAttributes.Private);
// Implement the getter
MethodBuilder getterMethod = typeBuilder.DefineMethod("get_Interceptor", InterceptorMethodsAttributes, CallingConventions.HasThis, typeof(IInterceptor), new System.Type[0]);
getterMethod.SetImplementationFlags(MethodImplAttributes.Managed | MethodImplAttributes.IL);
ILGenerator IL = getterMethod.GetILGenerator();
// This is equivalent to:
// get { return __interceptor;
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Ldfld, field);
IL.Emit(OpCodes.Ret);
// Implement the setter
MethodBuilder setterMethod = typeBuilder.DefineMethod("set_Interceptor", InterceptorMethodsAttributes, CallingConventions.HasThis, typeof (void), new[] {typeof (IInterceptor)});
setterMethod.SetImplementationFlags(MethodImplAttributes.Managed | MethodImplAttributes.IL);
IL = setterMethod.GetILGenerator();
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Ldarg_1);
IL.Emit(OpCodes.Stfld, field);
IL.Emit(OpCodes.Ret);
MethodInfo originalSetter = typeof (IProxy).GetMethod("set_Interceptor");
MethodInfo originalGetter = typeof (IProxy).GetMethod("get_Interceptor");
typeBuilder.DefineMethodOverride(setterMethod, originalSetter);
typeBuilder.DefineMethodOverride(getterMethod, originalGetter);
}
示例4: CreateProperty
private static void CreateProperty(TypeBuilder tb, string propertyName, Type propertyType)
{
FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName,
propertyType,
FieldAttributes.Private);
PropertyBuilder propertyBuilder =
tb.DefineProperty(
propertyName, System.Reflection.PropertyAttributes.HasDefault, propertyType, null);
MethodBuilder getPropMthdBldr =
tb.DefineMethod("get_" + propertyName,
MethodAttributes.Public |
MethodAttributes.SpecialName |
MethodAttributes.HideBySig,
propertyType, Type.EmptyTypes);
ILGenerator getIL = getPropMthdBldr.GetILGenerator();
getIL.Emit(OpCodes.Ldarg_0);
getIL.Emit(OpCodes.Ldfld, fieldBuilder);
getIL.Emit(OpCodes.Ret);
MethodBuilder setPropMthdBldr =
tb.DefineMethod("set_" + propertyName,
MethodAttributes.Public |
MethodAttributes.SpecialName |
MethodAttributes.HideBySig,
null, new Type[] { propertyType });
ILGenerator setIL = setPropMthdBldr.GetILGenerator();
setIL.Emit(OpCodes.Ldarg_0);
setIL.Emit(OpCodes.Ldarg_1);
setIL.Emit(OpCodes.Stfld, fieldBuilder);
setIL.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getPropMthdBldr);
propertyBuilder.SetSetMethod(setPropMthdBldr);
}
示例5: Extend
public override void Extend(Type baseType, TypeBuilder typeBuilder)
{
//build field
FieldBuilder field = typeBuilder.DefineField(FieldName, Type, FieldAttributes.Private);
//define property
PropertyBuilder property = typeBuilder.DefineProperty(Name, PropertyAttributes.None, Type, null);
//build setter
MethodBuilder setter = typeBuilder.DefineMethod("set_" + Name, MethodAttributes.Public | MethodAttributes.Virtual, null, new Type[] { Type });
ILGenerator setterILG = setter.GetILGenerator();
setterILG.Emit(OpCodes.Ldarg_0);
setterILG.Emit(OpCodes.Ldarg_1);
setterILG.Emit(OpCodes.Stfld, field);
setterILG.Emit(OpCodes.Ret);
property.SetSetMethod(setter);
//build getter
MethodBuilder getter = typeBuilder.DefineMethod("get_" + Name, MethodAttributes.Public | MethodAttributes.Virtual, Type, Type.EmptyTypes);
ILGenerator getterILG = getter.GetILGenerator();
getterILG.Emit(OpCodes.Ldarg_0);
getterILG.Emit(OpCodes.Ldfld, field);
getterILG.Emit(OpCodes.Ret);
property.SetGetMethod(getter);
}
示例6: BuildStringProperty
private static void BuildStringProperty(TypeBuilder typeBuilder, string property)
{
//implement properties from interface
FieldBuilder fieldBuilder = typeBuilder.DefineField(property.ToLower(), typeof(string), FieldAttributes.Private);
PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(property, PropertyAttributes.HasDefault, typeof(string), null);
const MethodAttributes getSetAttributes = MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig | MethodAttributes.Virtual;
//define 'get' accessor
MethodBuilder getPropertyBuilder = typeBuilder.DefineMethod("get_" + property, getSetAttributes, typeof(string),
Type.EmptyTypes);
//create IL code for get
ILGenerator genusGetIL = getPropertyBuilder.GetILGenerator();
genusGetIL.Emit(OpCodes.Ldarg_0);
genusGetIL.Emit(OpCodes.Ldfld, fieldBuilder);
genusGetIL.Emit(OpCodes.Ret);
//define 'set' accessor
MethodBuilder setPropertyBuilder = typeBuilder.DefineMethod("set_" + property, getSetAttributes, null,
new[] { typeof(String) });
//create IL code for set
ILGenerator genusSetIL = setPropertyBuilder.GetILGenerator();
genusSetIL.Emit(OpCodes.Ldarg_0);
genusSetIL.Emit(OpCodes.Ldarg_1);
genusSetIL.Emit(OpCodes.Stfld, fieldBuilder);
genusSetIL.Emit(OpCodes.Ret);
//map get and set to property 'methods'
propertyBuilder.SetGetMethod(getPropertyBuilder);
propertyBuilder.SetSetMethod(setPropertyBuilder);
}
示例7: 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);
}
示例8: CreateReadOnlyLazyProperty
internal static void CreateReadOnlyLazyProperty(TypeBuilder tb, PropertyInfo property, ILGenerator ctor, bool isInterfaceImplementation = true)
{
var propertyName = property.Name;
var propertyType = property.PropertyType;
//backing field
var lazyType = typeof(Lazy<>).MakeGenericType(propertyType);
var fb = tb.DefineField("_backing_" + propertyName, lazyType, FieldAttributes.Private);
//property
var pb = tb.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
//getter
var getterAttributes = isInterfaceImplementation ? InterfaceImplementationAttributes : OverrideAttributes;
var getMethod = tb.DefineMethod("get_" + propertyName, getterAttributes, propertyType, Type.EmptyTypes);
var getIL = getMethod.GetILGenerator();
getIL.DeclareLocal(propertyType);
getIL.Emit(OpCodes.Nop);
getIL.Emit(OpCodes.Ldarg_0);
getIL.Emit(OpCodes.Ldfld, fb);
getIL.EmitCall(OpCodes.Callvirt, lazyType.GetProperty("Value").GetGetMethod(), null);
getIL.Emit(OpCodes.Stloc_0);
getIL.Emit(OpCodes.Nop); //originally a br.s to the next line
getIL.Emit(OpCodes.Ldloc_0);
getIL.Emit(OpCodes.Ret);
//combine getter and property
pb.SetGetMethod(getMethod);
//initialize Lazy<T> in .ctor
ctor.Emit(OpCodes.Ldarg_0);
ctor.Emit(OpCodes.Newobj, lazyType.GetConstructor(Type.EmptyTypes));
ctor.Emit(OpCodes.Stfld, fb);
}
示例9: CreateDynamicMethod
public void CreateDynamicMethod()
{
#if SAVE_ASSEMBLY
if (_assemblyBuilder == null)
{
AssemblyName assemblyName = new AssemblyName("ExpressionAssembly");
_assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave, "I:\\Trash");
_moduleBuilder = _assemblyBuilder.DefineDynamicModule("ExpressionModule", "ExpressionModule.module");
}
string typeName = String.Format("Expression{0}", _typeCount);
_typeCount++;
_typeBuilder = _moduleBuilder.DefineType(typeName, TypeAttributes.Class | TypeAttributes.Public);
FieldBuilder filedBuilder = _typeBuilder.DefineField("Source", typeof(string), FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal);
filedBuilder.SetConstant(_source);
_methodBuilder = _typeBuilder.DefineMethod("Evaluate", MethodAttributes.Public | MethodAttributes.Static, typeof(object), new Type[] { typeof(object[]) });
_ilGenerator = _methodBuilder.GetILGenerator();
#else
_dynamicMethod = new DynamicMethod("Expression", typeof(object), new Type[] { typeof(object[]) }, GetType().Module);
_ilGenerator = _dynamicMethod.GetILGenerator();
#endif
}
示例10: 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;
}
示例11: 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);
}
示例12: DefineDelegateFieldAndOverride
static void DefineDelegateFieldAndOverride(TypeBuilder tb, int numArgs)
{
Type fieldType = FuncTypeHelpers.GetFFuncType(numArgs);
string fieldName = "_fn" + numArgs;
FieldBuilder fb = tb.DefineField(fieldName, fieldType, FieldAttributes.Public);
MethodBuilder mb = tb.DefineMethod("invoke", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual, typeof(object), CreateObjectTypeArray(numArgs));
ILGenerator gen = mb.GetILGenerator();
Label eqLabel = gen.DefineLabel();
// this._fni == null ?
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldfld, fb);
gen.Emit(OpCodes.Ldnull);
gen.Emit(OpCodes.Beq, eqLabel);
//Not equal to Null, invoke it.
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldfld, fb);
for (int i = 0; i < numArgs; i++)
gen.Emit(OpCodes.Ldarg, i+1);
gen.Emit(OpCodes.Call,fb.FieldType.GetMethod("Invoke"));
gen.Emit(OpCodes.Ret);
gen.MarkLabel(eqLabel);
// Equal to Null: throw WrongArityException
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Call, Method_AFn_WrongArityException);
gen.Emit(OpCodes.Throw);
}
示例13:
private static FieldBuilder CréeAttributs(TypeBuilder constructeurDeType, DataColumn colonne)
{
FieldBuilder champBldr = null;
PropertyBuilder constructeurPropriété;
#region Création de l'attribut
champBldr = constructeurDeType.DefineField(colonne.ColumnName.ToLower(), colonne.DataType, FieldAttributes.Private);
#endregion
#region Création de la Propriété associée
constructeurPropriété = constructeurDeType.DefineProperty(colonne.ColumnName, System.Reflection.PropertyAttributes.HasDefault, colonne.DataType, null);
// Choix des attributs
MethodAttributes attrGetSet = MethodAttributes.Public |
MethodAttributes.SpecialName | MethodAttributes.HideBySig;
#region Définition du "getter".
MethodBuilder constructeurGet = constructeurDeType.DefineMethod("get_" + colonne.ColumnName, attrGetSet, colonne.DataType, Type.EmptyTypes);
ILGenerator champGetIL = constructeurGet.GetILGenerator();
champGetIL.Emit(OpCodes.Ldarg_0);
champGetIL.Emit(OpCodes.Ldfld, champBldr);
champGetIL.Emit(OpCodes.Ret);
#endregion
#region Définition du "setter".
MethodBuilder constructeurSet = constructeurDeType.DefineMethod("set_" + colonne.ColumnName, attrGetSet, null, new Type[] { colonne.DataType });
ILGenerator champSetIL = constructeurSet.GetILGenerator();
champSetIL.Emit(OpCodes.Ldarg_0);
champSetIL.Emit(OpCodes.Ldarg_1);
champSetIL.Emit(OpCodes.Stfld, champBldr);
champSetIL.Emit(OpCodes.Ret);
#endregion
#region Association des accesseurs à la propriété
constructeurPropriété.SetGetMethod(constructeurGet);
constructeurPropriété.SetSetMethod(constructeurSet);
#endregion
#endregion
return champBldr;
}
示例14: 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;
}
示例15: BuildProperty
private static void BuildProperty(TypeBuilder typeBuilder, string name, Type type)
{
FieldBuilder field = typeBuilder.DefineField("m" + name, type, FieldAttributes.Private);
PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(name, PropertyAttributes.None, type, null);
MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Virtual;
MethodBuilder getter = typeBuilder.DefineMethod("get_" + name, getSetAttr, type, Type.EmptyTypes);
ILGenerator getIL = getter.GetILGenerator();
getIL.Emit(OpCodes.Ldarg_0);
getIL.Emit(OpCodes.Ldfld, field);
getIL.Emit(OpCodes.Ret);
MethodBuilder setter = typeBuilder.DefineMethod("set_" + name, getSetAttr, null, new Type[] { type });
ILGenerator setIL = setter.GetILGenerator();
setIL.Emit(OpCodes.Ldarg_0);
setIL.Emit(OpCodes.Ldarg_1);
setIL.Emit(OpCodes.Stfld, field);
setIL.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getter);
propertyBuilder.SetSetMethod(setter);
}