private static bool MethodResultIsCompatibleWith(IType methodResult, IList<IDeclaredType> contractResultTypes)
        {
            // Method return true for "object" <- "string"
            // but false for "string" <- "object"
            var rule = new CSharpTypeConversionRule(methodResult.Module);
            return
                contractResultTypes
                .All(contractResult =>
                {
                    var resultType = methodResult;
                    var contractResultType = contractResult;

                    // Corner case: we can use Contract.Result<object>() for method that returns Task<string>!
                    if (resultType.IsGenericTask() && !contractResultType.IsGenericTask())
                    {
                        resultType = resultType.GetTaskUnderlyingType();
                    }
                    
                    Contract.Assert(resultType != null);

                    return resultType.IsImplicitlyConvertibleTo(contractResult, rule);
                });
        }
        private static bool FilterToApplicableProperties(ISymbolInfo symbolInfo)
        {
            var declaredElement = symbolInfo.GetDeclaredElement() as ITypeMember;

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

            var predefinedType = declaredElement.GetPredefinedType();

            var property = declaredElement as IProperty;

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

            if (property.GetAccessRights() != AccessRights.PUBLIC || !property.IsStatic)
            {
                return(false);
            }

            var conversionRule = new CSharpTypeConversionRule(property.Module);

            var genericEnumerableTypeElement = predefinedType.GenericIEnumerable.GetTypeElement();

            if (genericEnumerableTypeElement == null)
            {
                return(false);
            }
            var objectArrayType = TypeFactory.CreateArrayType(predefinedType.Object, 1);
            var x = EmptySubstitution.INSTANCE.Extend(genericEnumerableTypeElement.TypeParameters, new[] { objectArrayType }).Apply(predefinedType.GenericIEnumerable);

            return(property.Type.IsImplicitlyConvertibleTo(x, conversionRule));
        }
示例#3
0
        private static bool MethodResultIsCompatibleWith(IType methodResult, IList <IDeclaredType> contractResultTypes)
        {
            // Method return true for "object" <- "string"
            // but false for "string" <- "object"
            var rule = new CSharpTypeConversionRule(methodResult.Module);

            return
                (contractResultTypes
                 .All(contractResult =>
            {
                var resultType = methodResult;
                var contractResultType = contractResult;

                // Corner case: we can use Contract.Result<object>() for method that returns Task<string>!
                if (resultType.IsGenericTask() && !contractResultType.IsGenericTask())
                {
                    resultType = resultType.GetTaskUnderlyingType();
                }

                Contract.Assert(resultType != null);

                return resultType.IsImplicitlyConvertibleTo(contractResult, rule);
            }));
        }