HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

MIST - auto-implemented, attribute-driven .NET notification mechanism

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mistautoimplementednetmechanismattributedrivennotification

Problem

I have a component that I've used myself for quite some time, the short story is that it automagically implements a property change notification mechanism (very useful for implementing INotifyPropertyChanged for example, but can be useful in other ways as well).

There are other solutions available which use a similar technique (post-build IL "Weaving") but I feel that I have a few novel features and via a NuGet package I have made it's use as simple as I think it could possibly be.

The truth is I use this component all the time, I've done so on a number of successful projects and I feel like it really helped me, so I would like to make it available. If .NET/WPF devs out there have a free minute (it literally only takes a minute to get it working) I'd like them to take a look and let me know what they think.

Here's the nuts and bolts (source) and here's the NuGet package (the NuGet package is definitely the way to go).

This is how it works:

The NuGet package now covers .NET framework versions 4.0-4.6+.

  • Create a WPF project in visual studio, Target any framework 4.0 - 4.6.2 (if there's any favorable response I'll set up other frameworks in the NuGet package, what I've done should mostly work for all of them)



  • In the package manager type the NuGet command: install-package Mathtone.MIST. Visual Studio will prompt you to reload the project.



  • This should set everything up, adding a reference to the Mathtone.MIST assembly (which only contains the attribute classes you will use to decorate your code), copying the Mathtone.MIST.Builder and Mono.Cecil (this is the framework that does the actual weaving of IL operations, a fine piece of work) assemblies to the NuGet package "tools" folder. Additionally, it executes a nonthreatening script that modifies your project file and adds the post build task.



Here is an example:

```
using Mathtone.MIST;

namespace Lightly.Misted {

//THe "Notifier" attribute indicates that notification should be implemented for thi

Solution

That's looks great, I didn't know that Cecil can be integrated in MSBUILD.
To add to another reviewers:

You are using OpCodes.Call, consider using OpCodes.Callvirt, so it will work, if [NotifyTarget] will be virtual/abstract.

In fact C# compiler emits callvirt for each instance call, ass it eforces null-check, but you can emit it only if method is actually virtual.

Additionaly, when looking for [NotifyTarget], search in base type, as potential user can create base class, which will have event itself, and NotifyTarget, and just use notifications in derived classes.

Context

StackExchange Code Review Q#129509, answer score: 4

Revisions (0)

No revisions yet.