Click here to Skip to main content
15,893,588 members
Articles / Desktop Programming / WPF

Reflection Studio - Part 1 - Introduction: Architecture and Design

Rate me:
Please Sign up or sign in to vote.
4.83/5 (23 votes)
22 Sep 2010GPL36 min read 60.1K   6.9K   111  
Reflection Studio is a "developer" application for assembly, database, performance, and code generation, written in C# under WPF 4.0.
using System;
using Mono.Cecil;
using Mono.Cecil.Cil;

namespace ReflectionStudio.Core.Helpers
{
	public class CecilHelper
	{
		//----------------------CECIL----------------------
		internal static readonly string CecilModul = "<Module>";

		internal static AssemblyDefinition GetAssembly(string pathFile)
		{
			AssemblyDefinition AsmToProfile = null;

			try
			{
				AsmToProfile = AssemblyFactory.GetAssembly(pathFile);
			}
			catch (Exception exception)
			{
				if (exception.Message != "The image is not a managed assembly")
				{
				}
			}
			return AsmToProfile;
		}

		internal static bool HasCustomAttribute(CustomAttributeCollection cac, string fullname)
		{
			if (cac.Count != 0)
			{
				foreach (CustomAttribute attribute in cac)
				{
					if (attribute.Constructor.DeclaringType.FullName == fullname)
					{
						return true;
					}
				}
			}
			return false;
		}

		public static bool IsSmallMethod(MethodBody body)
		{
			if (body.Instructions.Count == 0)
				return true;
			
			Instruction ins = body.Instructions[0];
			if (ins.OpCode == OpCodes.Nop)
				ins = ins.Next;
			
			if (MatchCILSequence(ins, new OpCode[] { OpCodes.Ret }))
				return true;
			
			if (MatchCILSequence(ins, new OpCode[] { OpCodes.Ldarg_0, OpCodes.Stsfld, OpCodes.Ret }))
				return true;
			
			if (MatchCILSequence(ins, new OpCode[] { OpCodes.Ldsfld, OpCodes.Stloc_0, OpCodes.Br_S, OpCodes.Ldloc_0, OpCodes.Ret }))
				return true;
		
			if (MatchCILSequence(ins, new OpCode[] { OpCodes.Ldarg_0, OpCodes.Ldarg_1, OpCodes.Stfld, OpCodes.Ret }))
				return true;
			
			if (MatchCILSequence(ins, new OpCode[] { OpCodes.Ldarg_0, OpCodes.Ldfld, OpCodes.Stloc_0, OpCodes.Br_S, OpCodes.Ldloc_0, OpCodes.Ret }))
				return true;
			
			if (((((ins.OpCode == OpCodes.Ldstr) || (ins.OpCode == OpCodes.Ldnull)) || ((ins.OpCode == OpCodes.Ldc_I4) || (ins.OpCode == OpCodes.Ldc_I4_0))) || (((ins.OpCode == OpCodes.Ldc_I4_1) || (ins.OpCode == OpCodes.Ldc_I4_2)) || ((ins.OpCode == OpCodes.Ldc_I4_3) || (ins.OpCode == OpCodes.Ldc_I4_4)))) || ((((ins.OpCode == OpCodes.Ldc_I4_5) || (ins.OpCode == OpCodes.Ldc_I4_6)) || ((ins.OpCode == OpCodes.Ldc_I4_7) || (ins.OpCode == OpCodes.Ldc_I4_8))) || (((ins.OpCode == OpCodes.Ldc_I4_M1) || (ins.OpCode == OpCodes.Ldc_I4_S)) || (((ins.OpCode == OpCodes.Ldc_I8) || (ins.OpCode == OpCodes.Ldc_R4)) || (ins.OpCode == OpCodes.Ldc_R8)))))
			{
				if (MatchCILSequence(ins.Next, new OpCode[] { OpCodes.Stloc_0, OpCodes.Br_S, OpCodes.Ldloc_0, OpCodes.Ret }))
					return true;
			}
			
			return false;
		}

		internal static bool MatchCILSequence(Instruction ins, params OpCode[] sequence)
		{
			foreach (OpCode code in sequence)
			{
				if (ins.OpCode != code)
				{
					return false;
				}
				ins = ins.Next;
			}
			return true;
		}

		internal static string GetMethodParamSignature(MethodDefinition methodDef)
		{
			string result= string.Empty;

			foreach (ParameterDefinition def in methodDef.Parameters)
				result += def.ParameterType.Name + ";";

			result += methodDef.ReturnType.ReturnType.Name;

			return result;
		}

		internal static string GetMethodParam(MethodDefinition methodDef)
		{
			string result = string.Empty;

			//foreach (ParameterDefinition def in methodDef.Parameters)
			//    result += def.ParameterType.Name + ", ";

			for (int i = 0; i < methodDef.Parameters.Count; i++)
			{
				ParameterDefinition def = methodDef.Parameters[i];
				result += def.ParameterType.Name;
				if( i < methodDef.Parameters.Count-1 )
					result += ", ";
			}

			return result;
		}


		internal static string MethodDisplayName(MethodDefinition methodDef)
		{
			if (methodDef != null)
			{
				if (methodDef.ReturnType.ReturnType.ToString().Equals("System.Void"))
					//return methodDef.ToString().Substring(methodDef.ToString().IndexOf("::") + 2);
					return string.Format("{0}({1})", methodDef.Name, GetMethodParam(methodDef));
				else
					//return methodDef.ToString().Substring(methodDef.ToString().IndexOf("::") + 2) + " : " + methodDef.ReturnType.ReturnType.Name;
					return string.Format("{0}({1}) : {2}", methodDef.Name, GetMethodParam(methodDef), methodDef.ReturnType.ReturnType.Name);
			}
			else
				return string.Empty;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions