Exemplo n.º 1
0
        public static bool IsAssignableFrom(this Type type, IDeclaredType c)
        {
            if (c == null)
                return false;

            return type.FullName == c.GetClrName().FullName || c.GetAllSuperTypes().Any(superType => type.FullName == superType.GetClrName().FullName);
        }
        private static bool IsTaskType(IDeclaredType type)
        {
            string typeName = type.GetClrName().FullName;

            return(string.Equals(typeName, TaskTypeName, StringComparison.Ordinal) ||
                   string.Equals(typeName, TaskOfTTypeName, StringComparison.Ordinal) ||
                   string.Equals(typeName, ValueTaskTypeName, StringComparison.Ordinal) ||
                   string.Equals(typeName, ValueTaskOfTTypeName, StringComparison.Ordinal));
        }
        static bool IsValidFieldOfType(this IDeclaredElement element, Type type)
        {
            IDeclaredType fieldType = element.GetValidatedFieldType();

            if (fieldType == null)
            {
                return(false);
            }

            return(fieldType.GetClrName().FullName == type.FullName);
        }
Exemplo n.º 4
0
        protected virtual bool ResultIsAlreadyCheckedByContractEnsures(
            IEnumerable <ContractEnsures> ensureAssertions, IDeclaredType methodReturnType)
        {
            Contract.Requires(ensureAssertions != null);
            Contract.Requires(methodReturnType != null);

            return(ensureAssertions
                   .Any(e => e.ChecksForNotNull(
                            pa => pa.With(x => x as ContractResultPredicateArgument)
                            .With(x => x.ResultTypeName.FullName) == methodReturnType.GetClrName().FullName)));
        }
Exemplo n.º 5
0
        private Type GetParameterType(IType type)
        {
            IDeclaredType declaredType = type.GetScalarType();

            if (declaredType != null)
            {
                return(Type.GetType(declaredType.GetClrName().FullName));
            }

            // todo temp hack
            return(typeof(Type));
        }
Exemplo n.º 6
0
        static bool IsValidFieldOfType(this IDeclaredElement element, Type type)
        {
            IDeclaredType fieldType = element.GetValidatedFieldType();

            if (fieldType == null)
            {
                return(false);
            }

#if RESHARPER_6
            return(fieldType.GetClrName().FullName == type.FullName);
#else
            return(new CLRTypeName(fieldType.GetCLRName()) == new CLRTypeName(type.FullName));
#endif
        }
Exemplo n.º 7
0
        private void AppendDeclaredType([NotNull] IDeclaredType declaredType, QualifierDisplays expectedQualifierDisplay, Context context)
        {
            if (declaredType.IsNullable())
            {
                IType underlyingType = declaredType.GetNullableUnderlyingType();
                if (underlyingType != null)
                {
                    AppendTypeWithoutModule(underlyingType, expectedQualifierDisplay, context);
                    AppendText("?", _highlighterIdProvider.Operator);
                    return;
                }
            }

            if (declaredType is IDynamicType)
            {
                AppendText("dynamic", _highlighterIdProvider.Keyword);
                return;
            }

            if (context.Options.UseTypeKeywords)
            {
                string typeKeyword = CSharpTypeFactory.GetTypeKeyword(declaredType.GetClrName());
                if (typeKeyword != null)
                {
                    AppendText(typeKeyword, _highlighterIdProvider.Keyword);
                    return;
                }
            }
            else if (declaredType.IsVoid())
            {
                AppendText("void", _highlighterIdProvider.Keyword);
                return;
            }

            ITypeElement typeElement = declaredType.GetTypeElement();

            if (typeElement == null || !typeElement.IsValid())
            {
                PsiLanguageType language = CSharpLanguage.Instance ?? (PsiLanguageType)UnknownLanguage.Instance;
                AppendText(declaredType.GetPresentableName(language), null);
            }
            else
            {
                AppendTypeElement(typeElement, declaredType.GetSubstitution(), expectedQualifierDisplay, context);
            }
        }
