-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathProgram.cs
120 lines (97 loc) · 3.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace PostSharp.Samples.DependencyResolution.Contextual
{
public interface ILogger
{
void Log(string message);
}
public static class AspectServiceLocator
{
private static CompositionContainer container;
private static HashSet<object> rules = new HashSet<object>();
public static void Initialize(ComposablePartCatalog catalog)
{
container = new CompositionContainer(catalog);
}
public static Lazy<T> GetService<T>(Type aspectType, MemberInfo targetElement) where T : class
{
return new Lazy<T>(() => GetServiceImpl<T>(aspectType, targetElement));
}
private static T GetServiceImpl<T>(Type aspectType, MemberInfo targetElement) where T : class
{
// The rule implementation is naive but this is for testing purpose only.
foreach (object rule in rules)
{
DependencyRule<T> typedRule = rule as DependencyRule<T>;
if (typedRule == null) continue;
T service = typedRule.Rule(aspectType, targetElement);
if (service != null) return service;
}
if (container == null)
throw new InvalidOperationException();
// Fallback to the container, which should be the default and production behavior.
return container.GetExport<T>().Value;
}
public static IDisposable AddRule<T>(Func<Type, MemberInfo, T> rule)
{
DependencyRule<T> dependencyRule = new DependencyRule<T>(rule);
rules.Add(dependencyRule);
return dependencyRule;
}
private class DependencyRule<T> : IDisposable
{
public DependencyRule(Func<Type, MemberInfo, T> rule)
{
this.Rule = rule;
}
public Func<Type, MemberInfo, T> Rule { get; private set; }
public void Dispose()
{
rules.Remove(this);
}
}
}
[PSerializable]
public class LogAspect : OnMethodBoundaryAspect
{
private Lazy<ILogger> logger;
public override void RuntimeInitialize(MethodBase method)
{
logger = AspectServiceLocator.GetService<ILogger>(this.GetType(), method);
}
public override void OnEntry(MethodExecutionArgs args)
{
logger.Value.Log("OnEntry");
}
}
internal class Program
{
private static void Main(string[] args)
{
AspectServiceLocator.Initialize(new TypeCatalog(typeof(ConsoleLogger)));
LoggedMethod();
Console.ReadLine();
}
[LogAspect]
public static void LoggedMethod()
{
Console.WriteLine("Hello, world.");
}
}
[Export(typeof(ILogger))]
internal class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
}