本文整理汇总了C#中System.Reflection.Emit.TypeBuilder类的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder类的具体用法?C# TypeBuilder怎么用?C# TypeBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeBuilder类属于System.Reflection.Emit命名空间,在下文中一共展示了TypeBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateProxiedMethod
public void CreateProxiedMethod(FieldInfo field, MethodInfo method, TypeBuilder typeBuilder)
{
const MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.Virtual;
ParameterInfo[] parameters = method.GetParameters();
MethodBuilder methodBuilder = typeBuilder.DefineMethod(method.Name, methodAttributes,
CallingConventions.HasThis, method.ReturnType,
parameters.Select(param => param.ParameterType).ToArray());
System.Type[] typeArgs = method.GetGenericArguments();
if (typeArgs.Length > 0)
{
var typeNames = new List<string>();
for (int index = 0; index < typeArgs.Length; index++)
{
typeNames.Add(string.Format("T{0}", index));
}
methodBuilder.DefineGenericParameters(typeNames.ToArray());
}
ILGenerator IL = methodBuilder.GetILGenerator();
Debug.Assert(MethodBodyEmitter != null);
MethodBodyEmitter.EmitMethodBody(IL, method, field);
}
示例2: 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);
}
示例3: EventBuilder
#pragma warning restore 169, 414
internal EventBuilder (TypeBuilder tb, string eventName, EventAttributes eventAttrs, Type eventType) {
name = eventName;
attrs = eventAttrs;
type = eventType;
typeb = tb;
table_idx = get_next_table_index (this, 0x14, true);
}
示例4: MethodBuilder
#pragma warning restore 169, 414
internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt)
{
this.name = name;
this.attrs = attributes;
this.call_conv = callingConvention;
this.rtype = returnType;
this.returnModReq = returnModReq;
this.returnModOpt = returnModOpt;
this.paramModReq = paramModReq;
this.paramModOpt = paramModOpt;
// The MSDN docs does not specify this, but the MS MethodBuilder
// appends a HasThis flag if the method is not static
if ((attributes & MethodAttributes.Static) == 0)
this.call_conv |= CallingConventions.HasThis;
if (parameterTypes != null) {
for (int i = 0; i < parameterTypes.Length; ++i)
if (parameterTypes [i] == null)
throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
this.parameters = new Type [parameterTypes.Length];
System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
}
type = tb;
table_idx = get_next_table_index (this, 0x06, true);
((ModuleBuilder)tb.Module).RegisterToken (this, GetToken ().Token);
}
示例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: GenerateInvocation
/// <summary>
/// Creates a <see cref="MethodBuilder"/> and implements a default wrapper.
/// </summary>
/// <param name="owner">The type that will own this method.</param>
/// <param name="interfaceType">Type of interface implemented by the <paramref name="owner"/>.</param>
/// <param name="overrideMethod">Method to override.</param>
/// <param name="fieldBuilders">Fields specified by the <see paramref="owner"/>.</param>
/// <returns>MethodBuilder with an already implemented wrapper.</returns>
public MethodBuilder GenerateInvocation(TypeBuilder owner, Type interfaceType, MethodInfo overrideMethod, IEnumerable<FieldBuilder> fieldBuilders)
{
var result = owner.DefineMethod
(
overrideMethod.Name,
MethodAttributes.Private | MethodAttributes.Virtual | MethodAttributes.Final |
MethodAttributes.HideBySig | MethodAttributes.NewSlot,
overrideMethod.ReturnType,
overrideMethod.GetParameters().OrderBy(p => p.Position).Select(t => t.ParameterType).ToArray()
);
result.SetImplementationFlags(MethodImplAttributes.AggressiveInlining);
var generator = result.GetILGenerator();
var fieldName = LibraryInterfaceMapper.GetFieldNameForMethodInfo(overrideMethod);
var field = fieldBuilders.First(f => f.Name == fieldName);
var parameters = overrideMethod.GetParameters();
OnInvokeBegin(owner, interfaceType, generator, overrideMethod);
generator.Emit(OpCodes.Ldarg_0); // this
generator.Emit(OpCodes.Ldfld, field); // MethodNameProc _glMethodName. Initialized by constructor.
foreach (var item in parameters.Where(p => !p.IsRetval).Select((p, i) => new { Type = p, Index = i }))
{
generator.Emit(OpCodes.Ldarg, item.Index + 1);
}
generator.EmitCall(OpCodes.Callvirt, field.FieldType.GetMethod("Invoke"), null);
OnInvokeEnd(owner, interfaceType, generator, overrideMethod);
generator.Emit(OpCodes.Ret);
owner.DefineMethodOverride(result, overrideMethod);
return result;
}
示例7: 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;
}
示例8: IntroductionProxyMethodBuilder
/// <summary>
/// Creates a new instance of the method builder.
/// </summary>
/// <param name="typeBuilder">The type builder to use.</param>
/// <param name="aopProxyGenerator">
/// The <see cref="IAopProxyTypeGenerator"/> implementation to use.
/// </param>
/// <param name="targetMethods">
///
/// </param>
/// <param name="index">index of the introduction to delegate call to</param>
public IntroductionProxyMethodBuilder(
TypeBuilder typeBuilder, IAopProxyTypeGenerator aopProxyGenerator,
IDictionary targetMethods, int index)
: base(typeBuilder, aopProxyGenerator, true, targetMethods)
{
this.index = index;
}
示例9: DefineInterface
public static void DefineInterface(TypeBuilder typeBuilder, Type interfaceType, Type implementType,
Action<ILGenerator, MethodInfo, MethodInfo> ilGenerator)
{
var proxyMethodBuilder = new ProxyMethodBuilder(typeBuilder);
InterfaceMapping mapping = implementType.GetInterfaceMap(interfaceType);
for (int i = 0; i < mapping.InterfaceMethods.Length; i++)
mapping.TargetMethods[i] = proxyMethodBuilder.DefineMethod(mapping.InterfaceMethods[i],
il => ilGenerator(il, mapping.InterfaceMethods[i], mapping.TargetMethods[i]));
foreach (PropertyInfo propertyInfo in interfaceType.GetProperties())
{
MethodBuilder getMethodBuilder = null;
MethodInfo getMethodInfo = propertyInfo.GetGetMethod();
if (getMethodInfo != null)
getMethodBuilder = (MethodBuilder)GetTargetMethodInfo(ref mapping, getMethodInfo);
MethodBuilder setMethodBuilder = null;
MethodInfo setMethodInfo = propertyInfo.GetSetMethod();
if (setMethodInfo != null)
setMethodBuilder = (MethodBuilder)GetTargetMethodInfo(ref mapping, setMethodInfo);
ProxyBuilderHelper.DefineProperty(typeBuilder, propertyInfo, getMethodBuilder, setMethodBuilder);
}
foreach (EventInfo eventInfo in interfaceType.GetEvents())
{
var addMethodBuilder = (MethodBuilder)GetTargetMethodInfo(ref mapping, eventInfo.GetAddMethod());
var removeMethodBuilder = (MethodBuilder)GetTargetMethodInfo(ref mapping, eventInfo.GetRemoveMethod());
ProxyBuilderHelper.DefineEvent(typeBuilder, eventInfo, addMethodBuilder, removeMethodBuilder);
}
}
示例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: ImplementProxy
public void ImplementProxy(TypeBuilder typeBuilder)
{
typeBuilder.AddInterfaceImplementation(typeof (IProxy));
InterceptorField = typeBuilder
.DefineField("__interceptor",
typeof (Proxy.DynamicProxy.IInterceptor),
FieldAttributes.Private);
var getterMethod = typeBuilder
.DefineMethod("get_Interceptor",
InterceptorMethodsAttributes,
CallingConventions.HasThis,
typeof (Proxy.DynamicProxy.IInterceptor),
new System.Type[0]);
getterMethod.SetImplementationFlags(MethodImplAttributes.IL);
var il = getterMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, InterceptorField);
il.Emit(OpCodes.Ret);
var setterMethod = typeBuilder
.DefineMethod("set_Interceptor",
InterceptorMethodsAttributes,
CallingConventions.HasThis,
typeof (void),
new[] {typeof (Proxy.DynamicProxy.IInterceptor)});
setterMethod.SetImplementationFlags(MethodImplAttributes.IL);
il = setterMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Stfld, InterceptorField);
il.Emit(OpCodes.Ret);
var originalSetter = typeof (IProxy).GetMethod("set_Interceptor");
var originalGetter = typeof (IProxy).GetMethod("get_Interceptor");
typeBuilder.DefineMethodOverride(setterMethod, originalSetter);
typeBuilder.DefineMethodOverride(getterMethod, originalGetter);
}
示例12: InterfaceImplementation
public InterfaceImplementation(
TypeBuilder typeBuilder,
Type @interface,
FieldBuilder proxyInterceptionPipelineField,
bool explicitImplementation)
: this(typeBuilder, @interface, proxyInterceptionPipelineField, explicitImplementation, null)
{ }
示例13: 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;
}
示例14: 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);
}
示例15: CreateProperty
private static void CreateProperty(string name, TypeBuilder tb, MethodInfo bgcmi, MethodInfo bscmi)
{
// define getter
var gmb = tb.DefineMethod("get_" + name,
MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public,
typeof(string),
null);
var g = gmb.GetILGenerator();
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldstr, name);
g.Emit(OpCodes.Call, bgcmi);
g.Emit(OpCodes.Ret);
// define setter
var smb = tb.DefineMethod("set_" + name,
MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public,
null,
new Type[] { typeof(string) });
g = smb.GetILGenerator();
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldstr, name);
g.Emit(OpCodes.Ldarg_1);
g.Emit(OpCodes.Call, bscmi);
g.Emit(OpCodes.Ret);
// define properties
var pb = tb.DefineProperty(name, PropertyAttributes.None, typeof(string), null);
pb.SetGetMethod(gmb);
pb.SetSetMethod(smb);
}