// Constructors

        /// <summary>
        /// Initializes new instance of this type.
        /// </summary>
        /// <param name="source">The source mapping.</param>
        public InterfaceMapping(ReflectionInterfaceMapping source)
        {
            TargetType       = source.TargetType;
            InterfaceType    = source.InterfaceType;
            TargetMethods    = new ReadOnlyList <MethodInfo>(source.TargetMethods.ToList());
            InterfaceMethods = new ReadOnlyList <MethodInfo>(source.InterfaceMethods.ToList());
        }
        public ExplicitInterface(object target, Type interfaceType)
        {
            if (target == null) throw new ScriptException("Null object conversion");
              if (interfaceType == null) throw new ArgumentNullException();

              Mapping = target.GetType().GetInterfaceMap(interfaceType);
              Target = target;
        }
        /// <summary>
        /// Returns the <see cref="MethodInfo"/> of implementation of <paramref name="interfaceMethod"/>.
        /// </summary>
        /// <param name="interfaceMethod">The interface method.</param>
        /// <param name="implementation">The implementation.</param>
        /// <returns>The <see cref="MethodInfo"/> of implementation of <paramref name="interfaceMethod"/>.</returns>
        private static MethodInfo GetImplementingMethod(MethodInfo interfaceMethod, InterfaceMapping implementation)
        {
            for (var i = 0; i < implementation.InterfaceMethods.Length; i++)
            {
                if (implementation.InterfaceMethods[i] == interfaceMethod)
                {
                    return implementation.TargetMethods[i];
                }
            }

            return null;
        }
        private MethodInfo GetTargetMethodInfo(MethodInfo methodInfo)
        {
            Type interfaceType = methodInfo.DeclaringType;
            var mapping = new InterfaceMapping();
            for (int i = 0; i < _mappings.Count; i++)
                if (_mappings[i].InterfaceType == interfaceType)
                {
                    mapping = _mappings[i];
                    break;
                }

            if (mapping.InterfaceType == null)
            {
                mapping = _typeBuilder.BaseType.GetInterfaceMap(interfaceType);
                _mappings.Add(mapping);
            }

            return ProxyBuilderHelper.GetTargetMethodInfo(ref mapping, methodInfo);
        }
 public MethodConfigCallback(ICatalogCollection coll, Type t, Type impl, Hashtable cache, RegistrationDriver driver)
 {
     this._type = t;
     this._impl = impl;
     this._coll = coll;
     this._cache = cache;
     this._driver = driver;
     if (this._type.IsInterface)
     {
         this._map = this._impl.GetInterfaceMap(this._type);
     }
     else
     {
         this._map.InterfaceMethods = null;
         this._map.InterfaceType = null;
         this._map.TargetMethods = null;
         this._map.TargetType = null;
     }
     RegistrationDriver.Populate(coll);
 }
		public InterfaceMembersOnClassCollector(Type type, ModuleScope scope, bool onlyProxyVirtual, InterfaceMapping map) : base(type, scope)
		{
			this.onlyProxyVirtual = onlyProxyVirtual;
			this.map = map;
		}
        public static MethodInfo GetTargetMethodInfo(ref InterfaceMapping mapping, MethodInfo interfaceMethod)
        {
            for (int i = 0; i < mapping.InterfaceMethods.Length; i++)
                if (mapping.InterfaceMethods[i] == interfaceMethod)
                    return mapping.TargetMethods[i];

            throw new InvalidOperationException("mixin target method not found");
        }
