public static ITypeMetadata FromTypeSymbol(ITypeSymbol symbol)
        {
            if (symbol.Name == "Nullable" && symbol.ContainingNamespace.Name == "System")
            {
                var type     = symbol as INamedTypeSymbol;
                var argument = type?.TypeArguments.FirstOrDefault();

                if (argument != null)
                {
                    return(new RoslynTypeMetadata(argument, true, false));
                }
            }
            else if (symbol.Name == "Task" && symbol.ContainingNamespace.GetFullName() == "System.Threading.Tasks")
            {
                var type     = symbol as INamedTypeSymbol;
                var argument = type?.TypeArguments.FirstOrDefault();

                if (argument != null)
                {
                    if (argument.Name == "Nullable" && argument.ContainingNamespace.Name == "System")
                    {
                        type = argument as INamedTypeSymbol;
                        var innerArgument = type?.TypeArguments.FirstOrDefault();

                        if (innerArgument != null)
                        {
                            return(new RoslynTypeMetadata(innerArgument, true, true));
                        }
                    }

                    return(new RoslynTypeMetadata(argument, false, true));
                }

                return(new RoslynVoidTaskMetadata());
            }
            else if (symbol.BaseType?.SpecialType == SpecialType.System_Enum)
            {
                var result          = new RoslynTypeMetadata(symbol, false, false);
                var namedTypeSymbol = symbol as INamedTypeSymbol;

                var symbols = namedTypeSymbol.GetMembers();
                if (symbols.Length == 0)
                {
                    result.DefaultValue = "enum should contain minimum one enum value";
                }
                else
                {
                    result.DefaultValue = $"{namedTypeSymbol.Name}.{symbols[0].Name}";
                }

                return(result);
            }

            return(new RoslynTypeMetadata(symbol, false, false));
        }
예제 #2
0
        public static ITypeMetadata FromTypeSymbol(ITypeSymbol symbol)
        {
            if (symbol.Name == "Nullable" && symbol.ContainingNamespace.Name == "System")
            {
                var type     = symbol as INamedTypeSymbol;
                var argument = type?.TypeArguments.FirstOrDefault();

                if (argument != null)
                {
                    return(new RoslynTypeMetadata(argument, true, false, false));
                }
            }
            else if (symbol.Name.StartsWith("List"))
            {
                var type     = symbol as INamedTypeSymbol;
                var argument = type?.TypeArguments.FirstOrDefault();

                if (argument != null)
                {
                    var roslynTypeMetadata = new RoslynTypeMetadata(argument, false, false, true);
                    return(roslynTypeMetadata);
                }
            }
            else if (symbol.Name == "Task" && symbol.ContainingNamespace.GetFullName() == "System.Threading.Tasks")
            {
                var type     = symbol as INamedTypeSymbol;
                var argument = type?.TypeArguments.FirstOrDefault();

                if (argument != null)
                {
                    if (argument.Name == "Nullable" && argument.ContainingNamespace.Name == "System")
                    {
                        type = argument as INamedTypeSymbol;
                        var innerArgument = type?.TypeArguments.FirstOrDefault();

                        if (innerArgument != null)
                        {
                            return(new RoslynTypeMetadata(innerArgument, true, true, false));
                        }
                    }

                    return(new RoslynTypeMetadata(argument, false, true, false));
                }

                return(new RoslynVoidTaskMetadata());
            }

            return(new RoslynTypeMetadata(symbol, false, false, false));
        }