Skip to content

Commit f7d2c2e

Browse files
committed
IL显式实现接口
1 parent 8b84d6d commit f7d2c2e

File tree

3 files changed

+9
-129
lines changed

3 files changed

+9
-129
lines changed

WebApiClientCore/HttpApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public static MethodInfo[] FindApiMethods(Type httpApiType)
9797
throw new ArgumentException(Resx.required_InterfaceType.Format(httpApiType.Name));
9898
}
9999

100-
return HttpApiMethodFinder
101-
.FindApiMethods(httpApiType)
100+
return httpApiType.GetInterfaces().Append(httpApiType)
101+
.SelectMany(item => item.GetMethods())
102102
.Select(item => item.EnsureApiMethod())
103103
.ToArray();
104104
}

WebApiClientCore/HttpApiMethodFinder.cs

Lines changed: 0 additions & 122 deletions
This file was deleted.

WebApiClientCore/Implementations/EmitHttpApiActivator.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class EmitHttpApiActivator<
1515
#if NET5_0_OR_GREATER
1616
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
1717
#endif
18-
THttpApi> : IHttpApiActivator<THttpApi>
18+
THttpApi> : IHttpApiActivator<THttpApi>
1919
{
2020
private readonly ApiActionInvoker[] actionInvokers;
2121
private readonly Func<IHttpApiInterceptor, ApiActionInvoker[], THttpApi> activator;
@@ -151,17 +151,19 @@ private static void BuildCtor(TypeBuilder builder, FieldBuilder fieldApiIntercep
151151
/// <param name="fieldActionInvokers">action执行器字段</param>
152152
private static void BuildMethods(TypeBuilder builder, MethodInfo[] actionMethods, FieldBuilder fieldApiInterceptor, FieldBuilder fieldActionInvokers)
153153
{
154-
const MethodAttributes implementAttribute = MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.NewSlot | MethodAttributes.HideBySig;
154+
// private final hidebysig newslot virtual
155+
const MethodAttributes implementAttribute = MethodAttributes.Private | MethodAttributes.Final | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;
155156

156157
for (var i = 0; i < actionMethods.Length; i++)
157158
{
158159
var actionMethod = actionMethods[i];
159160
var actionParameters = actionMethod.GetParameters();
160161
var parameterTypes = actionParameters.Select(p => p.ParameterType).ToArray();
162+
var actionMethodName = $"{actionMethod.DeclaringType?.FullName}.{actionMethod.Name}";
161163

162-
var iL = builder
163-
.DefineMethod(actionMethod.Name, implementAttribute, CallingConventions.Standard, actionMethod.ReturnType, parameterTypes)
164-
.GetILGenerator();
164+
var methodBuilder = builder.DefineMethod(actionMethodName, implementAttribute, CallingConventions.Standard, actionMethod.ReturnType, parameterTypes);
165+
builder.DefineMethodOverride(methodBuilder, actionMethod);
166+
var iL = methodBuilder.GetILGenerator();
165167

166168
// this.apiInterceptor
167169
iL.Emit(OpCodes.Ldarg_0);

0 commit comments

Comments
 (0)