public static INPCInfo GetINPCedInfo(ContextInfo info, INamedTypeSymbol classSymbol) =>
 new INPCInfo(classSymbol,
              info.INPCedSymbol,
              symbol => AttributeHelper.HasAttribute(symbol, info.ViewModelAttributeSymbol),
              "RaisePropertyChanged",
              "System.ComponentModel.PropertyChangedEventArgs",
              "protected void RaisePropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);");
 public static INPCInfo GetINPCingInfo(ContextInfo info, INamedTypeSymbol classSymbol) =>
 new INPCInfo(classSymbol,
              info.INPCingSymbol,
              symbol => AttributeHelper.HasAttribute(symbol, info.ViewModelAttributeSymbol) &&
              AttributeHelper.GetPropertyActualValue(symbol, info.ViewModelAttributeSymbol, AttributesGenerator.ImplementINPCing, false),
              "RaisePropertyChanging",
              "System.ComponentModel.PropertyChangingEventArgs",
              "protected void RaisePropertyChanging(PropertyChangingEventArgs e) => PropertyChanging?.Invoke(this, e);");
Ejemplo n.º 3
0
        public void Execute(GeneratorExecutionContext context)
        {
            if (context.SyntaxReceiver is not SyntaxReceiver receiver)
            {
                return;
            }

            var attributesSourceText = SourceText.From(InitializationGenerator.GetSourceCode(), Encoding.UTF8);

            context.AddSource(CreateFileName("Attributes"), attributesSourceText);
            var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(attributesSourceText));
            var contextInfo = new ContextInfo(context, compilation);

            var generatedClasses = new List <string>();

            foreach (var classSyntax in receiver.ClassSyntaxes)
            {
                var classSymbol = contextInfo.Compilation.GetSemanticModel(classSyntax.SyntaxTree).GetDeclaredSymbol(classSyntax);

                if (generatedClasses.Contains(classSymbol.Name))
                {
                    continue;
                }

                if (!AttributeHelper.HasAttribute(classSymbol, contextInfo.ViewModelAttributeSymbol))
                {
                    continue;
                }

                if (classSymbol.IsGenericType)
                {
                    context.ReportGenericViewModel(classSymbol);
                    continue;
                }

                if (!classSyntax.Modifiers.Any(x => x.ValueText == "partial"))
                {
                    context.ReportNoPartialModifier(classSymbol);
                    continue;
                }

                if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default))
                {
                    context.ReportClassWithinClass(classSymbol);
                    continue;
                }

                var classGenerator = new ClassGenerator(contextInfo, classSymbol);
                var classSource    = classGenerator.GetSourceCode();
                context.AddSource(CreateFileName(classSymbol.Name), SourceText.From(classSource, Encoding.UTF8));
                generatedClasses.Add(classSymbol.Name);
            }
        }
Ejemplo n.º 4
0
 static IEnumerable <T> GetProcessingMembers <T>(INamedTypeSymbol classSymbol, INamedTypeSymbol attributeSymbol) where T : ISymbol =>
 classSymbol.GetMembers()
 .OfType <T>()
 .Where(symbol => AttributeHelper.HasAttribute(symbol, attributeSymbol));