Is it possible to mix aspects that call different parts of a class (ie Methods and Properties)?
Pretty much as per the subject line.
Consider the following scenario where one has aspects to log methods, Properties and eventually (subject to future metalama developments) Events.
I would like to create fabric extension methods that would add all two / three at the same time.
16 Replies
Very much pseudo code here but something along the following lines is what I am aspiring to;
You mean one aspect class that targets both methods and properties?
essentially you would do
class TheAspect : Attribute, IAspect<IMethod>, IAspect<IFieldOrProperty>
No, one fabric that would allow you to add both aspects.
Would
type.MethodsAndAccessors()
work for you, or do you actually need special processing for properties?Yes fabrics can add many aspects, no problem with that
You can do many queries, reuse query parts, etc.
I take it though, or at least my possibly flawed experimentation to date suggests that you can't do that in one single method?
Can you share an example that doesn't work?
I'm trying to do something along the lines of the pseudo code above but a specific example of something that doesn't work is;
.SelectMany(type => type.Methods && type.Properties)
Now I freely admit my Linq knowledge is not as good as it ought to be, especially for this sort of situation.
You could do
.SelectMany(type => type.Methods.Concat<IMember>(type.Properties))
, but I don't think that's going to lead where you want.This may be one of those cases where less turns out to be more in the overall scheme of things.
I would probably do something like this:
(Again, assuming using
MethodsAndAccessors()
is not enough.)@petronderka I hadn't graped the significance of .MethodsAndAccessors sorry, closer to the goal now just need to tidy up the last bit which it doesn't like;
amender.Outbound
.SelectMany(compilation => compilation.AllTypes)
.Where(type => !type.IsStatic)
.SelectMany(type => type.MethodsAndAccessors())
.Where(method => method.Name != "ToString")
.AddAspectIfEligible<LogMethodAttribute>()
.AddAspectIfEligble<LogPropertyAttribute>();
An accessor is an
IMethod
, not an IProperty
, so .AddAspectIfEligible<LogMethodAttribute>()
should be enough.But the Log Method Attribute doesn't log Properties?
Which means that MethodsandAccessors won't work.
If you use it with
MethodsAndAccessors
, then it will log methods and property accessors.Ok thanks, I'll do some experimentation with this a little later on