Skip to content

Instantly share code, notes, and snippets.

@einarwh
einarwh / EventHandlerHookupMethods.cs
Created April 28, 2012 19:14
Code to inject add and remove methods for the event handler.
private MethodDefinition CreateAddPropertyChangedMethod()
{
return CreatePropertyChangedEventHookupMethod(
"add_PropertyChanged",
"Combine");
}
private MethodDefinition CreateRemovePropertyChangedMethod()
{
return CreatePropertyChangedEventHookupMethod(
@einarwh
einarwh / InjectNotificationCalls.cs
Created April 28, 2012 21:12
Code to inject notification calls, optionally refactoring out original setter.
private void InjectNotification(MethodDefinition methodDef,
IEnumerable<string> propNames)
{
if (_notifyMethodDef == null || methodDef == null)
{
return;
}
if (HasMultipleReturnPoints(methodDef))
{
RefactorSetterAndInjectNotification(methodDef, propNames);
@einarwh
einarwh / BirthDate.cs
Created April 28, 2012 21:19
Dependent properties
private DateTime _birthDate;
public DateTime BirthDate
{
get { return _birthDate; }
set { _birthDate = value; }
}
[Viewable]
public int Age
@einarwh
einarwh / BirthDateNotify.cs
Created April 28, 2012 21:22
Notification of dependent property.
private DateTime _birthDate;
public DateTime BirthDate
{
get { return _birthDate; }
set
{
_birthDate = value;
Notify("Age");
}
@einarwh
einarwh / HandlePropertyToNotify.cs
Created April 28, 2012 21:26
Find properties to tamper with, including dependent properties.
private void HandlePropertyToNotify(PropertyDefinition prop)
{
foreach (var affector in FindAffectingProperties(prop, new List<string>()))
{
AddDependency(affector, prop);
}
}
private void AddDependency(PropertyDefinition key,
PropertyDefinition value)
field private class [System]System.ComponentModel.PropertyChangedEventHandler PropertyChanged
event [System]System.ComponentModel.PropertyChangedEventHandler PropertyChanged
{
.addon instance void Silver.Needle.Tests.Data.Dependencies.Complex.PersonViewModel::add_PropertyChanged(class [System]System.ComponentModel.PropertyChangedEventHandler)
.removeon instance void Silver.Needle.Tests.Data.Dependencies.Complex.PersonViewModel::remove_PropertyChanged(class [System]System.ComponentModel.PropertyChangedEventHandler)
}
.method public final hidebysig specialname newslot virtual
instance void add_PropertyChanged (
@einarwh
einarwh / FindNotificationMethod.cs
Created May 2, 2012 13:31
Recursively try to find an existing notification method to fire the PropertyChanged event.
private static MethodDefinition FindNotificationMethod(
TypeDefinition typeDef,
bool includePrivateMethods = true)
{
foreach (var m in typeDef.Methods.Where(m => includePrivateMethods
|| m.Attributes.HasFlag(MethodAttributes.Public)))
{
if (IsProbableNotificationMethod(m))
{
return m;
@einarwh
einarwh / CreateNotificationMethodDefinition.cs
Created May 2, 2012 13:55
Create a notify method to fire the PropertyChanged event.
private MethodDefinition CreateNotificationMethodDefinition()
{
const string MethodName = "NotifyViewableProperty";
var methodDef = new MethodDefinition(MethodName,
MethodAttributes.Private |
MethodAttributes.HideBySig,
this._types.Void);
var paramDef = new ParameterDefinition("propertyName",
ParameterAttributes.None,
_types.String);
@einarwh
einarwh / PersonViewModel.cs
Created May 2, 2012 20:40
Simple view model with annotated viewable property.
public class PersonViewModel
{
[Viewable]
public string Name { get; set; }
}
@einarwh
einarwh / PersonViewModelByHand.cs
Created May 2, 2012 20:57
Implementing INotifyPropertyChanged by hand.
public class PersonViewModel : INotifyPropertyChanged
{
private string _name;
public event PropertyChangedEventHandler PropertyChanged;
public string Name
{
get
{