diff --git a/WpfExtended.SourceGeneration.Tests/Class1.cs b/WpfExtended.SourceGeneration.Tests/Class1.cs index e689303..cc79248 100644 --- a/WpfExtended.SourceGeneration.Tests/Class1.cs +++ b/WpfExtended.SourceGeneration.Tests/Class1.cs @@ -5,7 +5,7 @@ using System.Windows.Media.Effects; namespace WpfExtended.SourceGeneration.Tests { - public partial class Class1 : UserControl + internal partial class Class1 : UserControl { [GenerateDependencyProperty] public int someF; @@ -15,6 +15,7 @@ namespace WpfExtended.SourceGeneration.Tests public Effect effect; + public void TestValues() { this.SomeF.Should().Be(0); diff --git a/WpfExtended.SourceGeneration/DependencyPropertyGenerator.cs b/WpfExtended.SourceGeneration/DependencyPropertyGenerator.cs index 345fadb..7bb609a 100644 --- a/WpfExtended.SourceGeneration/DependencyPropertyGenerator.cs +++ b/WpfExtended.SourceGeneration/DependencyPropertyGenerator.cs @@ -102,8 +102,8 @@ namespace System.Extensions .WithNamespace( new NamespaceTemplate() .WithNamespace(namespaceName)) - .WithModifiers(Modifier.Public, Modifier.Partial) .WithName(classSymbol.Name); + AssignModifiers(source, classSymbol); foreach (var fieldSymbol in fields) { ProcessField(source, classSymbol, attributeSymbol, fieldSymbol); @@ -112,6 +112,15 @@ namespace System.Extensions return source.GenerateString(); } + private static void AssignModifiers(ClassTemplate source, INamedTypeSymbol classSymbol) + { + //Find class declaration syntax and parse modifiers. + if (classSymbol.DeclaringSyntaxReferences.Select(sr => sr.GetSyntax()).OfType().First() is ClassDeclarationSyntax classDeclarationSyntax) + { + source.WithModifiers(classDeclarationSyntax.Modifiers.Select(m => Modifier.Parse(m.ValueText)).ToArray()); + } + } + private static void ProcessField(ClassTemplate source, INamedTypeSymbol classSymbol, INamedTypeSymbol attributeSymbol, IFieldSymbol fieldSymbol) { var fieldName = fieldSymbol.Name; diff --git a/WpfExtended.SourceGeneration/Templates/ClassTemplate.cs b/WpfExtended.SourceGeneration/Templates/ClassTemplate.cs index a11d3df..610c26f 100644 --- a/WpfExtended.SourceGeneration/Templates/ClassTemplate.cs +++ b/WpfExtended.SourceGeneration/Templates/ClassTemplate.cs @@ -43,6 +43,11 @@ namespace System.Extensions.Templates this.Modifiers.AddRange(modifiers); return this; } + public ClassTemplate WithModifier(Modifier modifier) + { + this.Modifiers.Add(modifier); + return this; + } public ClassTemplate WithMethods(params MethodTemplate[] methodTemplates) { this.Methods.Clear(); diff --git a/WpfExtended.SourceGeneration/Templates/Modifiers.cs b/WpfExtended.SourceGeneration/Templates/Modifiers.cs index e3745d4..bf1d0e6 100644 --- a/WpfExtended.SourceGeneration/Templates/Modifiers.cs +++ b/WpfExtended.SourceGeneration/Templates/Modifiers.cs @@ -1,7 +1,11 @@ -namespace System.Extensions.Templates +using System.Collections; +using System.Collections.Generic; + +namespace System.Extensions.Templates { internal sealed class Modifier : AbstractTemplate { + public static Modifier Protected { get; } = new Modifier { Value = "protected" }; public static Modifier Readonly { get; } = new Modifier { Value = "readonly" }; public static Modifier Static { get; } = new Modifier { Value = "static" }; public static Modifier Public { get; } = new Modifier { Value = "public" }; @@ -22,5 +26,32 @@ { codeWriter.Append(this.Value); } + + public static bool TryParse(string value, out Modifier modifier) + { + foreach(var supportedModifier in SupportedModifiers) + { + if (supportedModifier.Value.Equals(value, StringComparison.Ordinal)) + { + modifier = supportedModifier; + return true; + } + } + + modifier = null; + return false; + } + + public static Modifier Parse(string value) + { + if (TryParse(value, out var modifier)) + { + return modifier; + } + + throw new InvalidOperationException($"Could not find a modifier with value {value}"); + } + + private static IEnumerable SupportedModifiers { get; } = new List { Protected, Readonly, Static, Internal, Public, Private, Abstract, Virtual, Partial, Sealed }; } }