├── PropertyInterception ├── PropertyInterception │ ├── Properties │ │ └── launchSettings.json │ ├── Interfaces │ │ ├── IExitInterceptor.cs │ │ ├── IPropertyGetterInterceptor.cs │ │ ├── IExceptionInterceptor.cs │ │ └── IPropertySetterInterceptor.cs │ ├── FieldContainer.cs │ ├── PropertyInterception.csproj │ ├── PropertyInterceptionSyntaxReceiver.cs │ ├── PropertyInterceptionInfo.cs │ └── PropertyInterceptionGenerator.cs ├── PropertyInterception.Tests │ ├── GeneratorTargets │ │ └── DummyObject.cs │ ├── PropertyInterception.Tests.csproj │ ├── Attributes │ │ ├── GetAttribute.cs │ │ ├── SetAttribute.cs │ │ ├── NormalAttribute.cs │ │ ├── ExceptionAttribute.cs │ │ └── ExitAttribute.cs │ ├── PropertyInterceptionGeneratorTests.cs │ └── PropertyInterceptionInfoTests.cs └── PropertyInterception.sln ├── LICENSE ├── README.md └── .gitignore /PropertyInterception/PropertyInterception/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "Debug Examples": { 4 | "commandName": "DebugRoslynComponent", 5 | "targetProject": "..\\PropertyInterception.Examples\\PropertyInterception.Examples.csproj" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception/Interfaces/IExitInterceptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace PropertyInterception.Interfaces 6 | { 7 | public interface IExitInterceptor 8 | { 9 | /// 10 | /// Gets triggered after a Setter/Getter is exited. 11 | /// 12 | void OnExit(PropertyInterceptionInfo propertyInterceptionInfo); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception/Interfaces/IPropertyGetterInterceptor.cs: -------------------------------------------------------------------------------- 1 | namespace PropertyInterception.Interfaces 2 | { 3 | public interface IPropertyGetterInterceptor : IExceptionInterceptor, IExitInterceptor 4 | { 5 | /// 6 | /// Gets called before the value of the backingfield is returned by the Getter. 7 | /// 8 | void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object currentValue); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception/Interfaces/IExceptionInterceptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PropertyInterception.Interfaces 4 | { 5 | public interface IExceptionInterceptor 6 | { 7 | /// 8 | /// Gets triggered when a Exception in a Get/Set-Block gets thrown. Return true to throw the Exception or false to ignore it. 9 | /// 10 | bool OnException(PropertyInterceptionInfo propertyInterceptionInfo,Exception exception); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception/Interfaces/IPropertySetterInterceptor.cs: -------------------------------------------------------------------------------- 1 | namespace PropertyInterception.Interfaces 2 | { 3 | public interface IPropertySetterInterceptor : IExceptionInterceptor, IExitInterceptor 4 | { 5 | /// 6 | /// Gets called before the value of the backingfield is set by the Setter. Return true to set the backingfield to the newValue or false to ignore the newValue. 7 | /// 8 | bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.Tests/GeneratorTargets/DummyObject.cs: -------------------------------------------------------------------------------- 1 | using PropertyInterception.Tests.Attributes; 2 | 3 | namespace PropertyInterception.Tests.GeneratorTargets 4 | { 5 | public partial class DummyObject 6 | { 7 | 8 | public bool TriggeredOnExit; 9 | public bool TriggeredException; 10 | 11 | [Normal] 12 | private string normalProperty; 13 | [Get] 14 | private string getProperty; 15 | [Set] 16 | private string setProperty; 17 | [Exit] 18 | private string onExitProperty; 19 | [Exception] 20 | private string onExceptionProperty; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.Tests/PropertyInterception.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.Tests/Attributes/GetAttribute.cs: -------------------------------------------------------------------------------- 1 | using PropertyInterception.Interfaces; 2 | using System; 3 | 4 | namespace PropertyInterception.Tests.Attributes 5 | { 6 | [AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false)] 7 | internal class GetAttribute : Attribute, IPropertyGetterInterceptor 8 | { 9 | public bool OnException(PropertyInterceptionInfo propertyInterceptionInfo,Exception exception) 10 | { 11 | return true; 12 | } 13 | 14 | public void OnExit(PropertyInterceptionInfo propertyInterceptionInfo) 15 | { 16 | 17 | } 18 | 19 | public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object currentValue) 20 | { 21 | propertyInterceptionInfo.SetValue("GetAttribute"); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.Tests/Attributes/SetAttribute.cs: -------------------------------------------------------------------------------- 1 | using PropertyInterception.Interfaces; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace PropertyInterception.Tests.Attributes 9 | { 10 | internal class SetAttribute : Attribute, IPropertySetterInterceptor 11 | { 12 | public bool OnException(PropertyInterceptionInfo propertyInterceptionInfo,Exception exception) 13 | { 14 | return true; 15 | } 16 | 17 | public void OnExit(PropertyInterceptionInfo propertyInterceptionInfo) 18 | { 19 | 20 | } 21 | 22 | public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue) 23 | { 24 | propertyInterceptionInfo.SetValue("SetAttribute"); 25 | return false; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception/FieldContainer.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using PropertyInterception.Interfaces; 3 | using System.Linq; 4 | 5 | namespace PropertyInterception 6 | { 7 | internal class FieldContainer 8 | { 9 | public IFieldSymbol FieldSymbol { get; } 10 | public AttributeData AttributeData { get; } 11 | 12 | public bool ShouldGenerateSetter { get; } 13 | 14 | public bool ShouldGenerateGetter { get; } 15 | 16 | public FieldContainer(IFieldSymbol fieldSymbol,AttributeData attributeData) 17 | { 18 | FieldSymbol=fieldSymbol; 19 | AttributeData=attributeData; 20 | var interfaces = AttributeData.AttributeClass.AllInterfaces; 21 | ShouldGenerateSetter = interfaces.Any(x => x.MetadataName == typeof(IPropertySetterInterceptor).Name); 22 | ShouldGenerateGetter = interfaces.Any(x => x.MetadataName == typeof(IPropertyGetterInterceptor).Name); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 LLukas22 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.Tests/Attributes/NormalAttribute.cs: -------------------------------------------------------------------------------- 1 | using PropertyInterception.Interfaces; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace PropertyInterception.Tests.Attributes 9 | { 10 | [AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false)] 11 | internal class NormalAttribute : Attribute, IPropertyGetterInterceptor, IPropertySetterInterceptor 12 | { 13 | public bool OnException(PropertyInterceptionInfo propertyInterceptionInfo,Exception exception) 14 | { 15 | return true; 16 | } 17 | 18 | public void OnExit(PropertyInterceptionInfo propertyInterceptionInfo) 19 | { 20 | 21 | } 22 | 23 | public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object currentValue) 24 | { 25 | 26 | } 27 | 28 | public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue) 29 | { 30 | return true; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.Tests/Attributes/ExceptionAttribute.cs: -------------------------------------------------------------------------------- 1 | using PropertyInterception.Interfaces; 2 | using PropertyInterception.Tests.GeneratorTargets; 3 | using System; 4 | 5 | namespace PropertyInterception.Tests.Attributes 6 | { 7 | internal class ExceptionAttribute : Attribute, IPropertyGetterInterceptor, IPropertySetterInterceptor 8 | { 9 | public bool OnException(PropertyInterceptionInfo propertyInterceptionInfo,Exception exception) 10 | { 11 | (propertyInterceptionInfo.Instance as DummyObject).TriggeredException = true; 12 | //Void the Exception 13 | return false; 14 | } 15 | 16 | public void OnExit(PropertyInterceptionInfo propertyInterceptionInfo) 17 | { 18 | 19 | } 20 | 21 | public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object currentValue) 22 | { 23 | throw new Exception(); 24 | } 25 | 26 | public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue) 27 | { 28 | throw new Exception(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.Tests/Attributes/ExitAttribute.cs: -------------------------------------------------------------------------------- 1 | using PropertyInterception.Interfaces; 2 | using PropertyInterception.Tests.GeneratorTargets; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace PropertyInterception.Tests.Attributes 10 | { 11 | [AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false)] 12 | internal class ExitAttribute : Attribute, IPropertyGetterInterceptor, IPropertySetterInterceptor 13 | { 14 | public bool OnException(PropertyInterceptionInfo propertyInterceptionInfo,Exception exception) 15 | { 16 | return true; 17 | } 18 | 19 | public void OnExit(PropertyInterceptionInfo propertyInterceptionInfo) 20 | { 21 | (propertyInterceptionInfo.Instance as DummyObject).TriggeredOnExit = true; 22 | } 23 | 24 | public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object currentValue) 25 | { 26 | 27 | } 28 | 29 | public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue) 30 | { 31 | return true; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception/PropertyInterception.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | true 6 | preview 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | true 18 | 19 | true 20 | Cauldron like Property-Interception using C# Source-Generators 21 | https://github.com/LLukas22/PropertyInterception 22 | https://github.com/LLukas22/PropertyInterception 23 | source-generators;property-interception 24 | Lukas Kreussel 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31717.71 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PropertyInterception", "PropertyInterception\PropertyInterception.csproj", "{A98018F6-4A6F-4789-B34D-CA0A7615FB32}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PropertyInterception.Tests", "PropertyInterception.Tests\PropertyInterception.Tests.csproj", "{1E7E5CEC-92E0-4F5C-8044-161F0CC5334E}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {A98018F6-4A6F-4789-B34D-CA0A7615FB32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {A98018F6-4A6F-4789-B34D-CA0A7615FB32}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {A98018F6-4A6F-4789-B34D-CA0A7615FB32}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {A98018F6-4A6F-4789-B34D-CA0A7615FB32}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {1E7E5CEC-92E0-4F5C-8044-161F0CC5334E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {1E7E5CEC-92E0-4F5C-8044-161F0CC5334E}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {1E7E5CEC-92E0-4F5C-8044-161F0CC5334E}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {1E7E5CEC-92E0-4F5C-8044-161F0CC5334E}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {9C36D325-2AB6-4333-A266-B2CEA5A84ABF} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception/PropertyInterceptionSyntaxReceiver.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CSharp.Syntax; 3 | using PropertyInterception.Interfaces; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace PropertyInterception 8 | { 9 | /// 10 | /// Created on demand before each generation pass 11 | /// 12 | internal class PropertyInterceptionSyntaxReceiver : ISyntaxContextReceiver 13 | { 14 | public List Fields { get; } = new List(); 15 | 16 | public void OnVisitSyntaxNode(GeneratorSyntaxContext context) 17 | { 18 | // any field with at least one attribute is a candidate for property generation 19 | if (context.Node is FieldDeclarationSyntax fieldDeclarationSyntax && fieldDeclarationSyntax.AttributeLists.Count > 0) 20 | { 21 | foreach (VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables) 22 | { 23 | // Get the symbol being declared by the field, and keep it if its annotated 24 | IFieldSymbol fieldSymbol = context.SemanticModel.GetDeclaredSymbol(variable) as IFieldSymbol; 25 | var attributes = fieldSymbol.GetAttributes(); 26 | 27 | if (attributes.Length > 1) 28 | continue; 29 | 30 | //Check if the Attribute implements IPropertyGetterInterceptor or IPropertySetterInterceptor 31 | if (attributes[0].AttributeClass.AllInterfaces.Any(i => (i.MetadataName == typeof(IPropertyGetterInterceptor).Name || i.MetadataName == typeof(IPropertySetterInterceptor).Name))){ 32 | Fields.Add(new(fieldSymbol, attributes[0])); 33 | } 34 | } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception/PropertyInterceptionInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace PropertyInterception 5 | { 6 | /// 7 | /// Contains Property Information 8 | /// 9 | public class PropertyInterceptionInfo 10 | { 11 | /// 12 | /// Object Instance of the Parent 13 | /// 14 | public object Instance { get; private set; } 15 | /// 16 | /// Type of the Parent 17 | /// 18 | public Type DeclaringType => Instance.GetType(); 19 | /// 20 | /// Name of the Property 21 | /// 22 | public string PropertyName { get; private set; } 23 | /// 24 | /// Type of the Property 25 | /// 26 | public Type PropertyType => field?.FieldType; 27 | 28 | private FieldInfo field; 29 | public PropertyInterceptionInfo(object instace,string propertyName) 30 | { 31 | if(instace == null) 32 | { 33 | throw new ArgumentException($"Instace can't be null!"); 34 | } 35 | 36 | if (string.IsNullOrEmpty(propertyName)) 37 | { 38 | throw new ArgumentException($"Propertyname can't be empty!"); 39 | } 40 | 41 | this.Instance = instace; 42 | this.PropertyName = propertyName; 43 | 44 | field = DeclaringType.GetField(propertyName, BindingFlags.NonPublic | BindingFlags.Instance); 45 | 46 | if(field == null) 47 | { 48 | throw new ArgumentException($"Object of Type '{this.Instance.GetType()}' has no Field '{propertyName}'!"); 49 | } 50 | } 51 | 52 | /// 53 | /// Gets the current value of the Property 54 | /// 55 | public object GetValue() 56 | { 57 | return field?.GetValue(Instance); 58 | } 59 | 60 | /// 61 | /// Gets the current value of the Property 62 | /// 63 | public TTYpe GetValue() 64 | { 65 | return (TTYpe)field?.GetValue(Instance); 66 | } 67 | 68 | /// 69 | /// Sets the value of the Property 70 | /// 71 | public void SetValue(object value) 72 | { 73 | field.SetValue(Instance, value); 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.Tests/PropertyInterceptionGeneratorTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using PropertyInterception.Tests.GeneratorTargets; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace PropertyInterception.Tests 10 | { 11 | [TestFixture] 12 | public class PropertyInterceptionGeneratorTests 13 | { 14 | [Test] 15 | public void Generated_Get_Set_Should_Work() 16 | { 17 | var dummyObject = new DummyObject(); 18 | 19 | Assert.DoesNotThrow(() => 20 | 21 | dummyObject.NormalProperty = "foobar" 22 | ); 23 | Assert.AreEqual("foobar", dummyObject.NormalProperty); 24 | } 25 | 26 | 27 | [Test] 28 | public void Generated_Get_Should_Intercept_Getter() 29 | { 30 | var dummyObject = new DummyObject(); 31 | 32 | Assert.DoesNotThrow(() => 33 | 34 | dummyObject.GetProperty = "foobar" 35 | ); 36 | Assert.AreEqual("GetAttribute", dummyObject.GetProperty); 37 | } 38 | 39 | 40 | [Test] 41 | public void Generated_Set_Should_Intercept_Setter() 42 | { 43 | var dummyObject = new DummyObject(); 44 | 45 | Assert.DoesNotThrow(() => 46 | 47 | dummyObject.SetProperty = "foobar" 48 | ); 49 | Assert.AreEqual("SetAttribute", dummyObject.SetProperty); 50 | } 51 | 52 | 53 | [Test] 54 | public void OnExit_Is_Triggered_OnSet() 55 | { 56 | var dummyObject = new DummyObject(); 57 | 58 | Assert.DoesNotThrow(() => 59 | 60 | dummyObject.OnExitProperty = "foobar" 61 | ); 62 | Assert.IsTrue(dummyObject.TriggeredOnExit); 63 | } 64 | 65 | [Test] 66 | public void OnExit_Is_Triggered_OnGet() 67 | { 68 | var dummyObject = new DummyObject(); 69 | 70 | Assert.DoesNotThrow(() => 71 | { var test = dummyObject.OnExitProperty; } 72 | ); 73 | Assert.IsTrue(dummyObject.TriggeredOnExit); 74 | } 75 | 76 | 77 | [Test] 78 | public void OnException_Is_Triggered_OnGetException() 79 | { 80 | var dummyObject = new DummyObject(); 81 | 82 | Assert.DoesNotThrow(() => 83 | { var test = dummyObject.OnExceptionProperty; } 84 | ); 85 | Assert.IsTrue(dummyObject.TriggeredException); 86 | } 87 | 88 | [Test] 89 | public void OnException_Is_Triggered_OnSetException() 90 | { 91 | var dummyObject = new DummyObject(); 92 | 93 | Assert.DoesNotThrow(() => 94 | { dummyObject.OnExceptionProperty = "foobar"; } 95 | ); 96 | Assert.IsTrue(dummyObject.TriggeredException); 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception.Tests/PropertyInterceptionInfoTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | 4 | namespace PropertyInterception.Tests 5 | { 6 | [TestFixture] 7 | public class PropertyInterceptionInfoTests 8 | { 9 | 10 | class Child 11 | { 12 | public Guid Id; 13 | public Child() 14 | { 15 | Id = Guid.NewGuid(); 16 | } 17 | } 18 | 19 | class Person 20 | { 21 | public string Name => name; 22 | private string name; 23 | 24 | public Child Child => child; 25 | private Child child; 26 | } 27 | 28 | [Test] 29 | public void PropertyInterceptionInfo_Can_Set_Value() 30 | { 31 | var person = new Person(); 32 | var info = new PropertyInterceptionInfo(person,"name"); 33 | ; 34 | 35 | Assert.Multiple(() => 36 | { 37 | Assert.DoesNotThrow(() => info.SetValue("foobar")); 38 | Assert.AreEqual("foobar", person.Name); 39 | }); 40 | } 41 | 42 | [Test] 43 | public void PropertyInterceptionInfo_Can_Get_Value() 44 | { 45 | var person = new Person(); 46 | var info = new PropertyInterceptionInfo(person, "name"); 47 | 48 | Assert.Multiple(() => 49 | { 50 | string value = string.Empty; 51 | Assert.DoesNotThrow(() => info.SetValue("foobar")); 52 | Assert.DoesNotThrow(() => value = info.GetValue()); 53 | Assert.AreEqual(person.Name,value); 54 | }); 55 | } 56 | 57 | 58 | [Test] 59 | public void PropertyInterceptionInfo_Can_Set_Object_Reference() 60 | { 61 | var person = new Person(); 62 | var info = new PropertyInterceptionInfo(person, "child"); 63 | var child = new Child(); 64 | Assert.Multiple(() => 65 | { 66 | Assert.DoesNotThrow(() => info.SetValue(child)); 67 | Assert.AreEqual(child.Id, person.Child.Id); 68 | Assert.AreSame(child, person.Child); 69 | }); 70 | } 71 | 72 | 73 | [Test] 74 | public void PropertyInterceptionInfo_Set_Throws_When_Type_Missmatches() 75 | { 76 | var person = new Person(); 77 | var info = new PropertyInterceptionInfo(person, "name"); 78 | Assert.Throws(() => info.SetValue(true)); 79 | } 80 | 81 | 82 | [Test] 83 | public void PropertyInterceptionInfo_Throws_When_Instance_Is_Null() 84 | { 85 | Assert.Throws(() => new PropertyInterceptionInfo(null, "name")); 86 | } 87 | 88 | [Test] 89 | public void PropertyInterceptionInfo_Throws_When_PropertyName_Is_Empty() 90 | { 91 | Assert.Throws(() => new PropertyInterceptionInfo(new Person(), "")); 92 | } 93 | 94 | [Test] 95 | public void PropertyInterceptionInfo_Throws_When_Instance_Doesnt_Contain_Property() 96 | { 97 | Assert.Throws(() => new PropertyInterceptionInfo(new Person(), "notintheclass")); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PropertyInterception [![NuGet](https://img.shields.io/nuget/v/PropertyInterception.svg)](https://www.nuget.org/packages/PropertyInterception/) 2 | 3 | Cauldron like [Property-Interception](https://github.com/Capgemini/Cauldron/wiki/Property-interception) using C# Source Generators. 4 | This Package allows you to control the Getter- and Setter-Behaviour of your Properties via an Attribute. 5 | 6 | ## Installation 7 | --- 8 | Via Nuget: Install-Package PropertyInterception 9 |
10 | Or download a [Release](https://github.com/LLukas22/PropertyInterception/releases). 11 | 12 | ## How it works 13 | --- 14 | Your code: 15 | ```C# 16 | internal class InterceptionAttribute : Attribute, IPropertyGetterInterceptor, IPropertySetterInterceptor 17 | { 18 | public bool OnException(PropertyInterceptionInfo propertyInterceptionInfo, Exception exception) 19 | { 20 | //Do Something on Error 21 | return false; 22 | } 23 | 24 | public void OnExit(PropertyInterceptionInfo propertyInterceptionInfo) 25 | { 26 | //Do Something on Exit 27 | } 28 | 29 | public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object currentValue) 30 | { 31 | //Do Something on Get 32 | } 33 | 34 | public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue) 35 | { 36 | //Do Something on Set 37 | return true; 38 | } 39 | } 40 | ``` 41 | ```C# 42 | public partial class Person 43 | { 44 | [Interception] 45 | private string name; 46 | } 47 | ``` 48 | 49 | What gets generated: 50 | 51 | ```C# 52 | public partial class Person 53 | { 54 | [NonSerialized] 55 | private NugetTest.InterceptionAttribute name_propertyInterceptionAttribute; 56 | [NonSerialized] 57 | private PropertyInterceptionInfo name_propertyInterceptionInfo; 58 | 59 | 60 | public string Name 61 | { 62 | get 63 | { 64 | 65 | if(this.name_propertyInterceptionInfo == null) 66 | { 67 | this.name_propertyInterceptionInfo = new PropertyInterceptionInfo(this,"name"); 68 | } 69 | 70 | if(this.name_propertyInterceptionAttribute == null) 71 | { 72 | this.name_propertyInterceptionAttribute = new NugetTest.InterceptionAttribute(); 73 | } 74 | 75 | try 76 | { 77 | name_propertyInterceptionAttribute.OnGet(this.name_propertyInterceptionInfo, this.name); 78 | return this.name; 79 | } 80 | catch (Exception e) 81 | { 82 | if(name_propertyInterceptionAttribute.OnException(this.name_propertyInterceptionInfo,e)) 83 | throw; 84 | else 85 | return this.name; 86 | } 87 | finally 88 | { 89 | name_propertyInterceptionAttribute.OnExit(this.name_propertyInterceptionInfo); 90 | } 91 | 92 | } 93 | 94 | set 95 | { 96 | 97 | if(this.name_propertyInterceptionInfo == null) 98 | { 99 | this.name_propertyInterceptionInfo = new PropertyInterceptionInfo(this,"name"); 100 | } 101 | 102 | if(this.name_propertyInterceptionAttribute == null) 103 | { 104 | this.name_propertyInterceptionAttribute = new NugetTest.InterceptionAttribute(); 105 | } 106 | 107 | try 108 | { 109 | if(name_propertyInterceptionAttribute.OnSet(this.name_propertyInterceptionInfo, this.name, value)) 110 | { 111 | this.name = value; 112 | } 113 | } 114 | catch (Exception e) 115 | { 116 | if(name_propertyInterceptionAttribute.OnException(this.name_propertyInterceptionInfo,e)) 117 | throw; 118 | } 119 | finally 120 | { 121 | name_propertyInterceptionAttribute.OnExit(this.name_propertyInterceptionInfo); 122 | } 123 | 124 | } 125 | } 126 | 127 | } 128 | ``` 129 | 130 | See PropertyInterception.Tests for more examples. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /PropertyInterception/PropertyInterception/PropertyInterceptionGenerator.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.Text; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace PropertyInterception 8 | { 9 | 10 | [Generator] 11 | public class PropertyInterceptionGenerator : ISourceGenerator 12 | { 13 | private static readonly DiagnosticDescriptor TopLevelError = new DiagnosticDescriptor(id: "PIGEN001", 14 | title: "Must be Top-Level", 15 | messageFormat: "'{0}' must be on the Top-Level of the Namespace", 16 | category: "PIGenerator", 17 | DiagnosticSeverity.Error, 18 | isEnabledByDefault: true); 19 | 20 | 21 | 22 | private static readonly DiagnosticDescriptor NameError = new DiagnosticDescriptor(id: "PIGEN002", 23 | title: "Invalide Property Name", 24 | messageFormat: "Can't generate Propertyname for Field '{0}'", 25 | category: "PIGenerator", 26 | DiagnosticSeverity.Error, 27 | isEnabledByDefault: true); 28 | public void Initialize(GeneratorInitializationContext context) 29 | { 30 | // Register a syntax receiver that will be created for each generation pass 31 | context.RegisterForSyntaxNotifications(() => new PropertyInterceptionSyntaxReceiver()); 32 | } 33 | 34 | public void Execute(GeneratorExecutionContext context) 35 | { 36 | // retrieve the populated receiver 37 | if (!(context.SyntaxContextReceiver is PropertyInterceptionSyntaxReceiver receiver)) 38 | return; 39 | 40 | //Check if we need to generate Something 41 | if (receiver.Fields.Count < 1) 42 | return; 43 | 44 | // group the fields by class, and generate the source 45 | foreach (IGrouping group in receiver.Fields.GroupBy(f => f.FieldSymbol.ContainingType)) 46 | { 47 | string classSource = ProcessClass(group.Key, group.ToList(), context); 48 | context.AddSource($"{group.Key.Name}_PropertyInterception.cs", SourceText.From(classSource, Encoding.UTF8)); 49 | } 50 | } 51 | 52 | private string ProcessClass(INamedTypeSymbol classSymbol, List fields, GeneratorExecutionContext context) 53 | { 54 | if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default)) 55 | { 56 | context.ReportDiagnostic(Diagnostic.Create(TopLevelError, Location.None, classSymbol.ContainingSymbol)); 57 | return null; //TODO: issue a diagnostic that it must be top level 58 | } 59 | 60 | string namespaceName = classSymbol.ContainingNamespace.ToDisplayString(); 61 | 62 | // begin building the generated source 63 | StringBuilder source = new StringBuilder($@" 64 | using System; 65 | using PropertyInterception; 66 | 67 | namespace {namespaceName} 68 | {{ 69 | public partial class {classSymbol.Name} 70 | {{ 71 | "); 72 | 73 | // create properties for each field 74 | foreach (FieldContainer fieldContainer in fields) 75 | { 76 | ProcessField(source, fieldContainer, context); 77 | } 78 | 79 | source.Append("\t}\n}"); 80 | return source.ToString(); 81 | } 82 | 83 | private void ProcessField(StringBuilder source, FieldContainer fieldContainer, GeneratorExecutionContext context) 84 | { 85 | // get the name and type of the field 86 | string fieldName = fieldContainer.FieldSymbol.Name; 87 | ITypeSymbol fieldType = fieldContainer.FieldSymbol.Type; 88 | 89 | string propertyName = chooseName(fieldName); 90 | if (propertyName.Length == 0 || propertyName == fieldName) 91 | { 92 | context.ReportDiagnostic(Diagnostic.Create(NameError, Location.None, $"{fieldContainer.FieldSymbol.ContainingType}.{fieldName}")); 93 | return; 94 | } 95 | 96 | string attributeName = $"{fieldName}_propertyInterceptionAttribute"; 97 | string propertyInterceptionInfo = $"{fieldName}_propertyInterceptionInfo"; 98 | source.AppendLine($"\t\t[NonSerialized]\n\t\tprivate {fieldContainer.AttributeData.AttributeClass} {attributeName};"); 99 | source.AppendLine($"\t\t[NonSerialized]\n\t\tprivate {nameof(PropertyInterceptionInfo)} {propertyInterceptionInfo};"); 100 | source.AppendLine($@" 101 | 102 | public {fieldType} {propertyName} 103 | {{ 104 | get 105 | {{ 106 | {GenerateGetter(fieldContainer, attributeName, propertyInterceptionInfo)} 107 | }} 108 | 109 | set 110 | {{ 111 | {GenerateSetter(fieldContainer, attributeName, propertyInterceptionInfo)} 112 | }} 113 | }} 114 | "); 115 | 116 | } 117 | 118 | private string GenerateSetter(FieldContainer fieldContainer, string attribute_name, string propertyInterceptionInfo) 119 | { 120 | if (!fieldContainer.ShouldGenerateSetter) 121 | return $"this.{fieldContainer.FieldSymbol.Name} = value;"; 122 | return $@" 123 | {EnsurePropertyInterceptionInfo(fieldContainer, propertyInterceptionInfo)} 124 | 125 | {EnsureInterceptionAttribute(fieldContainer, attribute_name)} 126 | 127 | try 128 | {{ 129 | if({attribute_name}.OnSet(this.{propertyInterceptionInfo}, this.{fieldContainer.FieldSymbol.Name}, value)) 130 | {{ 131 | this.{fieldContainer.FieldSymbol.Name} = value; 132 | }} 133 | }} 134 | catch (Exception e) 135 | {{ 136 | if({attribute_name}.OnException(this.{propertyInterceptionInfo},e)) 137 | throw; 138 | }} 139 | finally 140 | {{ 141 | {attribute_name}.OnExit(this.{propertyInterceptionInfo}); 142 | }} 143 | "; 144 | } 145 | 146 | private string GenerateGetter(FieldContainer fieldContainer, string attribute_name, string propertyInterceptionInfo) 147 | { 148 | if (!fieldContainer.ShouldGenerateGetter) 149 | return $"return this.{fieldContainer.FieldSymbol.Name};"; 150 | 151 | return $@" 152 | {EnsurePropertyInterceptionInfo(fieldContainer, propertyInterceptionInfo)} 153 | 154 | {EnsureInterceptionAttribute(fieldContainer, attribute_name)} 155 | 156 | try 157 | {{ 158 | {attribute_name}.OnGet(this.{propertyInterceptionInfo}, this.{fieldContainer.FieldSymbol.Name}); 159 | return this.{fieldContainer.FieldSymbol.Name}; 160 | }} 161 | catch (Exception e) 162 | {{ 163 | if({attribute_name}.OnException(this.{propertyInterceptionInfo},e)) 164 | throw; 165 | else 166 | return this.{fieldContainer.FieldSymbol.Name}; 167 | }} 168 | finally 169 | {{ 170 | {attribute_name}.OnExit(this.{propertyInterceptionInfo}); 171 | }} 172 | "; 173 | } 174 | 175 | private object EnsureInterceptionAttribute(FieldContainer fieldContainer, string attribute_name) 176 | { 177 | return $@"if(this.{attribute_name} == null) 178 | {{ 179 | this.{attribute_name} = new {fieldContainer.AttributeData.AttributeClass}(); 180 | }}"; 181 | } 182 | 183 | private string EnsurePropertyInterceptionInfo(FieldContainer fieldContainer, string propertyInterceptionInfo) 184 | { 185 | return $@"if(this.{propertyInterceptionInfo} == null) 186 | {{ 187 | this.{propertyInterceptionInfo} = new PropertyInterceptionInfo(this,""{fieldContainer.FieldSymbol.Name}""); 188 | }}"; 189 | } 190 | string chooseName(string fieldName) 191 | { 192 | fieldName = fieldName.TrimStart('_'); 193 | if (fieldName.Length == 0) 194 | return string.Empty; 195 | 196 | if (fieldName.Length == 1) 197 | return fieldName.ToUpper(); 198 | 199 | return fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1); 200 | } 201 | } 202 | } 203 | --------------------------------------------------------------------------------