コード例 #1
0
ファイル: MemberInfo.cs プロジェクト: aikixd/CodeGeneration
 internal static MethodMemberInfo FromSymbol(IMethodSymbol symbol)
 {
     try
     {
         return(new MethodMemberInfo(new RoslynOrigin(symbol), TypeInfo.FromSymbol(symbol.ReturnType)));
     }
     catch (Exception e)
     {
         throw new InvalidOperationException($"Could not create method info for {{{symbol}}}. See inner exception for details.", e);
     }
 }
コード例 #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
ファイル: ArgumentInfo.cs プロジェクト: aikixd/CodeGeneration
 internal static ParameterInfo FromSymbol(IParameterSymbol x)
 {
     return(new ParameterInfo(
                x.Name,
                TypeInfo.FromSymbol(x.Type)));
 }
コード例 #4
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.");
                }
            }
        }