Beispiel #1
0
        public ImmutableArray <ITypeSymbol> GetBindFromTypes(ITypeSymbol type)
        {
            var result = new List <ITypeSymbol>();

            foreach (var specificType in SpecificTypes)
            {
                if (_strictMode || type.CanBeCastedTo(specificType))
                {
                    result.Add(specificType);
                }
            }

            return(result.ToImmutableArray <ITypeSymbol>());
        }
Beispiel #2
0
        private void CheckForFromAndToTypes(
            List <ITypeSymbol> bindFromTypeSemantics,
            ITypeSymbol bindToTypeSemantic,
            bool toFactory
            )
        {
            if (bindFromTypeSemantics is null)
            {
                throw new ArgumentNullException(nameof(bindFromTypeSemantics));
            }

            if (bindToTypeSemantic is null)
            {
                throw new ArgumentNullException(nameof(bindToTypeSemantic));
            }

            //check for target type correct
            if (bindToTypeSemantic.TypeKind.NotIn(TypeKind.Class, TypeKind.Struct))
            {
                throw new DpdtException(
                          DpdtExceptionTypeEnum.IncorrectBinding_IncorrectTarget,
                          $"Type [{bindToTypeSemantic.ToDisplayString()}] is not a class or struct",
                          bindToTypeSemantic.ToDisplayString()
                          );
            }

            if (!toFactory)
            {
                //it's not a factory

                //check for cast exists
                foreach (var bindFromSemantic in bindFromTypeSemantics)
                {
                    if (!bindToTypeSemantic.CanBeCastedTo(bindFromSemantic.ToDisplayString()))
                    {
                        throw new DpdtException(
                                  DpdtExceptionTypeEnum.IncorrectBinding_IncorrectFrom,
                                  $"Type [{bindToTypeSemantic.ToDisplayString()}] cannot be casted to [{bindFromSemantic.ToDisplayString()}]",
                                  bindToTypeSemantic.ToDisplayString()
                                  );
                    }
                }
            }
            else
            {
                //it's a factory
                if (bindFromTypeSemantics.Count > 1)
                {
                    throw new DpdtException(
                              DpdtExceptionTypeEnum.IncorrectBinding_IncorrectFrom,
                              $"It is allowed only from bind from type for factory bindings. Take a look to [{bindToTypeSemantic.ToDisplayString()}] binding.",
                              bindToTypeSemantic.ToDisplayString()
                              );
                }

                //check for partial clause
                if (bindToTypeSemantic.DeclaringSyntaxReferences.Length > 1)
                {
                    //it's partial!
                }
                else
                {
                    var syntax = bindToTypeSemantic.DeclaringSyntaxReferences[0].GetSyntax();
                    if (syntax is ClassDeclarationSyntax cds)
                    {
                        if (!cds.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)))
                        {
                            throw new DpdtException(
                                      DpdtExceptionTypeEnum.TargetClassMustBePartial,
                                      $"Type [{bindToTypeSemantic.ToDisplayString()}] must be partial",
                                      bindToTypeSemantic.ToDisplayString()
                                      );
                        }
                    }
                }
            }
        }