Monday, 15 April 2013

c# - PostSharp injecting dependency to aspect -



c# - PostSharp injecting dependency to aspect -

is possible inject dependency postsharp aspect? i'd create navigatebackaspect - that:

[pserializable] class navigatebackaspect : onmethodboundaryaspect { private readonly inavigationservice _navigationservice; public navigatebackaspect(inavigationservice navigationservice) { _navigationservice = navigationservice; } public override void onexit(methodexecutionargs args) { base.onexit(args); var viewmodel = args.instance shared.viewmodel.viewmodel; if (viewmodel != null) { viewmodel.dispose(); _navigationservice.goback(); } } }

however i've read constructor/parameter injection not possible because constructor called 1 time after compilation. there workarounds?

this kind of thing not possible because attributes in general , aspects in particular can have constant expressions in constructor.

the constructor called 1 time per aspected target during compilation time. after happens, aspect serialized (along internal info may contain). deserialized during runtime, means constructor not called @ all. there no reason called; attribute constructors can contain constant info isn't liable change. see aspect lifetime here.

however, every aspected target still has own instance of aspect. means can during runtime initialization. have few options:

supply string key parameter constructor, , during runtime appropriate object using key (from shared dictionary). rely on aspected type have property or field containing object, , access through reflection. it's recommended during runtime initialization because reflection can cause performance problems.

c# postsharp

No comments:

Post a Comment