Update 06/3/2013 Aligned with current Mono.Cecil version Update 21/5/2012 Update to Release 1.2.1 (Portable Class Libraries support) Update 21/5/2012 Update to Release 1.2 (WinRT + .NET 4 fixes) Update 6/4/2012 Update to Release 1.1 (VS11 support) Update 7/3/2012 Update to Release 1.0.0.9 Mixed FX references fixed Update 13/10/2011 Update to Release 1.0.0.8 SL5 RC & various bug fixes Update 08/03/2011 Update to Release 1.0.0.5 Nullable types fix Update 04/03/2011 Update to Release 1.0.0.4 Magic in generic classes (Please, reinstall KindOfMagic extension) Update 03/03/2011 Update to Release 1.0.0.3 Silverlight, .NET Framework 3.5, 3.0, 2.0 supported (Please, reinstall KindOfMagic extension) Update 27/01/2011 Update to Release 1.0 (1.0.0.2) (Please, re-enable KindOfMagic in Project menu for your projects) Project home http://kindofmagic.codeplex.com Project Description
C# Edit|Remove csharp[Magic]public string Name { get { return _name; } set { _name = value; } }string _name; [Magic] public string Name { get { return _name; } set { _name = value; } } string _name; Or you may apply Magic attribute to a class, and all public properties (including all derived classes) will be transformed. C# Edit|Remove csharp[Magic]public class MyViewModel: INotifyPropertyChanged{ public string Name { get; set; } public string LastName { get; set; } .....} [Magic] public class MyViewModel: INotifyPropertyChanged { public string Name { get; set; } public string LastName { get; set; } ..... } To disable transformation, apply NoMagic attribute to a class or property:
That's exacly what Kind Of Magic does! No more, no less. 0) Enable your project: after installation of this VS2010 package, you'll find Enable/Disable KindOfMagic menu items in Project menu. 1) Define somewhere in your project MagicAttribute and NoMagicAttribute (optional) class, derived from Attribute. Neither visibility nor namespace are relevant. Here is an example, just two lines of code:
2) Apply Magic attribute to public properties in your ViewModel classes, implementing INotifyPropertyChanged. Your class must have accessible void RaisePropertyChanged(string) method. 3) Apply Magic attribute to ViewModel class if you want all public properties to raise PropertyChanged event. To exclude some of them, apply NoMagic attribute to these properties. Beacon method call Sometimes, when property setter code is relative complex, you may see this warning produced by KindOfMagic: C# Edit|Remove csharppublic object MyProperty { get { return _field; } set { _field = value; if (value != null) DoSomething(); }} public object MyProperty { get { return _field; } set { _field = value; if (value != null) DoSomething(); } } This happens because in IL, produced by C# compiler, it is not more possible to distinguish normal return (with keyword return) from conditional return. In this case, KindOfMagic redirects all returns to injected RaisePropertyChanged section of the setter, so all returns will raise property changed event. This gives you, as a programer, less control over your code. To solve this issue, I came up with the Beacon method idea. C# Edit|Remove csharppublic class MyViewModel : INotifyPropertyChanged{ ... [MethodImpl(MethodImplOptions.NoInlining)] // to preserve method call protected static void Raise() { }} public class MyViewModel : INotifyPropertyChanged { ... [MethodImpl(MethodImplOptions.NoInlining)] // to preserve method call protected static void Raise() { } } In every problematic setter, place a call to this method just before the last curly brace of your setter. Here is an example: C# Edit|Remove csharppublic object MyProperty { get { return _field; } set { _field = value; if (value != null) DoSomething(); Raise(); }} public object MyProperty { get { return _field; } set { _field = value; if (value != null) DoSomething(); Raise(); } } KindOfMagic will automagically replace Raise method call with RaisePropertyChanged section instead. No return statement will be remapped and no warning message will be emitted. Customization XML Edit|Remove xml<MagicTask .... MagicAttribute="NotifyAttribute" NoMagicAttribute="StopNotifyAttribute" RaiseMethod="OnPropertyChanged"/> <MagicTask .... MagicAttribute="NotifyAttribute" NoMagicAttribute="StopNotifyAttribute" RaiseMethod="OnPropertyChanged"/> This will use Notify/StopNotify instead of Magic/NoMagic attributes and OnPropertyChanged instead of RaisePropertyChanged method during transformation.
|