本文整理汇总了C#中System.Reflection.Emit.TypeBuilder.DefineProperty方法的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder.DefineProperty方法的具体用法?C# TypeBuilder.DefineProperty怎么用?C# TypeBuilder.DefineProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefineProperty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DefineProperty
internal static PropertyBuilder DefineProperty(TypeBuilder typeBuilder,
string name, Type type)
{
return typeBuilder.DefineProperty(name,
PropertyAttributes.HasDefault,
type, null);
}
示例2: 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);
}
示例3:
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;
}
示例4: MakeProperty
private static void MakeProperty(TypeBuilder typeBuilder, string name, Type type)
{
var fieldBuilder = typeBuilder.DefineField("_" + name, type, FieldAttributes.Private);
var propertyBuilder = typeBuilder.DefineProperty(name, PropertyAttributes.None, CallingConventions.HasThis, type,
null);
var getSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
var getter = typeBuilder.DefineMethod("get_" + name, getSetAttr, type, Type.EmptyTypes);
var getIL = getter.GetILGenerator();
getIL.Emit(OpCodes.Ldarg_0);
getIL.Emit(OpCodes.Ldfld, fieldBuilder);
getIL.Emit(OpCodes.Ret);
var setter = typeBuilder.DefineMethod("set_" + name, getSetAttr, null, new[] {type});
var setIL = setter.GetILGenerator();
setIL.Emit(OpCodes.Ldarg_0);
setIL.Emit(OpCodes.Ldarg_1);
setIL.Emit(OpCodes.Stfld, fieldBuilder);
setIL.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getter);
propertyBuilder.SetSetMethod(setter);
}
示例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: 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: 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;
}
示例8: 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);
}
示例9: CreateProperty
private static void CreateProperty(TypeBuilder tb, string propertyName, Type propertyType)
{
FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName, propertyType, FieldAttributes.Private);
PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName, 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[] { propertyType });
ILGenerator setIl = setPropMthdBldr.GetILGenerator();
Label modifyProperty = setIl.DefineLabel();
Label exitSet = setIl.DefineLabel();
setIl.MarkLabel(modifyProperty);
setIl.Emit(OpCodes.Ldarg_0);
setIl.Emit(OpCodes.Ldarg_1);
setIl.Emit(OpCodes.Stfld, fieldBuilder);
setIl.Emit(OpCodes.Nop);
setIl.MarkLabel(exitSet);
setIl.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getPropMthdBldr);
propertyBuilder.SetSetMethod(setPropMthdBldr);
}
示例10: 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;
}
示例11: CreatePropertyProxyBuilder
private PropertyBuilder CreatePropertyProxyBuilder(TypeBuilder typeBuilder, PropertyInfo contractProperty)
{
var builder = typeBuilder.DefineProperty(contractProperty.Name, contractProperty.Attributes,
contractProperty.PropertyType, null);
return builder;
}
示例12: createProperty
/// <summary>
/// 动态创建类的属性
/// </summary>
/// <param name="tb">承载该属性的类型</param>
/// <param name="properityName">属性名,首字母应大写</param>
/// <param name="properityType">属性数据类型</param>
public static FieldBuilder createProperty(TypeBuilder tb, string propertyName, Type propertyType)
{
//定义属性对应的私有字段
FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName, propertyType, FieldAttributes.Private);
//定义属性
PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);
//定义与属性相关的get方法
MethodBuilder getPropMthdBldr = tb.DefineMethod("get_" + propertyName,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
propertyType,
Type.EmptyTypes);
ILGenerator getIL = getPropMthdBldr.GetILGenerator();//获取il 生成器
getIL.Emit(OpCodes.Ldarg_0);
getIL.Emit(OpCodes.Ldfld, fieldBuilder);
getIL.Emit(OpCodes.Ret);
//定义与属性相关的set方法
MethodBuilder setPropMthdBldr = tb.DefineMethod("set_" + propertyName,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
null, new Type[] { propertyType });
ILGenerator setIL = setPropMthdBldr.GetILGenerator();
/*
* OpCodes.Ldarg_0:Ldarg是加载方法参数的意思。这里Ldarg_0事实上是对当前对象的引用即this。
* 因为类的实例方法(非静态方法)在调用时,this 是会作为第一个参数传入的。
*/
setIL.Emit(OpCodes.Ldarg_0);
setIL.Emit(OpCodes.Ldarg_1);//OpCodes.Ldarg_1:加载参数列表的第一个参数了。
setIL.Emit(OpCodes.Stfld, fieldBuilder);//OpCodes.Stfld:用新值替换在对象引用或指针的字段中存储的值。
setIL.Emit(OpCodes.Ret);
//把get/set方法和属性联系起来
propertyBuilder.SetGetMethod(getPropMthdBldr);
propertyBuilder.SetSetMethod(setPropMthdBldr);
return fieldBuilder;
}
示例13: 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);
}
示例14: CreateProperty
private void CreateProperty(TypeBuilder a_TypeBuilder, string a_PropertyName, Type a_PropertyType)
{
FieldBuilder fieldBuilder = a_TypeBuilder.DefineField("_" + a_PropertyName, a_PropertyType, FieldAttributes.Private);
PropertyBuilder propertyBuilder = a_TypeBuilder.DefineProperty(a_PropertyName, PropertyAttributes.HasDefault, a_PropertyType, null);
MethodBuilder getPropMthdBldr = a_TypeBuilder.DefineMethod("get_" + a_PropertyName,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
a_PropertyType, Type.EmptyTypes);
ILGenerator getIl = getPropMthdBldr.GetILGenerator();
// Load the current instance onto the stack
getIl.Emit(OpCodes.Ldarg_0);
// Loads the value of the field specified as parameter from the object on the stack
getIl.Emit(OpCodes.Ldfld, fieldBuilder);
// Returns the method and pushes the get value onto the caller's stack
getIl.Emit(OpCodes.Ret);
MethodBuilder setPropMthdBldr = a_TypeBuilder.DefineMethod("set_" + a_PropertyName,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
null, new[] { a_PropertyType });
ILGenerator setIl = setPropMthdBldr.GetILGenerator();
// Load the current instance onto the stack
setIl.Emit(OpCodes.Ldarg_0);
// Load the first argument, which is the assignment value onto the stack
setIl.Emit(OpCodes.Ldarg_1);
// Store the value currently on the stack in the field specified as parameter in the object on the stack
setIl.Emit(OpCodes.Stfld, fieldBuilder);
// Returns the setter
setIl.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getPropMthdBldr);
propertyBuilder.SetSetMethod(setPropMthdBldr);
}
示例15: 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);
}