本文整理汇总了C#中System.Reflection.Emit.TypeBuilder.DefineMethodOverride方法的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder.DefineMethodOverride方法的具体用法?C# TypeBuilder.DefineMethodOverride怎么用?C# TypeBuilder.DefineMethodOverride使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefineMethodOverride方法的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: 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: 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;
}
示例4: ImplementAddInterceptionBehavior
private static void ImplementAddInterceptionBehavior(TypeBuilder typeBuilder, FieldInfo proxyInterceptorPipelineField)
{
// Declaring method builder
// Method attributes
const MethodAttributes MethodAttributes = MethodAttributes.Private | MethodAttributes.Virtual
| MethodAttributes.Final | MethodAttributes.HideBySig
| MethodAttributes.NewSlot;
MethodBuilder methodBuilder =
typeBuilder.DefineMethod(
"Microsoft.Practices.Unity.InterceptionExtension.IInterceptingProxy.AddInterceptionBehavior",
MethodAttributes);
// Setting return type
methodBuilder.SetReturnType(typeof(void));
// Adding parameters
methodBuilder.SetParameters(typeof(IInterceptionBehavior));
// Parameter method
methodBuilder.DefineParameter(1, ParameterAttributes.None, "interceptor");
ILGenerator il = methodBuilder.GetILGenerator();
// Writing body
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, proxyInterceptorPipelineField);
il.Emit(OpCodes.Ldarg_1);
il.EmitCall(OpCodes.Callvirt, InterceptionBehaviorPipelineMethods.Add, null);
il.Emit(OpCodes.Ret);
typeBuilder.DefineMethodOverride(methodBuilder, IInterceptingProxyMethods.AddInterceptionBehavior);
}
示例5: Generate
public override MethodBuilder Generate(TypeBuilder typeBuilder)
{
var methodBuilder = GenerateMethod(typeBuilder);
typeBuilder.DefineMethodOverride(methodBuilder, _methodMetadata.Method);
return methodBuilder;
}
示例6: EmitOverride_GetCacheType
private void EmitOverride_GetCacheType(TypeBuilder typeBuilder, string overrideName, FieldBuilder fb_cacheType)
{
MethodBuilder mb = typeBuilder.DefineMethod(overrideName, MethodAttributes.Family | MethodAttributes.Virtual);
mb.SetReturnType(typeof(Type));
typeBuilder.DefineMethodOverride(mb,
typeof(ReadMapper).GetMethod(overrideName, BindingFlags.NonPublic | BindingFlags.Instance));
ILGenerator il = mb.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, fb_cacheType);
il.Emit(OpCodes.Ret);
}
示例7: 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"));
}
示例8: DefineMethod
private static MethodBuilder DefineMethod(TypeBuilder newType, Type interfaceType, string methodName)
{
MethodInfo getInterfaceMethodInfo = interfaceType.GetMethod(methodName);
MethodBuilder getMethod = newType.DefineMethod(
string.Concat(interfaceType.Name, '.', getInterfaceMethodInfo.Name),
MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.Virtual,
CallingConventions.Standard,
getInterfaceMethodInfo.ReturnType,
ClassWrappers.GetParameterTypes(getInterfaceMethodInfo.GetParameters()));
newType.DefineMethodOverride(getMethod, getInterfaceMethodInfo);
return getMethod;
}
示例9: WriteIsAuthorized
private static void WriteIsAuthorized(Type parentType, TypeBuilder builder)
{
MethodInfo protectedMethod = parentType.GetMethod("AuthorizeCore", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod);
MethodBuilder implementedMethod = builder.DefineMethod("IsAuthorized", MethodAttributes.Public | MethodAttributes.Virtual, typeof(bool), new[] { typeof(HttpContextBase) });
ILGenerator il = implementedMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Call, protectedMethod);
il.Emit(OpCodes.Ret);
MethodInfo interfaceMethod = authorizeAttributeType.GetMethod("IsAuthorized", BindingFlags.Public | BindingFlags.Instance);
builder.DefineMethodOverride(implementedMethod, interfaceMethod);
}
示例10: DefinePropertyGetMethod
private static MethodBuilder DefinePropertyGetMethod(TypeBuilder newType, string propertyName, Type interfaceType)
{
MethodInfo getPropertyMethod = interfaceType.GetProperty(propertyName).GetGetMethod();
if (getPropertyMethod == null)
return null;
MethodBuilder builder = newType.DefineMethod(
string.Concat(interfaceType.Name, '.', getPropertyMethod.Name),
MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.Virtual,
getPropertyMethod.ReturnType, Type.EmptyTypes);
newType.DefineMethodOverride(builder, getPropertyMethod);
return builder;
}
示例11: DefineMethod
private static MethodBuilder DefineMethod(TypeBuilder newType, Type interfaceType, string methodName, Type[] parameterTypes)
{
MethodInfo getMethod = interfaceType.GetMethod(methodName, parameterTypes);
if (getMethod == null)
return null;
MethodBuilder builder = newType.DefineMethod(
string.Concat(interfaceType.Name, '.', getMethod.Name),
MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.Virtual,
getMethod.ReturnType, parameterTypes);
newType.DefineMethodOverride(builder, getMethod);
return builder;
}
示例12: BuildIProxyTargetPropertyMethod
private static void BuildIProxyTargetPropertyMethod(
TypeBuilder proxyBuilder, Type targetType, Type iProxyType,
FieldBuilder wrappedObject, TypeDebugging debug)
{
var attributes = MethodAttributes.HideBySig |
MethodAttributes.Virtual | MethodAttributes.Private;
var method = proxyBuilder.DefineMethod(GetTargetMethod,
attributes, targetType, Type.EmptyTypes);
proxyBuilder.DefineMethodOverride(method,
iProxyType.GetProperty(TargetProperty).GetGetMethod());
using (var generator = debug.GetMethodDebugging(method))
{
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld, wrappedObject);
generator.Emit(OpCodes.Ret);
}
}
示例13: 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);
}
}
示例14: Generate
public override MethodBuilder Generate(TypeBuilder typeBuilder)
{
var targetMethodGenerator = new TargetedInvocationMethodGenerator(
_methodMetadata,
_targetField);
var targetMethod = targetMethodGenerator.Generate(typeBuilder);
var methodBuilder = GenerateMethod(
typeBuilder,
_methodMetadata,
_dispatcherField,
_methodInfoField,
targetMethod);
if (_methodMetadata.IsExplicitInterfaceImplementation)
{
typeBuilder.DefineMethodOverride(methodBuilder, _methodMetadata.Method);
}
return methodBuilder;
}
示例15: CreateMethod
public static MethodBuilder CreateMethod(TypeBuilder typeBuilder, MethodInfo method)
{
var scope = method.IsPublic
? MethodAttributes.Public
: MethodAttributes.Family;
var parameters = method.GetParameters();
var parameterTypes = parameters.Select(p => p.ParameterType)
.ToArray();
var methodBuilder = typeBuilder.DefineMethod(method.Name, scope | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.HideBySig, method.CallingConvention, method.ReturnType, parameterTypes);
foreach (var parameter in parameters)
{
methodBuilder.DefineParameter(parameter.Position + 1, parameter.Attributes, parameter.Name);
}
typeBuilder.DefineMethodOverride(methodBuilder, method);
return methodBuilder;
}