protected virtual void AddAdapterMethod(SupertypingAdapter adapter, MethodInfo method, MethodInfo adapteeMethod) { ParameterInfo[] parameters = typeManager.GetParameters(method); Type[] paramTypes = new Type[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { paramTypes[i] = parameters[i].ParameterType; } MethodBuilder mb = adapter.TypeBuilder. DefineMethod(method.DeclaringType.FullName + "." + method.Name, MethodAttributes.Private | MethodAttributes.Virtual | MethodAttributes.HideBySig, method.ReturnType, paramTypes); SupertypingAdapterMethod adapterMethod = new SupertypingAdapterMethod(mb, adapteeMethod, parameters.Length); adapter.Methods.Add(adapterMethod); adapter.TypeBuilder.DefineMethodOverride(mb, method); }
protected virtual void GenerateAdapter(SupertypingAdapter adapter) { ilGenerator = adapter.Constructor.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.Emit(OpCodes.Stfld, adapter.AdapteeField); ilGenerator.Emit(OpCodes.Ret); foreach (SupertypingAdapterMethod method in adapter.Methods) { ilGenerator = method.MethodBuilder.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldfld, adapter.AdapteeField); for (int i = 1; i <= method.ParameterCount; i++) { ilGenerator.Emit(OpCodes.Ldarg, i); } ilGenerator.Emit(OpCodes.Tailcall); if (adapter.AdapteeType.IsAbstract) ilGenerator.EmitCall(OpCodes.Callvirt, method.AdapteeMethod, null); else ilGenerator.EmitCall(OpCodes.Call, method.AdapteeMethod, null); ilGenerator.Emit(OpCodes.Ret); } adapter.TypeBuilder.CreateType(); }