public void Execute(AdviceSpec spec) { foreach (Navigator targetNav in spec.TargetNavigators) { Narrow.TypeDefinition(targetNav, spec).Attributes |= TypeAttributes.Serializable; } }
public void Execute(AdviceSpec spec) { foreach (Navigator targetNav in spec.TargetNavigators) { TypeDefinition typeDef = Narrow.TypeDefinition(targetNav, spec); foreach (Navigator aspectNav in spec.AspectNavigators) { typeDef.Interfaces.Add(Cil.TargetMainModule.Import(Narrow.InterfaceDefinition(aspectNav, spec))); } } }
public void Execute(AdviceSpec spec) { if (spec.AspectNavigators.Count != 1) { throw new AdviceException("One and only one base type can be set to a target type, but got " + spec.AspectNavigators.Count, spec); } TypeDefinition parentTypeDef = Narrow.TypeDefinition((Navigator)spec.AspectNavigators[0], spec); foreach (Navigator targetNav in spec.TargetNavigators) { Narrow.TypeDefinition(targetNav, spec).BaseType = Cil.TargetMainModule.Import(parentTypeDef); } }
public void Execute(AdviceSpec spec) { foreach (Navigator aspectNav in spec.AspectNavigators) { MethodDefinition aspectMetaMethod = Narrow.Method(aspectNav, spec); MethodInfo aspectMethod = Cil.Aspects.GetType(aspectMetaMethod.DeclaringType.FullName). GetMethod(aspectMetaMethod.Name); bool preconditions = aspectMethod.IsStatic && aspectMethod.ReturnType.FullName == typeof(string).FullName && aspectMethod.GetParameters().Length == 1 && aspectMethod.GetParameters()[0].ParameterType.FullName == typeof(TypeDefinition).FullName; if (!preconditions) { throw new AdviceException("Aspect method signature must match : \n" + "public static string YourMethod(Mono.Cecil.TypeDefinition td)", spec); } foreach (Navigator targetNav in spec.TargetNavigators) { TypeDefinition targetType = Narrow.TypeDefinition(targetNav, spec); // Only insert members on classes (not structs because fields MUST be initialized in the ctor, no field initializer) preconditions = !targetType.FullName.StartsWith("<") && !targetType.IsValueType && !targetType.IsInterface && !(targetType.BaseType != null && targetType.BaseType.Name == "Delegate"); if (preconditions) { string code = aspectMethod.Invoke(null, new object[] { targetType }) as string; if (code != null) { string className = DynamicClassName + ++m_ClassIndex; code = new StringBuilder("public class "). Append(className).Append(" { \n"). Append(code).Append("\n}").ToString(); try { // CodeDom CompilerResults results = m_Compiler.CompileAssemblyFromSource(m_CompilerParams, code); TypeDefinition dynamicType = AssemblyFactory.GetAssembly(results.PathToAssembly). MainModule.Types[className]; // Copy every method and field from the dynamic type to the targetTypeDef dynamicType = Cil.TargetMainModule.Inject(dynamicType); foreach (MethodDefinition method in dynamicType.Methods) { targetType.Methods.Add(method.Clone()); } foreach (FieldDefinition field in dynamicType.Fields) { targetType.Fields.Add(field.Clone()); } if (File.Exists(m_Path)) { File.Delete(m_Path); } } catch (Exception e) { Console.WriteLine("error " + e); } } } } // Remove aspect method from target assembly TypeDefinition typeDef = (TypeDefinition)aspectMetaMethod.DeclaringType; typeDef.Methods.Remove(aspectMetaMethod); } }