コード例 #1
0
        public AttributeInfo(TypeInfo type, MethodMemberInfo constructor, IList <object> passedArguments, IReadOnlyDictionary <string, object> namedArguments)
        {
            this.Type            = type ?? throw new ArgumentNullException(nameof(type));
            this.Constructor     = constructor ?? throw new ArgumentNullException(nameof(constructor));
            this.PassedArguments = passedArguments ?? throw new ArgumentNullException(nameof(passedArguments));
            this.NamedArguments  = namedArguments ?? throw new ArgumentNullException(nameof(namedArguments));

            if (this.Constructor.Parameters.Count() != passedArguments.Count)
            {
                throw new ArgumentException(
                          "Number of passed arguments and constructor parameters in an attribute do not match. " +
                          "Attribute {" + this.Type.Namespace + "." + this.Type.Name + "}.");
            }
        }
コード例 #2
0
        public static AttributeInfo Create(AttributeData attributeData)
        {
            return(new AttributeInfo(
                       TypeInfo.FromSymbol(attributeData.AttributeClass),
                       MethodMemberInfo.FromSymbol(attributeData.AttributeConstructor),
                       attributeData.ConstructorArguments.Select(getPassedArg).ToArray(),
                       attributeData.NamedArguments.ToDictionary(x => x.Key, x => getPassedArg(x.Value))));

            object getPassedArg(TypedConstant arg)
            {
                if (arg.Kind == TypedConstantKind.Array)
                {
                    return(arg.Values.Select(getPassedArg).Select(transformArg).ToArray());
                }

                else
                {
                    return(transformArg(arg.Value));
                }
            }

            object transformArg(object arg)
            {
                if (arg.GetType().IsPrimitive)
                {
                    return(arg);
                }

                if (arg is INamedTypeSymbol s)
                {
                    return(TypeInfo.FromSymbol(s));
                }

                return(arg);
            }
        }
コード例 #3
0
        public static InterfaceInfo FromSymbol(INamedTypeSymbol symbol)
        {
            if (symbol.TypeKind != Microsoft.CodeAnalysis.TypeKind.Interface)
            {
                return(null);
            }

            var props   = new LinkedList <PropertyMemberInfo>();
            var methods = new LinkedList <MethodMemberInfo>();

            foreach (var s in symbol.GetMembers())
            {
                switch (s.Kind)
                {
                case SymbolKind.Property:
                    props.AddLast(PropertyMemberInfo.FromSymbol((IPropertySymbol)s));
                    break;

                case SymbolKind.Method:
                    if (s.CanBeReferencedByName)
                    {
                        methods.AddLast(MethodMemberInfo.FromSymbol((IMethodSymbol)s));
                    }
                    break;
                }
            }

            return(new InterfaceInfo(
                       TypeInfo.FromSymbol(symbol),
                       getAccessibility(symbol.DeclaredAccessibility),
                       props,
                       methods,
                       symbol.GetAttributes().Select(AttributeInfo.Create).ToArray()));

            Accessibility getAccessibility(Microsoft.CodeAnalysis.Accessibility access)
            {
                switch (access)
                {
                case Microsoft.CodeAnalysis.Accessibility.NotApplicable:
                    return(Accessibility.None);

                case Microsoft.CodeAnalysis.Accessibility.Private:
                    return(Accessibility.Private);

                case Microsoft.CodeAnalysis.Accessibility.Protected:
                    return(Accessibility.Protected);

                case Microsoft.CodeAnalysis.Accessibility.Public:
                    return(Accessibility.Public);

                case Microsoft.CodeAnalysis.Accessibility.Internal:
                    return(Accessibility.Internal);

                case Microsoft.CodeAnalysis.Accessibility.ProtectedAndInternal:
                    return(Accessibility.Protected | Accessibility.Internal);

                default:
                    throw new ArgumentOutOfRangeException(nameof(access), $"Interface accessibility {access.ToString()} is not supported.");
                }
            }
        }