Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created May 2, 2012 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/2576527 to your computer and use it in GitHub Desktop.
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.
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