本文整理汇总了C#中System.Reflection.Emit.TypeBuilder.AddInterfaceImplementation方法的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder.AddInterfaceImplementation方法的具体用法?C# TypeBuilder.AddInterfaceImplementation怎么用?C# TypeBuilder.AddInterfaceImplementation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.AddInterfaceImplementation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: GenerateType
/// <summary>
/// Generates the type.
/// </summary>
/// <returns></returns>
public Type GenerateType()
{
if (_generatedType == null)
{
// Setup the assembly and type builder
AssemblyName assemblyName = new AssemblyName("Mock.Generated-" + _interfaceToImplement.Name);
AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
_typeBuilder = moduleBuilder.DefineType("Mock.Generated.Mock" + _interfaceToImplement.Name, TypeAttributes.Public);
_typeBuilder.AddInterfaceImplementation(_interfaceToImplement);
// Generate the body of the type
this.CreateFields();
this.CreateConstructor();
foreach (MethodInfo method in _interfaceToImplement.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(m => !m.Name.StartsWith("get_")))
{
this.ImplementMethod(method);
}
foreach (PropertyInfo property in _interfaceToImplement.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
this.ImplementProperty(property);
}
// Store the generated type for use again
_generatedType = _typeBuilder.CreateType();
}
return _generatedType;
}
示例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: BuildType
// 构造类型
void BuildType()
{
_typeBuilder = _moduleBuilder.DefineType(string.Format(TypeNameFormat, _realProxyType.Name),
TypeAttributes.Public | TypeAttributes.Sealed);
_typeBuilder.AddInterfaceImplementation(_interfaceType);
//if ( _baseType!=null)
// _typeBuilder.SetParent(_baseType);
}
示例5: ImplementIInterceptingProxy
internal static FieldBuilder ImplementIInterceptingProxy(TypeBuilder typeBuilder)
{
typeBuilder.AddInterfaceImplementation(typeof(IInterceptingProxy));
FieldBuilder proxyInterceptorPipelineField =
typeBuilder.DefineField(
"pipeline",
typeof(InterceptionBehaviorPipeline),
FieldAttributes.Private | FieldAttributes.InitOnly);
ImplementAddInterceptionBehavior(typeBuilder, proxyInterceptorPipelineField);
return proxyInterceptorPipelineField;
}
示例6: CreateType
public Type CreateType(String typeName, Type parent)
{
_typeBuilder = _moduleBuilder.DefineType(typeName,
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass |
TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout,
parent, new[] { typeof(IProxy) });
_typeBuilder.AddInterfaceImplementation(typeof(IProxy));
ImplementProxyInterface(parent);
var propHandlerDic = OverrideProperties(parent);
GenerateConstructor(propHandlerDic);
return _typeBuilder.CreateType();
}
示例7: Build
public void Build(TypeBuilder builder, Type contractType)
{
builder.AddInterfaceImplementation(typeof(IAutoNotifyPropertyChanged));
FieldBuilder addPropertyChangedField = builder.DefineField("PropertyChanged", typeof (PropertyChangingEventHandler), FieldAttributes.Private);
MethodBuilder addMethod = DefineAddOnMethod(builder, addPropertyChangedField);
MethodBuilder removeMethod = DefineRemoveOnMethod(builder, addPropertyChangedField);
MethodBuilder notifyPropertyChangedMethod = DefineRaiseMethod(builder, addPropertyChangedField);
EventBuilder pcevent = builder.DefineEvent("PropertyChanged", EventAttributes.None, typeof(PropertyChangedEventHandler));
pcevent.SetRaiseMethod(notifyPropertyChangedMethod);
pcevent.SetAddOnMethod(addMethod);
pcevent.SetRemoveOnMethod(removeMethod);
}
示例8: Implement
public static void Implement (TypeBuilder typeB, Type iface)
{
typeB.AddInterfaceImplementation (iface);
foreach (MethodInfo declMethod in iface.GetMethods ()) {
MethodBuilder method_builder = typeB.DefineMethod (declMethod.Name, MethodAttributes.Public | MethodAttributes.Virtual, declMethod.ReturnType, Mapper.GetTypes (ArgDirection.In, declMethod.GetParameters ()));
ILGenerator ilg = method_builder.GetILGenerator ();
//Mapper.GetTypes (ArgDirection.In, declMethod.GetParameters ())
ParameterInfo[] delegateParms = declMethod.GetParameters ();
Type[] hookupParms = new Type[delegateParms.Length+1];
hookupParms[0] = typeof (BusObject);
for (int i = 0; i < delegateParms.Length ; i++)
hookupParms[i+1] = delegateParms[i].ParameterType;
GenHookupMethod (ilg, declMethod, sendMethodCallMethod, Mapper.GetInterfaceName (iface), declMethod.Name, hookupParms);
typeB.DefineMethodOverride (method_builder, declMethod);
}
}
示例9: ImplementProxy
public void ImplementProxy(TypeBuilder b)
{
var dynamicProxyType = typeof(IDynamicProxy);
b.AddInterfaceImplementation(dynamicProxyType);
_fieldBuilder = b.DefineField("__interceptor", typeof(IMethodInterceptor), FieldAttributes.Private);
const MethodAttributes attributes = MethodAttributes.SpecialName | MethodAttributes.VtableLayoutMask | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Public;
var getMethodBuilder = b.DefineMethod("get_Interceptor", attributes, CallingConventions.HasThis, typeof(IMethodInterceptor), new Type[0]);
getMethodBuilder.SetImplementationFlags(MethodImplAttributes.IL);
var w = getMethodBuilder.GetILGenerator();
w.Emit(OpCodes.Ldarg_0);
w.Emit(OpCodes.Ldfld, _fieldBuilder);
w.Emit(OpCodes.Ret);
var setMethodBuilder = b.DefineMethod("set_Interceptor", attributes, CallingConventions.HasThis, typeof(void), new Type[] { typeof(IMethodInterceptor) });
setMethodBuilder.SetImplementationFlags(MethodImplAttributes.IL);
w = setMethodBuilder.GetILGenerator();
w.Emit(OpCodes.Ldarg_0);
w.Emit(OpCodes.Ldarg_1);
w.Emit(OpCodes.Stfld, _fieldBuilder);
w.Emit(OpCodes.Ret);
b.DefineMethodOverride(setMethodBuilder, dynamicProxyType.GetMethod("set_Interceptor"));
b.DefineMethodOverride(getMethodBuilder, dynamicProxyType.GetMethod("get_Interceptor"));
}
示例10: Implement
public static void Implement (TypeBuilder typeB, Type iface)
{
typeB.AddInterfaceImplementation (iface);
foreach (MethodInfo declMethod in iface.GetMethods ()) {
ParameterInfo[] parms = declMethod.GetParameters ();
Type[] parmTypes = new Type[parms.Length];
for (int i = 0 ; i < parms.Length ; i++)
parmTypes[i] = parms[i].ParameterType;
MethodAttributes attrs = declMethod.Attributes ^ MethodAttributes.Abstract;
MethodBuilder method_builder = typeB.DefineMethod (declMethod.Name, attrs, declMethod.ReturnType, parmTypes);
typeB.DefineMethodOverride (method_builder, declMethod);
//define in/out/ref/name for each of the parameters
for (int i = 0; i < parms.Length ; i++)
method_builder.DefineParameter (i, parms[i].Attributes, parms[i].Name);
ILGenerator ilg = method_builder.GetILGenerator ();
GenHookupMethod (ilg, declMethod, sendMethodCallMethod, Mapper.GetInterfaceName (iface), declMethod.Name);
}
}
示例11: Generate
void Generate(ref TypeBuilder tBuilder, Class c)
{
// parent and interfaces
foreach (Class baseType in c.BaseTypes) {
Type t = GetType(baseType);
if (t == null) {
// parent, or interface is not
// public namespace, or inner
Console.WriteLine("WARNING: skipped base type {0} for {1}",
baseType, tBuilder.FullName);
continue;
}
if (baseType.IsInterface) {
tBuilder.AddInterfaceImplementation(t);
} else {
tBuilder.SetParent(t);
}
}
}
示例12: ImplementInterface
/// <summary>
/// Implements an interface.
/// </summary>
/// <remarks>
/// Generates proxy methods that belongs to the interface
/// using the specified <paramref name="proxyMethodBuilder"/>.
/// </remarks>
/// <param name="typeBuilder">The type builder to use.</param>
/// <param name="proxyMethodBuilder">
/// The <see cref="IProxyMethodBuilder"/> implementation to use
/// </param>
/// <param name="intf">The interface to implement.</param>
/// <param name="targetType">
/// The <see cref="System.Type"/> of the target object.
/// </param>
/// <param name="proxyVirtualMethods">
/// <see langword="false"/> if target virtual methods should not be proxied;
/// otherwise <see langword="true"/>.
/// </param>
protected virtual void ImplementInterface(TypeBuilder typeBuilder,
IProxyMethodBuilder proxyMethodBuilder, Type intf,
Type targetType, bool proxyVirtualMethods)
{
Dictionary<string, MethodBuilder> methodMap = new Dictionary<string, MethodBuilder>();
InterfaceMapping mapping = GetInterfaceMapping(targetType, intf);
typeBuilder.AddInterfaceImplementation(intf);
for (int i = 0; i < mapping.InterfaceMethods.Length; i++)
{
if (!proxyVirtualMethods &&
!mapping.TargetMethods[i].DeclaringType.IsInterface &&
mapping.TargetMethods[i].IsVirtual &&
!mapping.TargetMethods[i].IsFinal)
continue;
MethodBuilder methodBuilder = proxyMethodBuilder.BuildProxyMethod(
mapping.TargetMethods[i], mapping.InterfaceMethods[i]);
ApplyMethodAttributes(methodBuilder, mapping.TargetMethods[i]);
methodMap[mapping.InterfaceMethods[i].Name] = methodBuilder;
}
foreach (PropertyInfo property in intf.GetProperties())
{
ImplementProperty(typeBuilder, intf, property, methodMap);
}
foreach (EventInfo evt in intf.GetEvents())
{
ImplementEvent(typeBuilder, intf, evt, methodMap);
}
}
示例13: CompileAnaClass
private void CompileAnaClass()
{
AnaClass = utilityClass.DefineNestedType(Constants.AnaClassName,
TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NestedPublic,
null, TypeBuilder.EmptyTypes);
var anaClassGenericParameters = AnaClass.DefineGenericParameters(new[] { Constants.AnaClassGenericParameterName }.Concat(declaration.TypeParameters).ToArray());
if (declaration.TypeParameters.Any())
{
AnaClass.AddInterfaceImplementation(TypeBuilder.MakeGenericType(anaClassGenericParameters.Skip(1).ToArray()));
}
else
{
AnaClass.AddInterfaceImplementation(TypeBuilder);
}
var anaClassGeneratorType = typeof(IFunction<,>).MakeGenericType(anaClassGenericParameters[0],
FunctorTypeMapper.Map(declaration.Type, declaration.VariableName, anaClassGenericParameters[0], anaClassGenericParameters.Skip(1).ToArray(), runtimeContainer));
var seedField = AnaClass.DefineField(Constants.AnaClassSeedFieldName, anaClassGenericParameters[0], FieldAttributes.Private);
var generatorField = AnaClass.DefineField(Constants.AnaClassGeneratorFieldName, anaClassGeneratorType, FieldAttributes.Private);
AnaClassConstructor = AnaClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { anaClassGenericParameters[0], anaClassGeneratorType });
var ctorBody = AnaClassConstructor.GetILGenerator();
ctorBody.Emit(OpCodes.Ldarg_0);
ctorBody.Emit(OpCodes.Call, typeof(object).GetConstructors()[0]);
ctorBody.Emit(OpCodes.Ldarg_0);
ctorBody.Emit(OpCodes.Ldarg_1);
ctorBody.Emit(OpCodes.Stfld, seedField);
ctorBody.Emit(OpCodes.Ldarg_0);
ctorBody.Emit(OpCodes.Ldarg_2);
ctorBody.Emit(OpCodes.Stfld, generatorField);
ctorBody.Emit(OpCodes.Ret);
var apply = AnaClass.DefineMethod(Constants.ApplyMethodName, MethodAttributes.Public | MethodAttributes.Virtual);
var resultType = apply.DefineGenericParameters(Constants.GFixFunctionClassGenericParameterName)[0];
var applyFunctionGenericParameters = new[] { resultType }.Concat(anaClassGenericParameters.Skip(1)).ToArray();
apply.SetParameters(GreatestFixedPointFunction.MakeGenericType(applyFunctionGenericParameters));
apply.SetReturnType(resultType);
var applyMethodGenericClass = TypeBuilder.GetMethod(
GreatestFixedPointFunction.MakeGenericType(applyFunctionGenericParameters),
GreatestFixedPointFunctionApplyMethod);
var applyMethodGenericMethod = applyMethodGenericClass.MakeGenericMethod(anaClassGenericParameters[0]);
var applyBody = apply.GetILGenerator();
applyBody.Emit(OpCodes.Ldarg_1);
applyBody.Emit(OpCodes.Ldarg_0);
applyBody.Emit(OpCodes.Ldfld, seedField);
applyBody.Emit(OpCodes.Ldarg_0);
applyBody.Emit(OpCodes.Ldfld, generatorField);
applyBody.Emit(OpCodes.Callvirt, applyMethodGenericMethod);
applyBody.Emit(OpCodes.Ret);
}
示例14: Compile
public Type Compile(Type superType, Type stubType, IPersistentVector interfaces, bool onetimeUse, GenContext context)
{
if (_compiledType != null)
return _compiledType;
string publicTypeName = IsDefType || (_isStatic && Compiler.IsCompiling) ? InternalName : InternalName + "__" + RT.nextID();
_typeBuilder = context.AssemblyGen.DefinePublicType(publicTypeName, superType, true);
context = context.WithNewDynInitHelper().WithTypeBuilder(_typeBuilder);
Var.pushThreadBindings(RT.map(Compiler.CompilerContextVar, context));
try
{
if (interfaces != null)
{
for (int i = 0; i < interfaces.count(); i++)
_typeBuilder.AddInterfaceImplementation((Type)interfaces.nth(i));
}
ObjExpr.MarkAsSerializable(_typeBuilder);
GenInterface.SetCustomAttributes(_typeBuilder, _classMeta);
try
{
if (IsDefType)
{
Compiler.RegisterDuplicateType(_typeBuilder);
Var.pushThreadBindings(RT.map(
Compiler.CompileStubOrigClassVar, stubType
));
//,
//Compiler.COMPILE_STUB_CLASS, _baseType));
}
EmitConstantFieldDefs(_typeBuilder);
EmitKeywordCallsiteDefs(_typeBuilder);
DefineStaticConstructor(_typeBuilder);
if (SupportsMeta)
_metaField = _typeBuilder.DefineField("__meta", typeof(IPersistentMap), FieldAttributes.Public | FieldAttributes.InitOnly);
// If this IsDefType, then it has already emitted the closed-over fields on the base class.
if ( ! IsDefType )
EmitClosedOverFields(_typeBuilder);
EmitProtocolCallsites(_typeBuilder);
_ctorInfo = EmitConstructor(_typeBuilder, superType);
if (_altCtorDrops > 0)
EmitFieldOnlyConstructor(_typeBuilder, superType);
if (SupportsMeta)
{
EmitNonMetaConstructor(_typeBuilder, superType);
EmitMetaFunctions(_typeBuilder);
}
EmitStatics(_typeBuilder);
EmitMethods(_typeBuilder);
//if (KeywordCallsites.count() > 0)
// EmitSwapThunk(_typeBuilder);
_compiledType = _typeBuilder.CreateType();
if (context.DynInitHelper != null)
context.DynInitHelper.FinalizeType();
_ctorInfo = GetConstructorWithArgCount(_compiledType, CtorTypes().Length);
return _compiledType;
}
finally
{
if (IsDefType)
Var.popThreadBindings();
}
}
finally
{
Var.popThreadBindings();
}
}
示例15: GenerateFnClass
private void GenerateFnClass(IPersistentVector interfaces, GenContext context)
{
string publicTypeName = IsDefType || (IsStatic && Compiler.IsCompiling) ? _internalName : _internalName + "__" + RT.nextID();
//Console.WriteLine("DefFn {0}, {1}", publicTypeName, context.AssemblyBuilder.GetName().Name);
_typeBuilder = context.AssemblyGen.DefinePublicType(publicTypeName, _baseType, true);
for (int i = 0; i < interfaces.count(); i++)
_typeBuilder.AddInterfaceImplementation((Type)interfaces.nth(i));
MarkAsSerializable(_typeBuilder);
GenInterface.SetCustomAttributes(_typeBuilder, _classMeta);
GenerateStaticConstructor(_typeBuilder, _baseType, context.IsDebuggable);
_ctorInfo = GenerateConstructor(_typeBuilder, _baseType);
if (_altCtorDrops > 0)
GenerateFieldOnlyConstructor(_typeBuilder, _baseType);
if (SupportsMeta)
{
_nonmetaCtorInfo = GenerateNonMetaConstructor(_typeBuilder, _baseType);
}
GenerateMetaFunctions(_typeBuilder);
//GenerateReloadVarsMethod(_typeBuilder, context);
// The incoming context holds info on the containing function.
// That is the one that holds the closed-over variable values.
//GenContext newContext = CreateContext(context, _typeBuilder, _baseType);
//GenerateMethods(newContext);
GenerateStatics(context);
GenerateMethods(context);
}