Exemplo n.º 8
0
        /// <summary>Creates a specific catch clause with given <paramref name="exceptionType"/> and <paramref name="catchBody"/>.</summary>
        /// <param name="exceptionType">Type of the exception to catch.</param>
        /// <param name="catchBody">Body of the created catch.</param>
        /// <param name="variableName">A name for catch variable.</param>
        public ISpecificCatchClause CreateSpecificCatchClause(IDeclaredType exceptionType, IBlock catchBody, string variableName)
        {
            var tryStatement = _factory.CreateStatement("try {} catch(Exception $0) {}", variableName) as ITryStatement;

            if (tryStatement == null)
            {
                return(null);
            }

            var catchClause = tryStatement.Catches[0] as ISpecificCatchClause;

            if (catchClause == null)
            {
                return(null);
            }

            if (catchBody == null)
            {
                catchBody = _factory.CreateBlock("{$1    // TODO: Handle the $0 $1}",
                                                 exceptionType.GetClrName().ShortName, Environment.NewLine);
            }

            if (exceptionType != null)
            {
                var exceptionDeclaration = catchClause.ExceptionDeclaration;
                if (exceptionDeclaration == null)
                {
                    return(null);
                }

#if R8
                var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType);
#else
                var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType, catchBody);
#endif
#if R2016_3
                catchClause.SetExceptionTypeUsage(declaredTypeUsageNode);
#else
                exceptionDeclaration.SetDeclaredTypeUsage(declaredTypeUsageNode);
#endif
            }

            catchClause.SetBody(catchBody);
            return(catchClause);
        }
Exemplo n.º 9
0
        private void AppendDeclaredType([NotNull] IDeclaredType declaredType, NamespaceDisplays expectedNamespaceDisplay)
        {
            if (declaredType.IsNullable())
            {
                IType underlyingType = declaredType.GetNullableUnderlyingType();
                if (underlyingType != null)
                {
                    AppendType(underlyingType, expectedNamespaceDisplay);
                    AppendText("?", VsHighlightingAttributeIds.Operator);
                    return;
                }
            }

            if (declaredType is IDynamicType)
            {
                AppendText("dynamic", VsHighlightingAttributeIds.Keyword);
                return;
            }

            if (_options.UseTypeKeywords)
            {
                string typeKeyword = CSharpTypeFactory.GetTypeKeyword(declaredType.GetClrName());
                if (typeKeyword != null)
                {
                    AppendText(typeKeyword, VsHighlightingAttributeIds.Keyword);
                    return;
                }
            }
            else if (declaredType.IsVoid())
            {
                AppendText("void", VsHighlightingAttributeIds.Keyword);
                return;
            }

            ITypeElement typeElement = declaredType.GetTypeElement();

            if (typeElement == null || !typeElement.IsValid())
            {
                AppendText(declaredType.GetPresentableName(UnknownLanguage.Instance), null);
                return;
            }

            AppendTypeElement(typeElement, declaredType.GetSubstitution(), expectedNamespaceDisplay);
        }
Exemplo n.º 10
0
        /// <summary>Creates a try statement for the given exception type and variable name. </summary>
        /// <param name="exceptionType">The exception type. </param>
        /// <param name="exceptionVariableName">The exception variable name. </param>
        /// <param name="context">The context. </param>
        /// <returns>The try statement. </returns>
        public ITryStatement CreateTryStatement(IDeclaredType exceptionType, string exceptionVariableName, ITreeNode context)
        {
            var tryStatement = _factory.CreateStatement("try {} catch($0 $1) {}",
                                                        exceptionType.GetClrName().ShortName, exceptionVariableName) as ITryStatement;

            if (tryStatement == null)
            {
                return(null);
            }

            var catchClause = tryStatement.Catches[0] as ISpecificCatchClause;

            if (catchClause == null)
            {
                return(tryStatement);
            }

            var exceptionDeclaration = catchClause.ExceptionDeclaration;

            if (exceptionDeclaration == null)
            {
                return(tryStatement);
            }

#if R8
            var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType);
#else
            var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType, context);
#endif
#if R2016_3
            catchClause.SetExceptionTypeUsage(declaredTypeUsageNode);
#else
            exceptionDeclaration.SetDeclaredTypeUsage(declaredTypeUsageNode);
#endif

            return(tryStatement);
        }
Exemplo n.º 11
0
            public override Expression VisitTypeofExpression(ITypeofExpression typeofExpressionParam, IMetadataResolver context)
            {
                IDeclaredType declaredType = typeofExpressionParam.ArgumentType.GetScalarType();

                if (declaredType == null || !declaredType.IsValid())
                {
                    return(base.VisitTypeofExpression(typeofExpressionParam, context));
                }

                string assemblyName = GetTypeAssemblyFullName(declaredType);

#if DEBUG
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    Console.WriteLine(assemblyName);
                }
#endif

                string fullyQualifiedTypeName = string.Format("{0}, {1}", declaredType.GetClrName().FullName, assemblyName);

                Type value = Type.GetType(fullyQualifiedTypeName);

                return(Expression.Constant(value, typeof(Type)));
            }