Esempio n. 8
0
        /// <summary>
        ///     Constructs individual operation objects based on the service implementation.
        /// </summary>
        /// <param name="map">Mapping of the service interface & implementation.</param>
        /// <param name="definitionsTypesList">Complex types that will need later processing.</param>
        internal IEnumerable<Tuple<string, PathAction>> GetActions(InterfaceMapping map,
                                                                   IList<Type> definitionsTypesList)
        {
            int methodsCounts = map.InterfaceMethods.Count();
            for (var index = 0; index < methodsCounts; index++)
            {
                MethodInfo implementation = map.TargetMethods[index];
                MethodInfo declaration = map.InterfaceMethods[index];

                //if the method is marked Hidden anywhere, skip it
                if (implementation.GetCustomAttribute<SwaggerWcfHiddenAttribute>() != null ||
                    declaration.GetCustomAttribute<SwaggerWcfHiddenAttribute>() != null)
                    continue;

                //if a tag from either implementation or declaration is marked as not visible, skip it
                List<SwaggerWcfTagAttribute> methodTags =
                    implementation.GetCustomAttributes<SwaggerWcfTagAttribute>().ToList();
                methodTags =
                    methodTags.Concat(declaration.GetCustomAttributes<SwaggerWcfTagAttribute>()).ToList();

                if (methodTags.Select(t => t.TagName).Any(HiddenTags.Contains))
                    continue;

                //find the WebGet/Invoke attributes, or skip if neither is present
                var wg = declaration.GetCustomAttribute<WebGetAttribute>();
                var wi = declaration.GetCustomAttribute<WebInvokeAttribute>();
                if (wg == null && wi == null)
                    continue;

                string httpMethod = (wi == null) ? "GET" : wi.Method;
                string uriTemplate = (wi == null) ? (wg.UriTemplate ?? "") : (wi.UriTemplate ?? "");

                //implementation description overrides interface description
                string description =
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(implementation, "Description") ??
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(declaration, "Description") ??
                    Helpers.GetCustomAttributeValue<string, DescriptionAttribute>(implementation, "Description") ??
                    Helpers.GetCustomAttributeValue<string, DescriptionAttribute>(declaration, "Description") ??
                    "";

                string summary =
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(implementation, "Summary") ??
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(declaration, "Summary") ??
                    "";

                string operationId =
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(implementation, "OperationId") ??
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(declaration, "OperationId") ??
                    "";

                string externalDocsDescription =
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(implementation,
                                                                                     "ExternalDocsDescription") ??
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(declaration,
                                                                                     "ExternalDocsDescription") ??
                    "";

                string externalDocsUrl =
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(implementation, "ExternalDocsUrl") ??
                    Helpers.GetCustomAttributeValue<string, SwaggerWcfPathAttribute>(declaration, "ExternalDocsUrl") ??
                    "";

                ExternalDocumentation externalDocs = null;
                if (!string.IsNullOrWhiteSpace(externalDocsDescription) || !string.IsNullOrWhiteSpace(externalDocsUrl))
                {
                    externalDocs = new ExternalDocumentation
                    {
                        Description = HttpUtility.HtmlEncode(externalDocsDescription),
                        Url = HttpUtility.HtmlEncode(externalDocsUrl)
                    };
                }

                bool deprecated =
                    Helpers.GetCustomAttributeValue<SwaggerWcfPathAttribute>(implementation, "Deprecated");
                if (!deprecated)
                    deprecated = Helpers.GetCustomAttributeValue<SwaggerWcfPathAttribute>(declaration, "Deprecated");

                var operation = new PathAction
                {
                    Id = httpMethod.ToLowerInvariant(),
                    Summary = HttpUtility.HtmlEncode(summary),
                    Description = HttpUtility.HtmlEncode(description),
                    Tags = methodTags.Where(t => !t.HideFromSpec).Select(t => HttpUtility.HtmlEncode(t.TagName)).ToList(),
                    Consumes = new List<string>(GetConsumes(implementation, declaration)),
                    Produces = new List<string>(GetProduces(implementation, declaration)),
                    Deprecated = deprecated,
                    OperationId = HttpUtility.HtmlEncode(operationId),
                    ExternalDocs = externalDocs,
                    Responses = GetResponseCodes(implementation, declaration, definitionsTypesList)
                    // Schemes = TODO: how to get available schemes for this WCF service? (schemes: http/https)
                };

                //try to map each implementation parameter to the uriTemplate.
                ParameterInfo[] parameters = declaration.GetParameters();
                if (parameters.Any())
                    operation.Parameters = new List<ParameterBase>();
                foreach (ParameterInfo parameter in parameters)
                {
                    TypeFormat typeFormat = Helpers.MapSwaggerType(parameter.ParameterType, definitionsTypesList);

                    SwaggerWcfParameterAttribute settings =
                        implementation.GetParameters()
                                      .First(p => p.Position.Equals(parameter.Position))
                                      .GetCustomAttribute<SwaggerWcfParameterAttribute>() ??
                        parameter.GetCustomAttribute<SwaggerWcfParameterAttribute>();

                    if (implementation.GetParameters()
                                      .First(p => p.Position.Equals(parameter.Position))
                                      .GetCustomAttribute<SwaggerWcfHiddenAttribute>() != null ||
                        parameter.GetCustomAttribute<SwaggerWcfHiddenAttribute>() != null)
                        continue;

                    List<SwaggerWcfTagAttribute> piTags =
                        methodTags.Concat(parameter.GetCustomAttributes<SwaggerWcfTagAttribute>())
                                  .ToList();

                    if (piTags.Select(t => t.TagName).Any(HiddenTags.Contains))
                        continue;

                    operation.Parameters.Add(GetParameter(typeFormat, parameter, settings, uriTemplate,
                                                          definitionsTypesList));
                }
                string uri = declaration.Name;

                if (!string.IsNullOrWhiteSpace(uriTemplate))
                {
                    int indexOfQuestionMark = uriTemplate.IndexOf('?');
                    if (indexOfQuestionMark < 0)
                        uri = uriTemplate;
                    else
                        uri = uriTemplate.Substring(0, indexOfQuestionMark);
                }
                yield return new Tuple<string, PathAction>(uri, operation);
            }
        }