private ServiceDescriptor ResolveIsClass() { string name = string.Concat(serviceType.Name, "Proxy"); var interfaces = serviceType.GetInterfaces(); var classEmitter = moduleEmitter.DefineType(name, TypeAttributes.Public | TypeAttributes.Class, serviceType, interfaces); var instanceAst = classEmitter.DefineField("____instance__", serviceType, FieldAttributes.Private | FieldAttributes.InitOnly | FieldAttributes.NotSerialized); bool throwsError = true; foreach (var constructorInfo in serviceType.GetConstructors()) { var parameterInfos = constructorInfo.GetParameters(); if (parameterInfos.Length > 0) { continue; } throwsError = false; var constructorEmitter = classEmitter.DefineConstructor(MethodAttributes.Public); var parameterEmitter = constructorEmitter.DefineParameter(serviceType, ParameterAttributes.None, "instance"); constructorEmitter.Append(Assign(instanceAst, parameterEmitter)); constructorEmitter.InvokeBaseConstructor(constructorInfo); break; } if (throwsError) { throw new AstException($"“{serviceType.FullName}”不存在无参构造函数!"); } var interceptMethods = new Dictionary <MethodInfo, IList <CustomAttributeData> >(MethodInfoEqualityComparer.Instance); foreach (var type in interfaces) { bool flag = type.IsDefined(InterceptAttributeType, false); var attributes = flag ? type.GetCustomAttributesData() : new CustomAttributeData[0]; foreach (var methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { var interceptAttributes = methodInfo.IsDefined(InterceptAttributeType, false) ? Merge(attributes, methodInfo.GetCustomAttributesData()) : attributes; if (interceptAttributes.Count == 0) { continue; } if (interceptMethods.TryGetValue(methodInfo, out var intercepts)) { interceptMethods[methodInfo] = Merge(intercepts, interceptAttributes); } else { interceptMethods.Add(methodInfo, interceptAttributes); } } } var iterationType = serviceType; do { if (iterationType.IsDefined(InterceptAttributeType, false)) { var intercepts = iterationType.GetCustomAttributesData(); foreach (var methodInfo in iterationType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { if (interceptMethods.TryGetValue(methodInfo, out var attributes)) { interceptMethods[methodInfo] = Merge(attributes, intercepts); } else { interceptMethods.Add(methodInfo, intercepts); } } } iterationType = iterationType.BaseType; } while (iterationType != null && iterationType != typeof(object)); foreach (var methodInfo in serviceType.GetMethods()) { if (interceptMethods.TryGetValue(methodInfo, out var interceptAttributes)) { InterceptCore.DefineMethodOverride(instanceAst, classEmitter, methodInfo, interceptAttributes); } } interceptMethods.Clear(); return(Resolve(serviceType, classEmitter.CreateType(), lifetime)); }
private ServiceDescriptor ResolveIsInterface() { string name = string.Concat(serviceType.Name, "Proxy"); var interfaces = serviceType.GetAllInterfaces(); var classEmitter = moduleEmitter.DefineType(name, TypeAttributes.Public | TypeAttributes.Class, null, interfaces); var instanceAst = classEmitter.DefineField("____instance__", serviceType, FieldAttributes.Private | FieldAttributes.InitOnly | FieldAttributes.NotSerialized); var constructorEmitter = classEmitter.DefineConstructor(MethodAttributes.Public); var parameterEmitter = constructorEmitter.DefineParameter(serviceType, ParameterAttributes.None, "instance"); constructorEmitter.Append(Assign(instanceAst, parameterEmitter)); var interceptMethods = new Dictionary <MethodInfo, IList <CustomAttributeData> >(MethodInfoEqualityComparer.Instance); foreach (var type in interfaces) { bool flag = type.IsDefined(InterceptAttributeType, false); var attributes = flag ? type.GetCustomAttributesData() : new CustomAttributeData[0]; foreach (var methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { var interceptAttributes = methodInfo.IsDefined(InterceptAttributeType, false) ? Merge(attributes, methodInfo.GetCustomAttributesData()) : attributes; if (interceptAttributes.Count == 0) { continue; } if (interceptMethods.TryGetValue(methodInfo, out var intercepts)) { interceptMethods[methodInfo] = Merge(intercepts, interceptAttributes); } else { interceptMethods.Add(methodInfo, interceptAttributes); } } } var propertyMethods = new HashSet <MethodInfo>(); foreach (var propertyInfo in serviceType.GetProperties()) { var propertyEmitter = classEmitter.DefineProperty(propertyInfo.Name, propertyInfo.Attributes, propertyInfo.PropertyType); if (propertyInfo.CanRead) { var readMethod = propertyInfo.GetGetMethod(true); propertyMethods.Add(readMethod); propertyEmitter.SetGetMethod(InterceptCore.DefineMethodOverride(instanceAst, classEmitter, readMethod, new CustomAttributeData[0])); } if (propertyInfo.CanWrite) { var writeMethod = propertyInfo.GetSetMethod(true); propertyMethods.Add(writeMethod); propertyEmitter.SetSetMethod(InterceptCore.DefineMethodOverride(instanceAst, classEmitter, writeMethod, new CustomAttributeData[0])); } } foreach (var methodInfo in serviceType.GetMethods()) { if (propertyMethods.Contains(methodInfo)) { continue; } if (interceptMethods.TryGetValue(methodInfo, out var interceptAttributes)) { InterceptCore.DefineMethodOverride(instanceAst, classEmitter, methodInfo, interceptAttributes); } else { InterceptCore.DefineMethodOverride(instanceAst, classEmitter, methodInfo, new CustomAttributeData[0]); } } propertyMethods.Clear(); interceptMethods.Clear(); return(Resolve(serviceType, classEmitter.CreateType(), lifetime)); }
private ServiceDescriptor ResolveIsClass() { string name = string.Concat(serviceType.Name, "Proxy"); var interfaces = implementationType.GetInterfaces(); var classEmitter = moduleEmitter.DefineType(name, TypeAttributes.Public | TypeAttributes.Class, implementationType, interfaces); foreach (var constructorInfo in implementationType.GetConstructors(BindingFlags.Public | BindingFlags.Instance)) { var constructorEmitter = classEmitter.DefineConstructor(constructorInfo.Attributes); var parameterInfos = constructorInfo.GetParameters(); var parameterEmiters = new ParameterEmitter[parameterInfos.Length]; for (int i = 0; i < parameterInfos.Length; i++) { parameterEmiters[i] = constructorEmitter.DefineParameter(parameterInfos[i]); } constructorEmitter.InvokeBaseConstructor(constructorInfo, parameterEmiters); } var interceptMethods = new Dictionary <MethodInfo, IList <CustomAttributeData> >(MethodInfoEqualityComparer.Instance); foreach (var type in interfaces) { bool flag = type.IsDefined(InterceptAttributeType, false); var attributes = flag ? type.GetCustomAttributesData() : new CustomAttributeData[0]; foreach (var methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { var interceptAttributes = methodInfo.IsDefined(InterceptAttributeType, false) ? Merge(attributes, methodInfo.GetCustomAttributesData()) : attributes; if (interceptAttributes.Count == 0) { continue; } if (interceptMethods.TryGetValue(methodInfo, out var intercepts)) { interceptMethods[methodInfo] = Merge(intercepts, interceptAttributes); } else { interceptMethods.Add(methodInfo, interceptAttributes); } } } var iterationType = serviceType; do { if (iterationType.IsDefined(InterceptAttributeType, false)) { var intercepts = iterationType.GetCustomAttributesData(); foreach (var methodInfo in iterationType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { if (interceptMethods.TryGetValue(methodInfo, out var attributes)) { interceptMethods[methodInfo] = Merge(intercepts, attributes); } else { interceptMethods.Add(methodInfo, intercepts); } } } iterationType = iterationType.BaseType; } while (iterationType != null && iterationType != typeof(object)); foreach (var methodInfo in serviceType.GetMethods()) { if (interceptMethods.TryGetValue(methodInfo, out var interceptAttributes)) { InterceptCore.DefineMethodOverride(This, classEmitter, methodInfo, interceptAttributes); } } interceptMethods.Clear(); return(new ServiceDescriptor(serviceType, classEmitter.CreateType(), lifetime)); }