Exemplo n.º 12
0
 internal static bool IsAssignableFrom(this Type type, IDeclaredType c)
 {
     return type.FullName == c.GetClrName().FullName || c.GetAllSuperTypes().Any(superType => type.FullName == superType.GetClrName().FullName);
 }
Exemplo n.º 13
0
 internal static bool IsAssignableFrom(this Type type, IDeclaredType c)
 {
     return(type.FullName == c.GetClrName().FullName || c.GetAllSuperTypes().Any(superType => type.FullName == superType.GetClrName().FullName));
 }
Exemplo n.º 14
0
        private StaticDeclaredTypeWrapper MakeDeclaredType(IDeclaredType typeHandle)
        {
            return declaredTypeMemoizer.Memoize(typeHandle, () =>
            {
                ITypeElement typeElement = typeHandle.GetTypeElement();
                if (typeElement == null)
                    throw new ReflectionResolveException(
                        String.Format(
                            "Cannot obtain type element for type '{0}' possibly because its source code is not available.",
#if RESHARPER_60_OR_NEWER
                            typeHandle.GetClrName()));
#else
                            typeHandle.GetCLRName()));
#endif

                return MakeDeclaredType(typeElement, typeHandle.GetSubstitution());
            });
Exemplo n.º 15
0
 public static bool IsGenericMicrosoftExtensionsLogger([NotNull] this IDeclaredType declared)
 {
     return(declared.GetClrName().FullName == "Microsoft.Extensions.Logging.ILogger`1");
 }
        /// <summary>Creates a try statement for the given exception type and variable name. </summary>
        /// <param name="exceptionType">The exception type. </param>
        /// <param name="exceptionVariableName">The exception variable name. </param>
        /// <param name="context">The context. </param>
        /// <returns>The try statement. </returns>
        public ITryStatement CreateTryStatement(IDeclaredType exceptionType, string exceptionVariableName, ITreeNode context)
        {
            var tryStatement = _factory.CreateStatement("try {} catch($0 $1) {}",
                exceptionType.GetClrName().ShortName, exceptionVariableName) as ITryStatement;
            if (tryStatement == null)
                return null;

            var catchClause = tryStatement.Catches[0] as ISpecificCatchClause;
            if (catchClause == null)
                return tryStatement;

            var exceptionDeclaration = catchClause.ExceptionDeclaration;
            if (exceptionDeclaration == null)
                return tryStatement;

            #if R8
            var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType);
            #else
            var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType, context);
            #endif

            exceptionDeclaration.SetDeclaredTypeUsage(declaredTypeUsageNode);

            return tryStatement;
        }
        protected virtual bool ResultIsAlreadyCheckedByContractEnsures(
            IEnumerable<ContractEnsures> ensureAssertions, IDeclaredType methodReturnType)
        {
            Contract.Requires(ensureAssertions != null);
            Contract.Requires(methodReturnType != null);

            return ensureAssertions
                .Any(e => e.ChecksForNotNull(
                    pa => pa.With(x => x as ContractResultPredicateArgument)
                        .With(x => x.ResultTypeName.FullName) == methodReturnType.GetClrName().FullName));
        }
        /// <summary>Creates a specific catch clause with given <paramref name="exceptionType"/> and <paramref name="catchBody"/>.</summary>
        /// <param name="exceptionType">Type of the exception to catch.</param>
        /// <param name="catchBody">Body of the created catch.</param>
        /// <param name="variableName">A name for catch variable.</param>
        public ISpecificCatchClause CreateSpecificCatchClause(IDeclaredType exceptionType, IBlock catchBody, string variableName)
        {
            var tryStatement = _factory.CreateStatement("try {} catch(Exception $0) {}", variableName) as ITryStatement;
            if (tryStatement == null)
                return null;

            var catchClause = tryStatement.Catches[0] as ISpecificCatchClause;
            if (catchClause == null)
                return null;

            if (catchBody == null)
            {
                catchBody = _factory.CreateBlock("{$1    // TODO: Handle the $0 $1}",
                    exceptionType.GetClrName().ShortName, Environment.NewLine);
            }

            if (exceptionType != null)
            {
                var exceptionDeclaration = catchClause.ExceptionDeclaration;
                if (exceptionDeclaration == null)
                    return null;

            #if R8
                var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType);
            #else
                var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType, catchBody);

            #endif
                exceptionDeclaration.SetDeclaredTypeUsage(declaredTypeUsageNode);
            }

            catchClause.SetBody(catchBody);
            return catchClause;
        }