Created
May 2, 2012 13:31
-
-
Save einarwh/2576527 to your computer and use it in GitHub Desktop.
Recursively try to find an existing notification method to fire the PropertyChanged event.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
var baseTypeRef = typeDef.BaseType; | |
if (baseTypeRef.FullName != "System.Object") | |
{ | |
return FindNotificationMethod(baseTypeRef.Resolve(), false); | |
} | |
return null; | |
} | |
private static bool IsProbableNotificationMethod( | |
MethodDefinition methodDef) | |
{ | |
return methodDef.HasBody | |
&& IsProbableNotificationMethodWithBody(methodDef); | |
} | |
private static bool IsProbableNotificationMethodWithBody( | |
MethodDefinition methodDef) | |
{ | |
foreach (var insn in methodDef.Body.Instructions) | |
{ | |
if (insn.OpCode == OpCodes.Callvirt) | |
{ | |
var callee = (MethodReference) insn.Operand; | |
if (callee.Name == "Invoke" | |
&& callee.DeclaringType.Name == "PropertyChangedEventHandler") | |
{ | |
return true; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment