https://github.com/llukas22/propertyinterception
Cauldron like Property-Iterception using C# Source Generators
https://github.com/llukas22/propertyinterception
source-generators
Last synced: 12 months ago
JSON representation
Cauldron like Property-Iterception using C# Source Generators
- Host: GitHub
- URL: https://github.com/llukas22/propertyinterception
- Owner: LLukas22
- License: mit
- Created: 2021-10-23T11:23:46.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-02-14T15:40:39.000Z (about 4 years ago)
- Last Synced: 2025-02-16T19:48:40.449Z (about 1 year ago)
- Topics: source-generators
- Language: C#
- Homepage:
- Size: 19.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PropertyInterception [](https://www.nuget.org/packages/PropertyInterception/)
Cauldron like [Property-Interception](https://github.com/Capgemini/Cauldron/wiki/Property-interception) using C# Source Generators.
This Package allows you to control the Getter- and Setter-Behaviour of your Properties via an Attribute.
## Installation
---
Via Nuget: Install-Package PropertyInterception
Or download a [Release](https://github.com/LLukas22/PropertyInterception/releases).
## How it works
---
Your code:
```C#
internal class InterceptionAttribute : Attribute, IPropertyGetterInterceptor, IPropertySetterInterceptor
{
public bool OnException(PropertyInterceptionInfo propertyInterceptionInfo, Exception exception)
{
//Do Something on Error
return false;
}
public void OnExit(PropertyInterceptionInfo propertyInterceptionInfo)
{
//Do Something on Exit
}
public void OnGet(PropertyInterceptionInfo propertyInterceptionInfo, object currentValue)
{
//Do Something on Get
}
public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue)
{
//Do Something on Set
return true;
}
}
```
```C#
public partial class Person
{
[Interception]
private string name;
}
```
What gets generated:
```C#
public partial class Person
{
[NonSerialized]
private NugetTest.InterceptionAttribute name_propertyInterceptionAttribute;
[NonSerialized]
private PropertyInterceptionInfo name_propertyInterceptionInfo;
public string Name
{
get
{
if(this.name_propertyInterceptionInfo == null)
{
this.name_propertyInterceptionInfo = new PropertyInterceptionInfo(this,"name");
}
if(this.name_propertyInterceptionAttribute == null)
{
this.name_propertyInterceptionAttribute = new NugetTest.InterceptionAttribute();
}
try
{
name_propertyInterceptionAttribute.OnGet(this.name_propertyInterceptionInfo, this.name);
return this.name;
}
catch (Exception e)
{
if(name_propertyInterceptionAttribute.OnException(this.name_propertyInterceptionInfo,e))
throw;
else
return this.name;
}
finally
{
name_propertyInterceptionAttribute.OnExit(this.name_propertyInterceptionInfo);
}
}
set
{
if(this.name_propertyInterceptionInfo == null)
{
this.name_propertyInterceptionInfo = new PropertyInterceptionInfo(this,"name");
}
if(this.name_propertyInterceptionAttribute == null)
{
this.name_propertyInterceptionAttribute = new NugetTest.InterceptionAttribute();
}
try
{
if(name_propertyInterceptionAttribute.OnSet(this.name_propertyInterceptionInfo, this.name, value))
{
this.name = value;
}
}
catch (Exception e)
{
if(name_propertyInterceptionAttribute.OnException(this.name_propertyInterceptionInfo,e))
throw;
}
finally
{
name_propertyInterceptionAttribute.OnExit(this.name_propertyInterceptionInfo);
}
}
}
}
```
See PropertyInterception.Tests for more examples.