Created
June 12, 2013 20:49
-
-
Save MatthewSteeples/5768974 to your computer and use it in GitHub Desktop.
Read through properties in a class and find out if they depend on any other properties in the same class
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
using Mono.Cecil; | |
using Mono.Cecil.Cil; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var assembly = AssemblyDefinition.ReadAssembly(Assembly.GetExecutingAssembly().Location); | |
var types = assembly.Modules.Select(b => b.Types).SelectMany(b => b); | |
foreach (var type in types) | |
{ | |
HashSet<MethodDefinition> methods = new HashSet<MethodDefinition>(type.Properties.Select(a => a.GetMethod)); | |
var dict = new Dictionary<PropertyDefinition, List<MethodDefinition>>(); | |
foreach (var property in type.Properties) | |
{ | |
var get = property.GetMethod; | |
if (get.HasBody) | |
{ | |
var any = get.Body.Instructions | |
.Where(a => a.OpCode.Code == Code.Call) | |
.Select(a => a.Operand as MethodDefinition) | |
.Where(a => a != null) | |
.Where(a => a.DeclaringType == get.DeclaringType) | |
.Where(a => methods.Contains(a)) | |
.ToList(); | |
if (any.Any()) | |
{ | |
dict.Add(property, any); | |
} | |
} | |
} | |
} | |
} | |
} | |
class TestClass | |
{ | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public string FullName { get { return string.Join(" ", FirstName, LastName); } } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment