示例#1
0
        private static ConstructorInfo FindConstructor(
            Type type,
            ImmutableArray <TypedConstant> symbolConstructorArguments)
        {
            var constructors = type.GetConstructors();

            foreach (var constructor in constructors)
            {
                var runtimeParameters = constructor.GetParameters();
                if (runtimeParameters.Length != symbolConstructorArguments.Length)
                {
                    continue;
                }

                var parametersMatched = true;
                for (var index = 0; index < runtimeParameters.Length; index++)
                {
                    var runtimeParameter = runtimeParameters[index].ParameterType;
                    if (symbolConstructorArguments[index].Kind == TypedConstantKind.Array &&
                        runtimeParameter.IsArray)
                    {
                        var arrayType = (IArrayTypeSymbol)symbolConstructorArguments[index].Type;
                        if (!CodeAnalysisSymbolBasedTypeInfo.IsType(
                                arrayType.ElementType,
                                runtimeParameter.GetElementType().GetTypeInfo()))
                        {
                            parametersMatched = false;
                            break;
                        }
                    }
                    else
                    {
                        if (!CodeAnalysisSymbolBasedTypeInfo.IsType(
                                symbolConstructorArguments[index].Type,
                                runtimeParameter.GetTypeInfo()))
                        {
                            parametersMatched = false;
                            break;
                        }
                    }
                }

                if (parametersMatched)
                {
                    return(constructor);
                }
            }

            throw new InvalidOperationException(Resources.FormatCodeAnalysisConstructorNotFound(type.FullName));
        }
示例#2
0
        /// <summary>
        /// Gets the sequence of <see cref="Attribute"/>s of type <typeparamref name="TAttribute"/>
        /// that are declared on the specified <paramref name="symbol"/>.
        /// </summary>
        /// <typeparam name="TAttribute">The <see cref="Attribute"/> type.</typeparam>
        /// <param name="symbol">The <see cref="ISymbol"/> to find attributes on.</param>
        /// <returns></returns>
        public static IEnumerable <TAttribute> GetCustomAttributes <TAttribute>(ISymbol symbol)
            where TAttribute : Attribute
        {
            if (symbol == null)
            {
                throw new ArgumentNullException(nameof(symbol));
            }

            var attributes = symbol.GetAttributes();

            if (attributes.Length > 0)
            {
                return(attributes
                       .Where(attribute => CodeAnalysisSymbolBasedTypeInfo.IsType(
                                  attribute.AttributeClass,
                                  typeof(TAttribute).GetTypeInfo()))
                       .Select(attribute => CreateAttribute <TAttribute>(attribute))
                       .ToArray());
            }

            return(Enumerable.Empty <TAttribute>());
        }