Skip to content

Commit c47f044

Browse files
author
xstos
committed
add postsharp example
postsharp allows interception of method calls, setting properties and much more for reducing boilerplate
1 parent 9673973 commit c47f044

6 files changed

+128
-0
lines changed

CSharpExamples.csproj

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="packages\PostSharp.6.0.23\build\PostSharp.props" Condition="Exists('packages\PostSharp.6.0.23\build\PostSharp.props')" />
34
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
45
<PropertyGroup>
56
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -11,6 +12,8 @@
1112
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
1213
<FileAlignment>512</FileAlignment>
1314
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
<NuGetPackageImportStamp>
16+
</NuGetPackageImportStamp>
1417
</PropertyGroup>
1518
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1619
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -33,11 +36,15 @@
3336
</PropertyGroup>
3437
<ItemGroup>
3538
<Compile Include="Example2_ImmutableNamedTuples.cs" />
39+
<Compile Include="Example3_PostSharp.cs" />
3640
<Compile Include="Main.cs" />
3741
<Compile Include="Example1_ObservableExpression.cs" />
3842
</ItemGroup>
3943
<ItemGroup>
4044
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="PostSharp, Version=6.0.23.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
46+
<HintPath>packages\PostSharp.Redist.6.0.23\lib\net45\PostSharp.dll</HintPath>
47+
</Reference>
4148
<Reference Include="System" />
4249
<Reference Include="System.Reactive.Core, Version=3.0.1000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
4350
<HintPath>packages\System.Reactive.Core.3.1.1\lib\net45\System.Reactive.Core.dll</HintPath>
@@ -61,4 +68,12 @@
6168
<None Include="packages.config" />
6269
</ItemGroup>
6370
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
71+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
72+
<PropertyGroup>
73+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
74+
</PropertyGroup>
75+
<Error Condition="!Exists('packages\PostSharp.6.0.23\build\PostSharp.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\PostSharp.6.0.23\build\PostSharp.props'))" />
76+
<Error Condition="!Exists('packages\PostSharp.6.0.23\build\PostSharp.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\PostSharp.6.0.23\build\PostSharp.targets'))" />
77+
</Target>
78+
<Import Project="packages\PostSharp.6.0.23\build\PostSharp.targets" Condition="Exists('packages\PostSharp.6.0.23\build\PostSharp.targets')" />
6479
</Project>

Example1_ObservableExpression.cs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static partial class Examples
7676
{
7777
public static void Example1()
7878
{
79+
Console.WriteLine("Example1");
7980
ObservableExpression<int> subExpr = 0;
8081

8182
var myExpr = subExpr + 1000 + subExpr;

Example2_ImmutableNamedTuples.cs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static partial class Examples
1919
{
2020
public static void Example2()
2121
{
22+
Console.WriteLine("Example2");
2223
var jimmy = Person.New(name: "Jimmy", age: 5);
2324

2425
//do something with jimmy

Example3_PostSharp.cs

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using PostSharp.Aspects;
5+
using PostSharp.Reflection;
6+
7+
namespace CSharpExamples
8+
{
9+
[Serializable]
10+
public class DeltaAspect : LocationInterceptionAspect
11+
{
12+
//public static StreamWriter Writer = new StreamWriter(@"c:\deltaaspect.txt");
13+
14+
private static Action<string> log = (text) =>
15+
{
16+
// Writer.WriteLine(text);
17+
// Writer.Flush();
18+
Console.WriteLine(text);
19+
};
20+
public override void OnSetValue(LocationInterceptionArgs args)
21+
{
22+
object oldValue = args.GetCurrentValue();
23+
object newValue = args.Value;
24+
25+
if (args.Location.LocationKind == LocationKind.Property)
26+
{
27+
if (args.Location.PropertyInfo.PropertyType.IsValueType &&
28+
!args.Location.Name.EndsWith("__BackingField"))
29+
{
30+
if (!oldValue.Equals(newValue))
31+
{
32+
var classInstance = args.Instance; //this is the class who's property changed
33+
var text = $"{args.LocationFullName} changed {oldValue} -> {newValue}";
34+
log(text);
35+
}
36+
}
37+
}
38+
39+
base.OnSetValue(args);
40+
}
41+
}
42+
43+
[Serializable]
44+
public sealed class TraceAspect : OnMethodBoundaryAspect
45+
{
46+
//public static StreamWriter Writer = new StreamWriter(@"c:\methodaspect.txt");
47+
48+
static Action<string> log = (text) =>
49+
{
50+
// Writer.WriteLine(text);
51+
// Writer.Flush();
52+
Console.WriteLine(text);
53+
};
54+
55+
[NonSerialized] private string enteringMessage;
56+
[NonSerialized] private string exitingMessage;
57+
58+
public TraceAspect() { }
59+
60+
public override void RuntimeInitialize(MethodBase method)
61+
{
62+
var methodName = method.DeclaringType.FullName + "." + method.Name;
63+
enteringMessage = "Method Enter: "+ methodName;
64+
exitingMessage = "Method Exit: " + methodName;
65+
}
66+
67+
public override void OnEntry(MethodExecutionArgs args) => log(enteringMessage);
68+
69+
public override void OnExit(MethodExecutionArgs args) => log(exitingMessage);
70+
}
71+
[DeltaAspect]
72+
[TraceAspect]
73+
class ClassIWantToTrack
74+
{
75+
public int Number { get; set; }
76+
public void Dog() { }
77+
public void Cat() { }
78+
}
79+
80+
public static partial class Examples
81+
{
82+
public static void Example3()
83+
{
84+
Console.WriteLine("Example3");
85+
ClassIWantToTrack foo = new ClassIWantToTrack();
86+
foo.Number = 1;
87+
foo.Number = 2;
88+
foo.Dog();
89+
foo.Cat();
90+
91+
//prints:
92+
93+
//Method Enter: CSharpExamples.ClassIWantToTrack..ctor
94+
//Method Exit: CSharpExamples.ClassIWantToTrack..ctor
95+
//Method Enter: CSharpExamples.ClassIWantToTrack.set_Number
96+
//CSharpExamples.ClassIWantToTrack.Number changed 0 -> 1
97+
//Method Exit: CSharpExamples.ClassIWantToTrack.set_Number
98+
//Method Enter: CSharpExamples.ClassIWantToTrack.set_Number
99+
//CSharpExamples.ClassIWantToTrack.Number changed 1 -> 2
100+
//Method Exit: CSharpExamples.ClassIWantToTrack.set_Number
101+
//Method Enter: CSharpExamples.ClassIWantToTrack.Dog
102+
//Method Exit: CSharpExamples.ClassIWantToTrack.Dog
103+
//Method Enter: CSharpExamples.ClassIWantToTrack.Cat
104+
//Method Exit: CSharpExamples.ClassIWantToTrack.Cat
105+
106+
}
107+
}
108+
}

Main.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static void Main()
1212
{
1313
Examples.Example1();
1414
Examples.Example2();
15+
Examples.Example3();
1516
Console.ReadLine();
1617
}
1718
}

packages.config

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="PostSharp" version="6.0.23" targetFramework="net452" />
4+
<package id="PostSharp.Redist" version="6.0.23" targetFramework="net452" />
35
<package id="System.Reactive" version="3.1.1" targetFramework="net452" />
46
<package id="System.Reactive.Core" version="3.1.1" targetFramework="net452" />
57
<package id="System.Reactive.Interfaces" version="3.1.1" targetFramework="net452" />

0 commit comments

Comments
 (0)