Xamarin and Aspect Orientated Programming
source link: https://www.banditoth.hu/2021/05/25/xamarin-and-aspect-orientated-programming/?utm_campaign=xamarin-and-aspect-orientated-programming
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Xamarin and Aspect Orientated Programming
I have started implementing my very first AOP like library, which is now available for testing in NuGet.org as a pre-release version.
This package is a part of my Xamarin.Forms Toolkit, and it is a good time to say thank you for using all of the three packages more than 500 times.
When I had published them, I thought the only user will be me.
In the first release of the package (1.0.0-pre-01), you can decorate your methods with attributes, in order to listen for Entering the method, and Exiting the method.
This can be handy for example, when you need to log your method invocations (Firebase analytics, Basic logging) or when you need to measure the elapsed time of the method run.
[ConsoleYaay]
public
void
BoringMethod()
{
Console.WriteLine(DateTime.Now);
}
For example, the following code above will result the output below:
yaaay on enter!
2021. 05. 25. 16:56:01
yaaaay on exit
This can be achieved implementing IMethodDecorator class like this:
[System.AttributeUsage(System.AttributeTargets.Method)]
public
class
ConsoleYaay : Attribute, IMethodDecorator
{
public
ConsoleYaay()
{
}
public
void
OnEnter()
{
Console.WriteLine(
"yaaay on enter!"
);
}
public
void
OnExit()
{
Console.WriteLine(
"yaaaay on exit"
);
}
}
Try it in your project
Let’s jump to https://github.com/banditoth/Forms.RecurrenceToolkit/ to see the code, or https://www.nuget.org/packages/banditoth.Forms.RecurrenceToolkit.AOP/ to download it as a package
Future improvements
This package can be improved in a lot of aspects, so stay tuned for more details. Or just simply make a pull request with your ideas
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK