public GetTypeReference ( |
||
type | ||
return |
public static bool Implements(this TypeDefinition typeDefinition, System.Type type) { if (type.IsInterface == false) { throw new InvalidOperationException("The <type> argument (" + type.Name + ") must be an Interface type."); } var referenceFinder = new ReferenceFinder(typeDefinition.Module); var baseTypeDefinition = referenceFinder.GetTypeReference(type); return typeDefinition.Implements(baseTypeDefinition); }
public static bool Implements(this TypeDefinition typeDefinition, System.Type type) { if (type.IsInterface == false) { throw new InvalidOperationException("The <type> argument (" + type.Name + ") must be an Interface type."); } var referenceFinder = new ReferenceFinder(typeDefinition.Module); var baseTypeDefinition = referenceFinder.GetTypeReference(type); return(typeDefinition.Implements(baseTypeDefinition)); }
public void Decorate(MethodDefinition method, CustomAttribute attribute) { method.Body.InitLocals = true; var getMethodFromHandleRef = referenceFinder.GetMethodReference(typeof(MethodBase), md => md.Name == "GetMethodFromHandle" && md.Parameters.Count == 2); var getCustomAttributesRef = referenceFinder.GetMethodReference(typeof(MemberInfo), md => md.Name == "GetCustomAttributes" && md.Parameters.Count == 2); var getTypeFromHandleRef = referenceFinder.GetMethodReference(typeof(Type), md => md.Name == "GetTypeFromHandle"); var methodBaseTypeRef = referenceFinder.GetTypeReference(typeof(MethodBase)); var exceptionTypeRef = referenceFinder.GetTypeReference(typeof(Exception)); var methodVariableDefinition = AddVariableDefinition(method, "__fody$method", methodBaseTypeRef); var attributeVariableDefinition = AddVariableDefinition(method, "__fody$attribute", attribute.AttributeType); var exceptionVariableDefinition = AddVariableDefinition(method, "__fody$exception", exceptionTypeRef); VariableDefinition retvalVariableDefinition = null; if (method.ReturnType.FullName != "System.Void") { retvalVariableDefinition = AddVariableDefinition(method, "__fody$retval", method.ReturnType); } var onEntryMethodRef = referenceFinder.GetMethodReference(attribute.AttributeType, md => md.Name == "OnEntry"); var onExitMethodRef = referenceFinder.GetMethodReference(attribute.AttributeType, md => md.Name == "OnExit"); var onExceptionMethodRef = referenceFinder.GetMethodReference(attribute.AttributeType, md => md.Name == "OnException"); var processor = method.Body.GetILProcessor(); var methodBodyFirstInstruction = method.Body.Instructions.First(); if (method.IsConstructor) { methodBodyFirstInstruction = method.Body.Instructions.First(i => i.OpCode == OpCodes.Call).Next; } var getAttributeInstanceInstructions = GetAttributeInstanceInstructions(processor, method, attribute, attributeVariableDefinition, methodVariableDefinition, getCustomAttributesRef, getTypeFromHandleRef, getMethodFromHandleRef); var callOnEntryInstructions = GetCallOnEntryInstructions(processor, attributeVariableDefinition, methodVariableDefinition, onEntryMethodRef); var saveRetvalInstructions = GetSaveRetvalInstructions(processor, retvalVariableDefinition); var callOnExitInstructions = GetCallOnExitInstructions(processor, attributeVariableDefinition, methodVariableDefinition, onExitMethodRef); var methodBodyReturnInstructions = GetMethodBodyReturnInstructions(processor, retvalVariableDefinition); var methodBodyReturnInstruction = methodBodyReturnInstructions.First(); var tryCatchLeaveInstructions = GetTryCatchLeaveInstructions(processor, methodBodyReturnInstruction); var catchHandlerInstructions = GetCatchHandlerInstructions(processor, attributeVariableDefinition, exceptionVariableDefinition, methodVariableDefinition, onExceptionMethodRef); ReplaceRetInstructions(processor, saveRetvalInstructions.Concat(callOnExitInstructions).First()); processor.InsertBefore(methodBodyFirstInstruction, getAttributeInstanceInstructions); processor.InsertBefore(methodBodyFirstInstruction, callOnEntryInstructions); processor.InsertAfter(method.Body.Instructions.Last(), methodBodyReturnInstructions); processor.InsertBefore(methodBodyReturnInstruction, saveRetvalInstructions); processor.InsertBefore(methodBodyReturnInstruction, callOnExitInstructions); processor.InsertBefore(methodBodyReturnInstruction, tryCatchLeaveInstructions); processor.InsertBefore(methodBodyReturnInstruction, catchHandlerInstructions); method.Body.ExceptionHandlers.Add(new ExceptionHandler(ExceptionHandlerType.Catch) { CatchType = exceptionTypeRef, TryStart = methodBodyFirstInstruction, TryEnd = tryCatchLeaveInstructions.Last().Next, HandlerStart = catchHandlerInstructions.First(), HandlerEnd = catchHandlerInstructions.Last().Next }); }