Beispiel #1
0
        private static ICSharpFile GetLinkedTypeFile(string linkedTypeName, string linkedTypeNamespace, ITypeElement templateLinkedType)
        {
            var elementFactory = CSharpElementFactory.GetInstance(templateLinkedType.GetFirstDeclaration <IDeclaration>().NotNull());
            var templateLinkedTypeSourceFile = templateLinkedType.GetSingleOrDefaultSourceFile().NotNull("templateLinkedTypeSourceFile != null");
            var templateFile = templateLinkedTypeSourceFile.GetPrimaryPsiFile().NotNull("templateFile != null");

            var fileText = templateFile.GetText()
                           .Replace(templateLinkedType.GetContainingNamespace().QualifiedName, linkedTypeNamespace)
                           .Replace(templateLinkedType.ShortName, linkedTypeName);

            var linkedTypeFile = elementFactory.CreateFile(fileText);

            var typeDeclarations = GetTypeDeclarations(linkedTypeFile);
            var linkedType       = (IClassDeclaration)typeDeclarations.Single(x => x.DeclaredName == linkedTypeName);

            // Remove base types
            foreach (var x in linkedType.SuperTypes)
            {
                linkedType.RemoveSuperInterface(x);
            }

            // Clear body
            linkedType.SetBody(((IClassLikeDeclaration)elementFactory.CreateTypeMemberDeclaration("class C{}")).Body);

            // Remove unrelated types
            foreach (var declaration in linkedTypeFile.TypeDeclarations.Where(x => x.DeclaredName != linkedTypeName))
            {
                ModificationUtil.DeleteChild(declaration);
            }

            return(linkedTypeFile);
        }
        public bool SuppressUsageInspectionsOnElement(IDeclaredElement element, out ImplicitUseKindFlags flags)
        {
            flags = ImplicitUseKindFlags.Default;
            ITypeElement typeElement = element as ITypeElement;

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

            var            patternManager = typeElement.GetSolution().GetComponent <IPatternManager>();
            IPsiSourceFile file           = typeElement.GetSingleOrDefaultSourceFile();

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

            IEnumerable <RegistrationInfo> registrations = patternManager.GetRegistrationsForFile(file);

            if (registrations.Any(info => info.Registration.IsSatisfiedBy(typeElement)))
            {
                flags = ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature;
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        public static string GetDerivedNamespace(ITypeElement sourceType, ITypeElement templateLinkedType)
        {
            var sourceDefaultNamespace = GetDefaultNamespace(sourceType.GetSingleOrDefaultSourceFile().GetProject().NotNull());
            var linkedDefaultNamespace = GetDefaultNamespace(templateLinkedType.GetSingleOrDefaultSourceFile().GetProject().NotNull());
            var sourceNamespaceTail    = sourceType.GetContainingNamespace().QualifiedName.TrimFromStart(sourceDefaultNamespace);

            return(linkedDefaultNamespace + sourceNamespaceTail);
        }