diff --git a/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/LinFu.AOP.MethodInterceptionSample.csproj b/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/LinFu.AOP.MethodInterceptionSample.csproj index 9ba164e..a757a82 100644 --- a/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/LinFu.AOP.MethodInterceptionSample.csproj +++ b/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/LinFu.AOP.MethodInterceptionSample.csproj @@ -1,109 +1,95 @@ - - - - Debug - AnyCPU - 9.0.21022 - 2.0 - {47E8A072-1E89-4FD8-AEB0-85C105F7CC52} - Exe - Properties - LinFu.AOP.MethodInterceptionSample - LinFu.AOP.MethodInterceptionSample - v4.0 - 512 - - - 3.5 - - - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - false - true - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - False - ..\lib\LinFu.Core.dll - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - - - - - {40E585A8-2D84-45C9-8EE8-9600152D8D96} - SampleLibrary - - - - - - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - true - - - False - Windows Installer 3.1 - true - - - + + + + Debug + AnyCPU + 9.0.21022 + 2.0 + {47E8A072-1E89-4FD8-AEB0-85C105F7CC52} + Exe + Properties + LinFu.AOP.MethodInterceptionSample + LinFu.AOP.MethodInterceptionSample + v4.0 + 512 + + + 3.5 + + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + ..\lib\LinFu.AOP.Interfaces.dll + + + + + + + + + + {40E585A8-2D84-45C9-8EE8-9600152D8D96} + SampleLibrary + + + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + + False + Windows Installer 3.1 + true + + + + --> \ No newline at end of file diff --git a/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/Program.cs b/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/Program.cs index 5d21445..8efb883 100644 --- a/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/Program.cs +++ b/examples/LinFu.AOP.Samples/LinFu.AOP.MethodInterceptionSample/Program.cs @@ -1,49 +1,43 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading; -using LinFu.AOP.Cecil.Extensions; -using LinFu.AOP.Interfaces; -using SampleLibrary; - -namespace LinFu.AOP.MethodInterceptionSample -{ - public class SampleInterceptor : IInterceptor - { - public object Intercept(IInvocationInfo info) - { - var methodName = info.TargetMethod.Name; - Console.WriteLine("method '{0}' called", methodName); - - // Replace the input parameter with 42 - var result = info.TargetMethod.Invoke(info.Target, new object[]{42}); - - return result; - } - } - - - // NOTE: Remember to check the SampleLibrary.csproj file for an example on how to use LinFu.AOP.Tasks to transparently - // postweave your DLLs! - class Program - { - static void Main(string[] args) - { - var employee = new Employee(); - - // All LinFu.AOP-modified objects implement the IModifiableType interface - var modifiableType = employee as IModifiableType; - - // Plug in our custom implementation - if (modifiableType != null) - modifiableType.MethodBodyReplacementProvider = new SimpleMethodReplacementProvider(new SampleInterceptor()); - - // The employee object will call the interceptor instead of the actual method implementation - employee.Pay(12345); - - return; - } - } -} +using System; +using LinFu.AOP.Interfaces; +using SampleLibrary; + +namespace LinFu.AOP.MethodInterceptionSample +{ + public class SampleInterceptor : IInterceptor + { + public object Intercept(IInvocationInfo info) + { + var methodName = info.TargetMethod.Name; + Console.WriteLine("method '{0}' called", methodName); + + // Replace the input parameter with 42 + var result = info.TargetMethod.Invoke(info.Target, new object[]{42}); + + return result; + } + } + + + // NOTE: Remember to check the SampleLibrary.csproj file for an example on how to use LinFu.AOP.Tasks to transparently + // postweave your DLLs! + class Program + { + static void Main(string[] args) + { + var employee = new Employee(); + + // All LinFu.AOP-modified objects implement the IModifiableType interface + var modifiableType = employee as IModifiableType; + + // Plug in our custom implementation + if (modifiableType != null) + modifiableType.MethodBodyReplacementProvider = new SimpleMethodReplacementProvider(new SampleInterceptor()); + + // The employee object will call the interceptor instead of the actual method implementation + employee.Pay(12345); + + return; + } + } +} diff --git a/examples/LinFu.AOP.Samples/SampleLibrary/BankAccount.cs b/examples/LinFu.AOP.Samples/SampleLibrary/BankAccount.cs index 3414766..d747a16 100644 --- a/examples/LinFu.AOP.Samples/SampleLibrary/BankAccount.cs +++ b/examples/LinFu.AOP.Samples/SampleLibrary/BankAccount.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; - +using System; + namespace SampleLibrary { public class BankAccount diff --git a/examples/LinFu.AOP.Samples/SampleLibrary/Employee.cs b/examples/LinFu.AOP.Samples/SampleLibrary/Employee.cs index c509949..7f8661a 100644 --- a/examples/LinFu.AOP.Samples/SampleLibrary/Employee.cs +++ b/examples/LinFu.AOP.Samples/SampleLibrary/Employee.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - +using System; + namespace SampleLibrary { public class Employee diff --git a/examples/LinFu.AOP.Samples/SampleLibrary/SampleLibrary.csproj b/examples/LinFu.AOP.Samples/SampleLibrary/SampleLibrary.csproj index cd2e48a..297a184 100644 --- a/examples/LinFu.AOP.Samples/SampleLibrary/SampleLibrary.csproj +++ b/examples/LinFu.AOP.Samples/SampleLibrary/SampleLibrary.csproj @@ -1,108 +1,93 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {40E585A8-2D84-45C9-8EE8-9600152D8D96} - Library - Properties - SampleLibrary - SampleLibrary - v4.0 - 512 - - - 3.5 - - - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - false - true - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - False - ..\lib\LinFu.Core.dll - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - - - - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - true - - - False - Windows Installer 3.1 - true - - - - - - $(MSBuildProjectDirectory)\$(OutputPath)\..\..\..\lib\LinFu.Core.dll - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {40E585A8-2D84-45C9-8EE8-9600152D8D96} + Library + Properties + SampleLibrary + SampleLibrary + v4.0 + 512 + + + 3.5 + + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + true + + + False + Windows Installer 3.1 + true + + + + + + $(MSBuildProjectDirectory)\$(OutputPath)\..\..\..\lib\LinFu.AOP.Tasks.dll + + + + + \ No newline at end of file diff --git a/lib/Mono.Cecil.Mdb.dll b/lib/Mono.Cecil.Mdb.dll new file mode 100644 index 0000000..b67da6e Binary files /dev/null and b/lib/Mono.Cecil.Mdb.dll differ diff --git a/lib/Mono.Cecil.Mdb.pdb b/lib/Mono.Cecil.Mdb.pdb new file mode 100644 index 0000000..0e5d0f0 Binary files /dev/null and b/lib/Mono.Cecil.Mdb.pdb differ diff --git a/lib/Mono.Cecil.Pdb.dll b/lib/Mono.Cecil.Pdb.dll index fc5b842..97d3854 100644 Binary files a/lib/Mono.Cecil.Pdb.dll and b/lib/Mono.Cecil.Pdb.dll differ diff --git a/lib/Mono.Cecil.Pdb.pdb b/lib/Mono.Cecil.Pdb.pdb new file mode 100644 index 0000000..5aed49a Binary files /dev/null and b/lib/Mono.Cecil.Pdb.pdb differ diff --git a/lib/Mono.Cecil.Rocks.dll b/lib/Mono.Cecil.Rocks.dll new file mode 100644 index 0000000..41694e5 Binary files /dev/null and b/lib/Mono.Cecil.Rocks.dll differ diff --git a/lib/Mono.Cecil.Rocks.pdb b/lib/Mono.Cecil.Rocks.pdb new file mode 100644 index 0000000..77d0b21 Binary files /dev/null and b/lib/Mono.Cecil.Rocks.pdb differ diff --git a/lib/Mono.Cecil.dll b/lib/Mono.Cecil.dll index 38caf01..58f1a37 100644 Binary files a/lib/Mono.Cecil.dll and b/lib/Mono.Cecil.dll differ diff --git a/lib/Mono.Cecil.pdb b/lib/Mono.Cecil.pdb new file mode 100644 index 0000000..a3c6151 Binary files /dev/null and b/lib/Mono.Cecil.pdb differ diff --git a/src/CommonAssemblyInfo.cs b/src/CommonAssemblyInfo.cs index 19f016c..c522466 100644 --- a/src/CommonAssemblyInfo.cs +++ b/src/CommonAssemblyInfo.cs @@ -11,6 +11,8 @@ [assembly: AssemblyTitle("LinFu")] [assembly: AssemblyProduct("LinFu")] [assembly: AllowPartiallyTrustedCallers] +[assembly: SecurityRules(SecurityRuleSet.Level1)] + // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from diff --git a/src/LinFu.AOP.Interfaces/LinFu.AOP.Interfaces.csproj b/src/LinFu.AOP.Interfaces/LinFu.AOP.Interfaces.csproj index 673c4de..30a9d91 100644 --- a/src/LinFu.AOP.Interfaces/LinFu.AOP.Interfaces.csproj +++ b/src/LinFu.AOP.Interfaces/LinFu.AOP.Interfaces.csproj @@ -1,116 +1,117 @@ - - - - Debug - AnyCPU - 9.0.21022 - 2.0 - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - Library - Properties - LinFu.AOP.Interfaces - LinFu.AOP.Interfaces - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - bin\Debug\LinFu.AOP.Interfaces.XML - - - pdbonly - true - ..\..\build\Release\ - TRACE - prompt - 4 - ..\..\build\Release\LinFu.AOP.Interfaces.XML - - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - Properties\CommonAssemblyInfo.cs - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {22EEB00F-F471-486C-A6AD-60F088821C78} - LinFu.Reflection - - - + + + + Debug + AnyCPU + 9.0.21022 + 2.0 + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + Library + Properties + LinFu.AOP.Interfaces + LinFu.AOP.Interfaces + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + bin\Debug\LinFu.AOP.Interfaces.XML + + + pdbonly + true + ..\..\build\Release\ + TRACE + prompt + 4 + ..\..\build\Release\LinFu.AOP.Interfaces.XML + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {22EEB00F-F471-486C-A6AD-60F088821C78} + LinFu.Reflection + + + + --> \ No newline at end of file diff --git a/src/LinFu.AOP.Tasks/LinFu.AOP.Tasks.csproj b/src/LinFu.AOP.Tasks/LinFu.AOP.Tasks.csproj index da21a0a..fd8f8e0 100644 --- a/src/LinFu.AOP.Tasks/LinFu.AOP.Tasks.csproj +++ b/src/LinFu.AOP.Tasks/LinFu.AOP.Tasks.csproj @@ -1,96 +1,97 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {70AB1ECE-216D-43C2-A4DE-52BE01B56D6A} - Library - Properties - LinFu.AOP.Tasks - LinFu.AOP.Tasks - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - ..\..\build\Debug\LinFu.AOP.Tasks.XML - - - pdbonly - true - ..\..\build\Release\ - TRACE - prompt - 4 - - - - - - 3.5 - - - False - ..\..\lib\Mono.Cecil.dll - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - CommonAssemblyInfo.cs - - - - - - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - LinFu.AOP.Interfaces - - - {613B6547-DCBB-4505-82B8-B4179BFC95CE} - LinFu.AOP.Cecil - - - {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} - LinFu.Reflection.Emit - - - {22EEB00F-F471-486C-A6AD-60F088821C78} - LinFu.Reflection - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {70AB1ECE-216D-43C2-A4DE-52BE01B56D6A} + Library + Properties + LinFu.AOP.Tasks + LinFu.AOP.Tasks + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + ..\..\build\Debug\LinFu.AOP.Tasks.XML + + + pdbonly + true + ..\..\build\Release\ + TRACE + prompt + 4 + + + + + + 3.5 + + + False + ..\..\lib\Mono.Cecil.dll + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + CommonAssemblyInfo.cs + + + + + + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + LinFu.AOP.Interfaces + + + {613B6547-DCBB-4505-82B8-B4179BFC95CE} + LinFu.AOP.Cecil + + + {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} + LinFu.Reflection.Emit + + + {22EEB00F-F471-486C-A6AD-60F088821C78} + LinFu.Reflection + + + + + + + --> \ No newline at end of file diff --git a/src/LinFu.AOP.Tasks/PostWeaveTask.cs b/src/LinFu.AOP.Tasks/PostWeaveTask.cs index 0dc1f24..1408a6f 100644 --- a/src/LinFu.AOP.Tasks/PostWeaveTask.cs +++ b/src/LinFu.AOP.Tasks/PostWeaveTask.cs @@ -75,7 +75,7 @@ public override bool Execute() { Log.LogMessage("PostWeaving Assembly '{0}' -> '{1}'", TargetFile, outputFile); - AssemblyDefinition assembly = AssemblyFactory.GetAssembly(TargetFile); + AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(TargetFile); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(TargetFile); string pdbFileName = string.Format("{0}.pdb", filenameWithoutExtension); @@ -84,7 +84,7 @@ public override bool Execute() ModuleDefinition module = assembly.MainModule; if (pdbExists) - module.LoadSymbols(); + module.ReadSymbols(); if (InterceptAllMethodCalls) assembly.InterceptAllMethodCalls(); @@ -103,7 +103,7 @@ public override bool Execute() // Update the PDB info if it exists if (pdbExists) - module.SaveSymbols(); + module.Write(pdbFileName); assembly.Save(outputFile); } diff --git a/src/LinFu.AOP/AssemblyDefinitionExtensions.cs b/src/LinFu.AOP/AssemblyDefinitionExtensions.cs index 66eb988..c5b1a8d 100644 --- a/src/LinFu.AOP/AssemblyDefinitionExtensions.cs +++ b/src/LinFu.AOP/AssemblyDefinitionExtensions.cs @@ -19,7 +19,6 @@ public static void RemoveStrongName(this AssemblyDefinition sourceAssembly) nameDef.PublicKey = null; nameDef.PublicKeyToken = null; nameDef.HashAlgorithm = AssemblyHashAlgorithm.None; - nameDef.Flags = ~AssemblyFlags.PublicKey; nameDef.HasPublicKey = false; } } diff --git a/src/LinFu.AOP/BaseMethodRewriter.cs b/src/LinFu.AOP/BaseMethodRewriter.cs index 32ca3ba..d02cc43 100644 --- a/src/LinFu.AOP/BaseMethodRewriter.cs +++ b/src/LinFu.AOP/BaseMethodRewriter.cs @@ -15,19 +15,19 @@ public abstract class BaseMethodRewriter : IMethodRewriter #region IMethodRewriter Members /// - /// Rewrites a target method using the given CilWorker. + /// Rewrites a target method using the given ILProcessor. /// /// The target method. - /// The CilWorker that will be used to rewrite the target method. + /// The ILProcessor that will be used to rewrite the target method. /// The original instructions from the target method body. - public void Rewrite(MethodDefinition method, CilWorker IL, IEnumerable oldInstructions) + public void Rewrite(MethodDefinition method, ILProcessor IL, IEnumerable oldInstructions) { if (!ShouldRewrite(method)) return; TypeDefinition declaringType = method.DeclaringType; - MethodBody body = IL.GetBody(); + MethodBody body = IL.Body; body.InitLocals = true; ModuleDefinition module = declaringType.Module; @@ -90,9 +90,9 @@ protected virtual bool ShouldRewrite(MethodDefinition targetMethod) /// Rewrites the instructions in the target method body. /// /// The target method. - /// The instance that represents the method body. + /// The instance that represents the method body. /// The IL instructions of the original method body. - protected abstract void RewriteMethodBody(MethodDefinition method, CilWorker IL, + protected abstract void RewriteMethodBody(MethodDefinition method, ILProcessor IL, IEnumerable oldInstructions); } } \ No newline at end of file diff --git a/src/LinFu.AOP/BaseReflectionVisitor.cs b/src/LinFu.AOP/BaseReflectionVisitor.cs index 43f6044..cdb1427 100644 --- a/src/LinFu.AOP/BaseReflectionVisitor.cs +++ b/src/LinFu.AOP/BaseReflectionVisitor.cs @@ -1,347 +1,348 @@ -using Mono.Cecil; - -#pragma warning disable 1591 - -namespace LinFu.AOP.Cecil -{ - public abstract class BaseReflectionVisitor : IReflectionVisitor, IReflectionStructureVisitor - { - #region IReflectionStructureVisitor Members - - public virtual void TerminateAssemblyDefinition(AssemblyDefinition asm) - { - } - - public virtual void VisitAssemblyDefinition(AssemblyDefinition asm) - { - VisitCustomAttributeCollection(asm.CustomAttributes); - VisitModuleDefinitionCollection(asm.Modules); - VisitSecurityDeclarationCollection(asm.SecurityDeclarations); - - TerminateAssemblyDefinition(asm); - } - - public virtual void VisitAssemblyLinkedResource(AssemblyLinkedResource res) - { - } - - public virtual void VisitAssemblyNameDefinition(AssemblyNameDefinition name) - { - } - - public virtual void VisitAssemblyNameReference(AssemblyNameReference name) - { - } - - public virtual void VisitEmbeddedResource(EmbeddedResource res) - { - } - - public virtual void VisitLinkedResource(LinkedResource res) - { - } - - public virtual void VisitModuleReference(ModuleReference module) - { - } - - #endregion - - #region Collection Visitors - - #region IReflectionStructureVisitor Members - - public virtual void VisitAssemblyNameReferenceCollection(AssemblyNameReferenceCollection names) - { - foreach (AssemblyNameReference nameRef in names) - { - VisitAssemblyNameReference(nameRef); - } - } - - public virtual void VisitModuleDefinitionCollection(ModuleDefinitionCollection modules) - { - foreach (ModuleDefinition module in modules) - { - VisitModuleDefinition(module); - } - } - - public virtual void VisitModuleReferenceCollection(ModuleReferenceCollection modules) - { - foreach (ModuleReference module in modules) - { - VisitModuleReference(module); - } - } - - public virtual void VisitResourceCollection(ResourceCollection resources) - { - } - - #endregion - - #region IReflectionVisitor Members - - public virtual void VisitTypeDefinition(TypeDefinition type) - { - // Visit the interfaces - VisitInterfaceCollection(type.Interfaces); - - // Visit the constructors - VisitConstructorCollection(type.Constructors); - - // Visit the fields - VisitFieldDefinitionCollection(type.Fields); - - // Visit the methods - VisitMethodDefinitionCollection(type.Methods); - - // Visit the properties - VisitPropertyDefinitionCollection(type.Properties); - - // Visit the events - VisitEventDefinitionCollection(type.Events); - - // Visit the nested types - VisitNestedTypeCollection(type.NestedTypes); - - // Visit the security declaration - VisitSecurityDeclarationCollection(type.SecurityDeclarations); - } - - public virtual void VisitConstructorCollection(ConstructorCollection ctors) - { - foreach (MethodDefinition ctor in ctors) - { - VisitConstructor(ctor); - } - } - - public virtual void VisitCustomAttributeCollection(CustomAttributeCollection customAttrs) - { - foreach (CustomAttribute ca in customAttrs) - { - VisitCustomAttribute(ca); - } - } - - public virtual void VisitEventDefinitionCollection(EventDefinitionCollection events) - { - foreach (EventDefinition eventDef in events) - { - VisitEventDefinition(eventDef); - } - } - - public virtual void VisitExternTypeCollection(ExternTypeCollection externs) - { - foreach (TypeReference type in externs) - { - VisitExternType(type); - } - } - - public virtual void VisitFieldDefinitionCollection(FieldDefinitionCollection fields) - { - foreach (FieldDefinition field in fields) - { - VisitFieldDefinition(field); - } - } - - public virtual void VisitGenericParameterCollection(GenericParameterCollection genparams) - { - foreach (GenericParameter param in genparams) - { - VisitGenericParameter(param); - } - } - - public virtual void VisitInterfaceCollection(InterfaceCollection interfaces) - { - foreach (TypeReference interfaceType in interfaces) - { - VisitInterface(interfaceType); - } - } - - public virtual void VisitMemberReferenceCollection(MemberReferenceCollection members) - { - foreach (MemberReference memberRef in members) - { - VisitMemberReference(memberRef); - } - } - - public virtual void VisitMethodDefinition(MethodDefinition method) - { - // Visit the parameters - VisitParameterDefinitionCollection(method.Parameters); - - // Visit the generic parameters - VisitGenericParameterCollection(method.GenericParameters); - - // Visit the method overrides - VisitOverrideCollection(method.Overrides); - - // Visit the security declarations - VisitSecurityDeclarationCollection(method.SecurityDeclarations); - } - - public virtual void VisitMethodDefinitionCollection(MethodDefinitionCollection methods) - { - foreach (MethodDefinition method in methods) - { - VisitMethodDefinition(method); - } - } - - public virtual void VisitOverrideCollection(OverrideCollection overrides) - { - foreach (MethodReference method in overrides) - { - VisitOverride(method); - } - } - - public virtual void VisitParameterDefinitionCollection(ParameterDefinitionCollection parameters) - { - foreach (ParameterDefinition param in parameters) - { - VisitParameterDefinition(param); - } - } - - public virtual void VisitPropertyDefinitionCollection(PropertyDefinitionCollection properties) - { - foreach (PropertyDefinition property in properties) - { - VisitPropertyDefinition(property); - } - } - - public virtual void VisitSecurityDeclarationCollection(SecurityDeclarationCollection secDecls) - { - foreach (SecurityDeclaration securityDeclaration in secDecls) - { - VisitSecurityDeclaration(securityDeclaration); - } - } - - public virtual void VisitTypeDefinitionCollection(TypeDefinitionCollection types) - { - foreach (TypeDefinition typeDef in types) - { - VisitTypeDefinition(typeDef); - } - } - - public virtual void VisitTypeReferenceCollection(TypeReferenceCollection refs) - { - foreach (TypeReference type in refs) - { - VisitTypeReference(type); - } - } - - #endregion - - #endregion - - #region IReflectionVisitor Members - - public virtual void TerminateModuleDefinition(ModuleDefinition module) - { - VisitTypeDefinitionCollection(module.Types); - } - - public virtual void VisitConstructor(MethodDefinition ctor) - { - } - - public virtual void VisitCustomAttribute(CustomAttribute customAttr) - { - } - - public virtual void VisitEventDefinition(EventDefinition evt) - { - } - - public virtual void VisitExternType(TypeReference externType) - { - } - - public virtual void VisitFieldDefinition(FieldDefinition field) - { - } - - public virtual void VisitGenericParameter(GenericParameter genparam) - { - } - - public virtual void VisitInterface(TypeReference interf) - { - } - - public virtual void VisitMarshalSpec(MarshalSpec marshalSpec) - { - } - - public virtual void VisitMemberReference(MemberReference member) - { - } - - public virtual void VisitModuleDefinition(ModuleDefinition module) - { - VisitCustomAttributeCollection(module.CustomAttributes); - VisitResourceCollection(module.Resources); - VisitAssemblyNameReferenceCollection(module.AssemblyReferences); - VisitModuleReferenceCollection(module.ModuleReferences); - VisitMemberReferenceCollection(module.MemberReferences); - VisitTypeReferenceCollection(module.TypeReferences); - VisitExternTypeCollection(module.ExternTypes); - TerminateModuleDefinition(module); - } - - public virtual void VisitNestedTypeCollection(NestedTypeCollection nestedTypes) - { - foreach (TypeDefinition nestedType in nestedTypes) - { - VisitNestedType(nestedType); - } - } - - public virtual void VisitOverride(MethodReference ov) - { - } - - public virtual void VisitSecurityDeclaration(SecurityDeclaration secDecl) - { - } - - public virtual void VisitPInvokeInfo(PInvokeInfo pinvk) - { - } - - public virtual void VisitParameterDefinition(ParameterDefinition parameter) - { - } - - public virtual void VisitPropertyDefinition(PropertyDefinition property) - { - } - - public virtual void VisitNestedType(TypeDefinition nestedType) - { - } - - public virtual void VisitTypeReference(TypeReference type) - { - } - - #endregion - } -} - +using System.Collections.Generic; +using Mono.Cecil; + +#pragma warning disable 1591 + +namespace LinFu.AOP.Cecil +{ + public abstract class BaseReflectionVisitor + { + #region IReflectionStructureVisitor Members + + public virtual void TerminateAssemblyDefinition(AssemblyDefinition asm) + { + } + + public virtual void VisitAssemblyDefinition(AssemblyDefinition asm) + { + VisitCustomAttributeCollection(asm.CustomAttributes); + VisitModuleDefinitionCollection(asm.Modules); + VisitSecurityDeclarationCollection(asm.SecurityDeclarations); + + TerminateAssemblyDefinition(asm); + } + + public virtual void VisitAssemblyLinkedResource(AssemblyLinkedResource res) + { + } + + public virtual void VisitAssemblyNameDefinition(AssemblyNameDefinition name) + { + } + + public virtual void VisitAssemblyNameReference(AssemblyNameReference name) + { + } + + public virtual void VisitEmbeddedResource(EmbeddedResource res) + { + } + + public virtual void VisitLinkedResource(LinkedResource res) + { + } + + public virtual void VisitModuleReference(ModuleReference module) + { + } + + #endregion + + #region Collection Visitors + + #region IReflectionStructureVisitor Members + + public virtual void VisitAssemblyNameReferenceCollection(IEnumerable names) + { + foreach (AssemblyNameReference nameRef in names) + { + VisitAssemblyNameReference(nameRef); + } + } + + public virtual void VisitModuleDefinitionCollection(IEnumerable modules) + { + foreach (ModuleDefinition module in modules) + { + VisitModuleDefinition(module); + } + } + + public virtual void VisitModuleReferenceCollection(IEnumerable modules) + { + foreach (ModuleReference module in modules) + { + VisitModuleReference(module); + } + } + + public virtual void VisitResourceCollection(IEnumerable resources) + { + } + + #endregion + + #region IReflectionVisitor Members + + public virtual void VisitTypeDefinition(TypeDefinition type) + { + // Visit the interfaces + VisitInterfaceCollection(type.Interfaces); + + // Visit the constructors + //VisitConstructorCollection(type.Constructors); + + // Visit the fields + VisitFieldDefinitionCollection(type.Fields); + + // Visit the methods + VisitMethodDefinitionCollection(type.Methods); + + // Visit the properties + VisitPropertyDefinitionCollection(type.Properties); + + // Visit the events + VisitEventDefinitionCollection(type.Events); + + // Visit the nested types + VisitNestedTypeCollection(type.NestedTypes); + + // Visit the security declaration + VisitSecurityDeclarationCollection(type.SecurityDeclarations); + } + + public virtual void VisitConstructorCollection(IEnumerable ctors) + { + foreach (MethodDefinition ctor in ctors) + { + VisitConstructor(ctor); + } + } + + public virtual void VisitCustomAttributeCollection(IEnumerable customAttrs) + { + foreach (CustomAttribute ca in customAttrs) + { + VisitCustomAttribute(ca); + } + } + + public virtual void VisitEventDefinitionCollection(IEnumerable events) + { + foreach (EventDefinition eventDef in events) + { + VisitEventDefinition(eventDef); + } + } + + public virtual void VisitExternTypeCollection(IEnumerable externs) + { + foreach (TypeReference type in externs) + { + VisitExternType(type); + } + } + + public virtual void VisitFieldDefinitionCollection(IEnumerable fields) + { + foreach (FieldDefinition field in fields) + { + VisitFieldDefinition(field); + } + } + + public virtual void VisitGenericParameterCollection(IEnumerable genparams) + { + foreach (GenericParameter param in genparams) + { + VisitGenericParameter(param); + } + } + + public virtual void VisitInterfaceCollection(IEnumerable interfaces) + { + foreach (TypeReference interfaceType in interfaces) + { + VisitInterface(interfaceType); + } + } + + public virtual void VisitMemberReferenceCollection(IEnumerable members) + { + foreach (MemberReference memberRef in members) + { + VisitMemberReference(memberRef); + } + } + + public virtual void VisitMethodDefinition(MethodDefinition method) + { + // Visit the parameters + VisitParameterDefinitionCollection(method.Parameters); + + // Visit the generic parameters + VisitGenericParameterCollection(method.GenericParameters); + + // Visit the method overrides + VisitOverrideCollection(method.Overrides); + + // Visit the security declarations + VisitSecurityDeclarationCollection(method.SecurityDeclarations); + } + + public virtual void VisitMethodDefinitionCollection(IEnumerable methods) + { + foreach (MethodDefinition method in methods) + { + VisitMethodDefinition(method); + } + } + + public virtual void VisitOverrideCollection(IEnumerable overrides) + { + foreach (MethodReference method in overrides) + { + VisitOverride(method); + } + } + + public virtual void VisitParameterDefinitionCollection(IEnumerable parameters) + { + foreach (ParameterDefinition param in parameters) + { + VisitParameterDefinition(param); + } + } + + public virtual void VisitPropertyDefinitionCollection(IEnumerable properties) + { + foreach (PropertyDefinition property in properties) + { + VisitPropertyDefinition(property); + } + } + + public virtual void VisitSecurityDeclarationCollection(IEnumerable secDecls) + { + foreach (SecurityDeclaration securityDeclaration in secDecls) + { + VisitSecurityDeclaration(securityDeclaration); + } + } + + public virtual void VisitTypeDefinitionCollection(IEnumerable types) + { + foreach (TypeDefinition typeDef in types) + { + VisitTypeDefinition(typeDef); + } + } + + public virtual void VisitTypeReferenceCollection(IEnumerable refs) + { + foreach (TypeReference type in refs) + { + VisitTypeReference(type); + } + } + + #endregion + + #endregion + + #region IReflectionVisitor Members + + public virtual void TerminateModuleDefinition(ModuleDefinition module) + { + VisitTypeDefinitionCollection(module.Types); + } + + public virtual void VisitConstructor(MethodDefinition ctor) + { + } + + public virtual void VisitCustomAttribute(CustomAttribute customAttr) + { + } + + public virtual void VisitEventDefinition(EventDefinition evt) + { + } + + public virtual void VisitExternType(TypeReference externType) + { + } + + public virtual void VisitFieldDefinition(FieldDefinition field) + { + } + + public virtual void VisitGenericParameter(GenericParameter genparam) + { + } + + public virtual void VisitInterface(TypeReference interf) + { + } + + public virtual void VisitMarshalSpec(MarshalInfo marshalInfo) + { + } + + public virtual void VisitMemberReference(MemberReference member) + { + } + + public virtual void VisitModuleDefinition(ModuleDefinition module) + { + VisitCustomAttributeCollection(module.CustomAttributes); + VisitResourceCollection(module.Resources); + VisitAssemblyNameReferenceCollection(module.AssemblyReferences); + VisitModuleReferenceCollection(module.ModuleReferences); + VisitMemberReferenceCollection(module.GetMemberReferences()); + VisitTypeReferenceCollection(module.GetTypeReferences()); + //VisitExternTypeCollection(module.ExternTypes); + TerminateModuleDefinition(module); + } + + public virtual void VisitNestedTypeCollection(IEnumerable nestedTypes) + { + foreach (TypeDefinition nestedType in nestedTypes) + { + VisitNestedType(nestedType); + } + } + + public virtual void VisitOverride(MethodReference ov) + { + } + + public virtual void VisitSecurityDeclaration(SecurityDeclaration secDecl) + { + } + + public virtual void VisitPInvokeInfo(PInvokeInfo pinvk) + { + } + + public virtual void VisitParameterDefinition(ParameterDefinition parameter) + { + } + + public virtual void VisitPropertyDefinition(PropertyDefinition property) + { + } + + public virtual void VisitNestedType(TypeDefinition nestedType) + { + } + + public virtual void VisitTypeReference(TypeReference type) + { + } + + #endregion + } +} + #pragma warning restore 1591 \ No newline at end of file diff --git a/src/LinFu.AOP/CatchAllThrownExceptions.cs b/src/LinFu.AOP/CatchAllThrownExceptions.cs index fc2abf6..ed90e0a 100644 --- a/src/LinFu.AOP/CatchAllThrownExceptions.cs +++ b/src/LinFu.AOP/CatchAllThrownExceptions.cs @@ -42,7 +42,7 @@ public override void AddLocals(MethodDefinition hostMethod) _exceptionHandler = hostMethod.AddLocal(); _exceptionInfo = hostMethod.AddLocal(); - TypeReference returnType = hostMethod.ReturnType.ReturnType; + TypeReference returnType = hostMethod.ReturnType; if (returnType != _voidType) _returnValue = hostMethod.AddLocal(); } @@ -51,9 +51,9 @@ public override void AddLocals(MethodDefinition hostMethod) /// Rewrites the instructions in the target method body to support dynamic exception handling. /// /// The target method. - /// The instance that represents the method body. + /// The instance that represents the method body. /// The IL instructions of the original method body. - protected override void RewriteMethodBody(MethodDefinition targetMethod, CilWorker IL, + protected override void RewriteMethodBody(MethodDefinition targetMethod, ILProcessor IL, IEnumerable oldInstructions) { Instruction endOfOriginalInstructionBlock = IL.Create(OpCodes.Nop); @@ -61,10 +61,10 @@ protected override void RewriteMethodBody(MethodDefinition targetMethod, CilWork Instruction endLabel = IL.Create(OpCodes.Nop); - Instruction tryStart = IL.Emit(OpCodes.Nop); - Instruction tryEnd = IL.Emit(OpCodes.Nop); - Instruction catchStart = IL.Emit(OpCodes.Nop); - Instruction catchEnd = IL.Emit(OpCodes.Nop); + Instruction tryStart = IL.Create(OpCodes.Nop); + Instruction tryEnd = IL.Create(OpCodes.Nop); + Instruction catchStart = IL.Create(OpCodes.Nop); + Instruction catchEnd = IL.Create(OpCodes.Nop); ModuleDefinition module = IL.GetModule(); var handler = new ExceptionHandler(ExceptionHandlerType.Catch); @@ -81,7 +81,7 @@ protected override void RewriteMethodBody(MethodDefinition targetMethod, CilWork var emitter = new InvocationInfoEmitter(true); - TypeReference returnType = targetMethod.ReturnType.ReturnType; + TypeReference returnType = targetMethod.ReturnType; // try { IL.Append(tryStart); @@ -174,7 +174,7 @@ protected override void RewriteMethodBody(MethodDefinition targetMethod, CilWork /// The instance that will emit the current method context. private void SaveExceptionInfo(MethodDefinition targetMethod, IEmitInvocationInfo emitter) { - CilWorker IL = targetMethod.GetILGenerator(); + ILProcessor IL = targetMethod.GetILGenerator(); ModuleDefinition module = IL.GetModule(); emitter.Emit(targetMethod, targetMethod, _invocationInfo); @@ -187,7 +187,7 @@ private void SaveExceptionInfo(MethodDefinition targetMethod, IEmitInvocationInf IL.Emit(OpCodes.Newobj, exceptionInfoConstructor); IL.Emit(OpCodes.Stloc, _exceptionInfo); - TypeReference returnType = targetMethod.ReturnType.ReturnType; + TypeReference returnType = targetMethod.ReturnType; if (returnType == _voidType || _returnValue == null) return; diff --git a/src/LinFu.AOP/Emitters/AddMethodReplacementImplementation.cs b/src/LinFu.AOP/Emitters/AddMethodReplacementImplementation.cs index 1f87c1f..941cbe9 100644 --- a/src/LinFu.AOP/Emitters/AddMethodReplacementImplementation.cs +++ b/src/LinFu.AOP/Emitters/AddMethodReplacementImplementation.cs @@ -37,11 +37,11 @@ public AddMethodReplacementImplementation(IMethodBodyRewriterParameters paramete /// /// Adds method body interception to the target method. /// - /// The pointing to the target method body. - public void Emit(CilWorker IL) + /// The pointing to the target method body. + public void Emit(ILProcessor IL) { MethodDefinition method = IL.GetMethod(); - TypeReference returnType = method.ReturnType.ReturnType; + TypeReference returnType = method.ReturnType; Instruction endLabel = IL.Create(OpCodes.Nop); Instruction executeOriginalInstructions = IL.Create(OpCodes.Nop); diff --git a/src/LinFu.AOP/Emitters/AddOriginalInstructions.cs b/src/LinFu.AOP/Emitters/AddOriginalInstructions.cs index cb99c8d..9fffe61 100644 --- a/src/LinFu.AOP/Emitters/AddOriginalInstructions.cs +++ b/src/LinFu.AOP/Emitters/AddOriginalInstructions.cs @@ -29,8 +29,8 @@ public AddOriginalInstructions(IEnumerable oldInstructions, Instruc /// /// Adds the original instructions to a given method body. /// - /// The responsible for the target method body. - public void Emit(CilWorker IL) + /// The responsible for the target method body. + public void Emit(ILProcessor IL) { var originalInstructions = new List(_oldInstructions); Instruction lastInstruction = originalInstructions.LastOrDefault(); diff --git a/src/LinFu.AOP/Emitters/EmitAfterInvoke.cs b/src/LinFu.AOP/Emitters/EmitAfterInvoke.cs index 0e56c5d..2c88be1 100644 --- a/src/LinFu.AOP/Emitters/EmitAfterInvoke.cs +++ b/src/LinFu.AOP/Emitters/EmitAfterInvoke.cs @@ -38,8 +38,8 @@ public EmitAfterInvoke(VariableDefinition surroundingImplementation, /// /// Emits the call to the instance. /// - /// The that points to the current method body. - public void Emit(CilWorker IL) + /// The that points to the current method body. + public void Emit(ILProcessor IL) { ModuleDefinition module = IL.GetModule(); @@ -52,7 +52,7 @@ public void Emit(CilWorker IL) #endregion - private static void Emit(CilWorker IL, ModuleDefinition module, + private static void Emit(ILProcessor IL, ModuleDefinition module, VariableDefinition surroundingImplementation, VariableDefinition invocationInfo, VariableDefinition returnValue) diff --git a/src/LinFu.AOP/Emitters/EmitBeforeInvoke.cs b/src/LinFu.AOP/Emitters/EmitBeforeInvoke.cs index c424bbd..efb15f0 100644 --- a/src/LinFu.AOP/Emitters/EmitBeforeInvoke.cs +++ b/src/LinFu.AOP/Emitters/EmitBeforeInvoke.cs @@ -40,8 +40,8 @@ public EmitBeforeInvoke(VariableDefinition invocationInfo, /// /// Emits the call to the instance. /// - /// The that points to the current method body. - public void Emit(CilWorker IL) + /// The that points to the current method body. + public void Emit(ILProcessor IL) { MethodDefinition targetMethod = IL.GetMethod(); TypeDefinition declaringType = targetMethod.DeclaringType; diff --git a/src/LinFu.AOP/Emitters/GetAroundInvokeProvider.cs b/src/LinFu.AOP/Emitters/GetAroundInvokeProvider.cs index c3c466a..aed63aa 100644 --- a/src/LinFu.AOP/Emitters/GetAroundInvokeProvider.cs +++ b/src/LinFu.AOP/Emitters/GetAroundInvokeProvider.cs @@ -30,8 +30,8 @@ public GetAroundInvokeProvider(VariableDefinition aroundInvokeProvider, string p /// /// Emits the call to obtain the instance. /// - /// The pointing to the target method body. - public void Emit(CilWorker IL) + /// The pointing to the target method body. + public void Emit(ILProcessor IL) { MethodDefinition method = IL.GetMethod(); ModuleDefinition module = IL.GetModule(); diff --git a/src/LinFu.AOP/Emitters/GetClassMethodReplacementProvider.cs b/src/LinFu.AOP/Emitters/GetClassMethodReplacementProvider.cs index 530d94f..6a364da 100644 --- a/src/LinFu.AOP/Emitters/GetClassMethodReplacementProvider.cs +++ b/src/LinFu.AOP/Emitters/GetClassMethodReplacementProvider.cs @@ -49,8 +49,8 @@ public GetClassMethodReplacementProvider(VariableDefinition invocationInfo, /// /// Emits the instructions that obtain a class-level instance. /// - /// The instance that points to the instructions in the method body. - public void Emit(CilWorker IL) + /// The instance that points to the instructions in the method body. + public void Emit(ILProcessor IL) { ModuleDefinition module = IL.GetModule(); MethodDefinition method = IL.GetMethod(); diff --git a/src/LinFu.AOP/Emitters/GetInterceptionDisabled.cs b/src/LinFu.AOP/Emitters/GetInterceptionDisabled.cs index a84ad87..6a08263 100644 --- a/src/LinFu.AOP/Emitters/GetInterceptionDisabled.cs +++ b/src/LinFu.AOP/Emitters/GetInterceptionDisabled.cs @@ -40,8 +40,8 @@ public GetInterceptionDisabled(MethodReference hostMethod, VariableDefinition in /// /// Emits the instructions that determine whether or not method interception is disabled. /// - /// The instance responsible for adding or removing instructions to the method body. - public void Emit(CilWorker IL) + /// The instance responsible for adding or removing instructions to the method body. + public void Emit(ILProcessor IL) { ModuleDefinition module = IL.GetModule(); TypeReference modifiableType = module.ImportType(); diff --git a/src/LinFu.AOP/Emitters/GetMethodReplacementProvider.cs b/src/LinFu.AOP/Emitters/GetMethodReplacementProvider.cs index 2208b27..936719c 100644 --- a/src/LinFu.AOP/Emitters/GetMethodReplacementProvider.cs +++ b/src/LinFu.AOP/Emitters/GetMethodReplacementProvider.cs @@ -24,6 +24,9 @@ public class GetMethodReplacementProvider : IInstructionEmitter public GetMethodReplacementProvider(VariableDefinition methodReplacementProvider, MethodDefinition hostMethod, Func resolveGetProviderMethod) { + if (methodReplacementProvider.VariableType.FullName != typeof(IMethodReplacementProvider).FullName) { + throw new InvalidProgramException(); + } _methodReplacementProvider = methodReplacementProvider; _hostMethod = hostMethod; _resolveGetProviderMethod = resolveGetProviderMethod; @@ -34,8 +37,8 @@ public GetMethodReplacementProvider(VariableDefinition methodReplacementProvider /// /// Emits the instructions that obtain the instance. /// - /// The instance. - public void Emit(CilWorker IL) + /// The instance. + public void Emit(ILProcessor IL) { MethodDefinition method = _hostMethod; TypeDefinition declaringType = method.DeclaringType; diff --git a/src/LinFu.AOP/Emitters/GetSurroundingClassImplementation.cs b/src/LinFu.AOP/Emitters/GetSurroundingClassImplementation.cs index 1cf18f0..2cba535 100644 --- a/src/LinFu.AOP/Emitters/GetSurroundingClassImplementation.cs +++ b/src/LinFu.AOP/Emitters/GetSurroundingClassImplementation.cs @@ -36,8 +36,8 @@ public GetSurroundingClassImplementation(VariableDefinition invocationInfo, /// /// Emits the instructions that obtain the instance. /// - /// The that points to the current method body. - public void Emit(CilWorker IL) + /// The that points to the current method body. + public void Emit(ILProcessor IL) { MethodDefinition method = IL.GetMethod(); TypeDefinition declaringType = method.DeclaringType; diff --git a/src/LinFu.AOP/Emitters/GetSurroundingImplementationInstance.cs b/src/LinFu.AOP/Emitters/GetSurroundingImplementationInstance.cs index fc0c087..e949715 100644 --- a/src/LinFu.AOP/Emitters/GetSurroundingImplementationInstance.cs +++ b/src/LinFu.AOP/Emitters/GetSurroundingImplementationInstance.cs @@ -40,7 +40,7 @@ public GetSurroundingImplementationInstance(VariableDefinition aroundInvokeProvi /// Emits the instructions that obtain the current instance. /// /// - public void Emit(CilWorker IL) + public void Emit(ILProcessor IL) { ModuleDefinition module = IL.GetModule(); diff --git a/src/LinFu.AOP/Emitters/InvokeMethodReplacement.cs b/src/LinFu.AOP/Emitters/InvokeMethodReplacement.cs index 408c41e..aa835fa 100644 --- a/src/LinFu.AOP/Emitters/InvokeMethodReplacement.cs +++ b/src/LinFu.AOP/Emitters/InvokeMethodReplacement.cs @@ -40,12 +40,12 @@ public InvokeMethodReplacement(Instruction executeOriginalInstructions, /// /// Emits the instructions that call the method replacement instead of the original method body. /// - /// The that points to the current method body. - public void Emit(CilWorker IL) + /// The that points to the current method body. + public void Emit(ILProcessor IL) { ModuleDefinition module = IL.GetModule(); MethodDefinition method = IL.GetMethod(); - TypeReference returnType = method.ReturnType.ReturnType; + TypeReference returnType = method.ReturnType; VariableDefinition methodReplacement = MethodDefinitionExtensions.AddLocal(method, typeof (IInterceptor)); GetMethodReplacementInstance(method, IL, methodReplacement, _methodReplacementProvider, _invocationInfo); @@ -66,7 +66,7 @@ public void Emit(CilWorker IL) #endregion - private static void InvokeInterceptor(ModuleDefinition module, CilWorker IL, + private static void InvokeInterceptor(ModuleDefinition module, ILProcessor IL, VariableDefinition methodReplacement, TypeReference returnType, VariableDefinition invocationInfo) { @@ -78,7 +78,7 @@ private static void InvokeInterceptor(ModuleDefinition module, CilWorker IL, } private static void GetMethodReplacementInstance(MethodDefinition method, - CilWorker IL, + ILProcessor IL, VariableDefinition methodReplacement, VariableDefinition methodReplacementProvider, VariableDefinition invocationInfo) diff --git a/src/LinFu.AOP/Emitters/SaveReturnValue.cs b/src/LinFu.AOP/Emitters/SaveReturnValue.cs index c0d67bf..193fcb9 100644 --- a/src/LinFu.AOP/Emitters/SaveReturnValue.cs +++ b/src/LinFu.AOP/Emitters/SaveReturnValue.cs @@ -29,8 +29,8 @@ public SaveReturnValue(TypeReference returnType, VariableDefinition returnValue) /// /// Saves the return value from a given method call. /// - /// The pointing to the target method body. - public void Emit(CilWorker IL) + /// The pointing to the target method body. + public void Emit(ILProcessor IL) { ModuleDefinition module = IL.GetModule(); TypeReference voidType = module.ImportType(typeof (void)); @@ -39,7 +39,7 @@ public void Emit(CilWorker IL) if (_returnType is GenericParameter || returnTypeIsValueType) IL.Create(OpCodes.Box, _returnType); - if (_returnType != voidType) + if (_returnType.FullName != voidType.FullName) IL.Create(OpCodes.Stloc, _returnValue); } diff --git a/src/LinFu.AOP/Emitters/SurroundMethodBody.cs b/src/LinFu.AOP/Emitters/SurroundMethodBody.cs index f7c55bc..a1ea4a5 100644 --- a/src/LinFu.AOP/Emitters/SurroundMethodBody.cs +++ b/src/LinFu.AOP/Emitters/SurroundMethodBody.cs @@ -79,8 +79,8 @@ public SurroundMethodBody(VariableDefinition methodReplacementProvider, /// /// Adds a prolog to the given method body. /// - /// The that points to the given method body. - public void AddProlog(CilWorker IL) + /// The that points to the given method body. + public void AddProlog(ILProcessor IL) { MethodDefinition method = IL.GetMethod(); _surroundingImplementation = method.AddLocal(); @@ -131,8 +131,8 @@ public void AddProlog(CilWorker IL) /// /// Adds an epilog to the given method body. /// - /// The that points to the given method body. - public void AddEpilog(CilWorker IL) + /// The that points to the given method body. + public void AddEpilog(ILProcessor IL) { Instruction skipEpilog = IL.Create(OpCodes.Nop); diff --git a/src/LinFu.AOP/Extensions/CecilVisitorExtensions.cs b/src/LinFu.AOP/Extensions/CecilVisitorExtensions.cs index 42d3725..8970179 100644 --- a/src/LinFu.AOP/Extensions/CecilVisitorExtensions.cs +++ b/src/LinFu.AOP/Extensions/CecilVisitorExtensions.cs @@ -1,59 +1,272 @@ -using LinFu.AOP.Cecil.Interfaces; -using Mono.Cecil; - -namespace LinFu.AOP.Cecil.Extensions -{ - /// - /// A helper class that extends Cecil to support LinFu's weaver model. - /// - public static class CecilVisitorExtensions - { - /// - /// Allows a instance to traverse any - /// instance. - /// - /// The visitable object. - /// The type weaver. - public static void Accept(this IReflectionVisitable visitable, ITypeWeaver typeWeaver) - { - var visitor = new TypeWeaverVisitor(typeWeaver); - visitable.Accept(visitor); - } - - /// - /// Allows a instance to traverse any - /// instance. - /// - /// The visitable object. - /// The type weaver. - public static void Accept(this IReflectionStructureVisitable visitable, ITypeWeaver typeWeaver) - { - var visitor = new TypeWeaverVisitor(typeWeaver); - visitable.Accept(visitor); - } - - /// - /// Allows a instance to traverse any - /// instance. - /// - /// The visitable object. - /// The method weaver. - public static void Accept(this IReflectionStructureVisitable visitable, IMethodWeaver methodWeaver) - { - var visitor = new MethodWeaverVisitor(methodWeaver); - visitable.Accept(visitor); - } - - /// - /// Allows a instance to traverse any - /// instance. - /// - /// The visitable object. - /// The method weaver. - public static void Accept(this IReflectionVisitable visitable, IMethodWeaver methodWeaver) - { - var visitor = new MethodWeaverVisitor(methodWeaver); - visitable.Accept(visitor); - } - } +using System; +using System.Collections.Generic; +using LinFu.AOP.Cecil.Interfaces; +using Mono.Cecil; + +namespace LinFu.AOP.Cecil.Extensions +{ + /// + /// A helper class that extends Cecil to support LinFu's weaver model. + /// + public static class CecilVisitorExtensions + { + /// + /// Allows a instance to traverse any + /// instance. + /// + /// The visitable object. + /// The type weaver. + public static void Accept(this object visitable, ITypeWeaver typeWeaver) + { + var visitor = new TypeWeaverVisitor(typeWeaver); + visitable.Accept(visitor); + } + + /// + /// Allows a instance to traverse any + /// instance. + /// + /// The visitable object. + /// The method weaver. + public static void Accept(this object visitable, IMethodWeaver methodWeaver) + { + var visitor = new MethodWeaverVisitor(methodWeaver); + visitable.Accept(visitor); + } + + public static void Accept(this object visitable, BaseReflectionVisitor visitor) + { + if (visitable is AssemblyDefinition) { + var assemblyDefinition = (AssemblyDefinition) visitable; + assemblyDefinition.Accept(visitor); + } else if (visitable is TypeDefinition) { + var typeDefinition = (TypeDefinition) visitable; + typeDefinition.Accept(visitor); + } else { + throw new NotImplementedException(); + } + } + + + public static void Accept(this AssemblyDefinition visitable, BaseReflectionVisitor visitor) + { + visitor.VisitAssemblyDefinition (visitable); + + visitable.Name.Accept (visitor); + visitable.Modules.Accept (visitor); + + visitor.TerminateAssemblyDefinition (visitable); + } + + public static void Accept(this AssemblyLinkedResource visitable, BaseReflectionVisitor visitor) + { + visitor.VisitAssemblyLinkedResource (visitable); + } + + public static void Accept(this AssemblyNameDefinition visitable, BaseReflectionVisitor visitor) + { + visitor.VisitAssemblyNameDefinition (visitable); + } + + public static void Accept(this AssemblyNameReference visitable, BaseReflectionVisitor visitor) + { + visitor.VisitAssemblyNameReference (visitable); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitAssemblyNameReferenceCollection (visitable); + } + + public static void Accept(this EmbeddedResource visitable, BaseReflectionVisitor visitor) + { + visitor.VisitEmbeddedResource (visitable); + } + + public static void Accept(this LinkedResource visitable, BaseReflectionVisitor visitor) + { + visitor.VisitLinkedResource (visitable); + } + + public static void Accept(this ModuleDefinition visitable, BaseReflectionVisitor visitor) + { + visitor.VisitModuleDefinition (visitable); + + visitable.AssemblyReferences.Accept (visitor); + visitable.ModuleReferences.Accept (visitor); + visitable.Resources.Accept (visitor); + + /////// + + visitable.Types.Accept (visitor); + visitable.GetTypeReferences().Accept (visitor); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitModuleDefinitionCollection (visitable); + } + + public static void Accept(this ModuleReference visitable, BaseReflectionVisitor visitor) + { + visitor.VisitModuleReference (visitable); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitModuleReferenceCollection (visitable); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitResourceCollection (visitable); + } + + public static void Accept(this CustomAttribute visitable, BaseReflectionVisitor visitor) + { + visitor.VisitCustomAttribute (visitable); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitCustomAttributeCollection (visitable); + } + + public static void Accept(this EventDefinition visitable, BaseReflectionVisitor visitor) + { + visitor.VisitEventDefinition (visitable); + + visitable.CustomAttributes.Accept (visitor); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitEventDefinitionCollection (visitable); + } + + public static void Accept(this FieldDefinition visitable, BaseReflectionVisitor visitor) + { + visitor.VisitFieldDefinition (visitable); + + //if (visitable.MarshalSpec != null) + // visitable.MarshalSpec.Accept (visitor); + + visitable.CustomAttributes.Accept (visitor); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitFieldDefinitionCollection (visitable); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitGenericParameterCollection (visitable); + } + + public static void Accept(this MemberReference visitable, BaseReflectionVisitor visitor) + { + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitMemberReferenceCollection (visitable); + } + + public static void Accept(this MethodDefinition visitable, BaseReflectionVisitor visitor) + { + visitor.VisitMethodDefinition (visitable); + + visitable.GenericParameters.Accept (visitor); + visitable.Parameters.Accept (visitor); + + if (visitable.PInvokeInfo != null) + visitable.PInvokeInfo.Accept (visitor); + + visitable.SecurityDeclarations.Accept (visitor); + //visitable.Overrides.Accept (visitor); + visitable.CustomAttributes.Accept (visitor); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitMethodDefinitionCollection (visitable); + } + + public static void Accept(this ParameterDefinition visitable, BaseReflectionVisitor visitor) + { + visitor.VisitParameterDefinition (visitable); + + //if (visitable.MarshalSpec != null) + // visitable.MarshalSpec.Accept (visitor); + + visitable.CustomAttributes.Accept (visitor); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitParameterDefinitionCollection (visitable); + } + + public static void Accept(this ParameterReference visitable, BaseReflectionVisitor visitor) + { + } + + public static void Accept(this PInvokeInfo visitable, BaseReflectionVisitor visitor) + { + visitor.VisitPInvokeInfo (visitable); + } + + public static void Accept(this PropertyDefinition visitable, BaseReflectionVisitor visitor) + { + visitor.VisitPropertyDefinition (visitable); + + visitable.CustomAttributes.Accept (visitor); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitPropertyDefinitionCollection (visitable); + } + + public static void Accept(this SecurityDeclaration visitable, BaseReflectionVisitor visitor) + { + visitor.VisitSecurityDeclaration (visitable); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitSecurityDeclarationCollection (visitable); + } + + public static void Accept(this TypeDefinition visitable, BaseReflectionVisitor visitor) + { + visitor.VisitTypeDefinition (visitable); + + visitable.GenericParameters.Accept (visitor); + visitable.Interfaces.Accept (visitor); + //visitable.Constructors.Accept (visitor); + visitable.Methods.Accept (visitor); + visitable.Fields.Accept (visitor); + visitable.Properties.Accept (visitor); + visitable.Events.Accept (visitor); + visitable.NestedTypes.Accept (visitor); + visitable.CustomAttributes.Accept (visitor); + visitable.SecurityDeclarations.Accept (visitor); + } + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitTypeDefinitionCollection (visitable); + } + + public static void Accept(this TypeReference visitable, BaseReflectionVisitor visitor) + { + visitor.VisitTypeReference (visitable); + } + + public static void Accept(this IEnumerable visitable, BaseReflectionVisitor visitor) + { + visitor.VisitTypeReferenceCollection (visitable); + } + } } \ No newline at end of file diff --git a/src/LinFu.AOP/Extensions/ExceptionHandlerInterceptionExtensions.cs b/src/LinFu.AOP/Extensions/ExceptionHandlerInterceptionExtensions.cs index ebcacd0..d2051a4 100644 --- a/src/LinFu.AOP/Extensions/ExceptionHandlerInterceptionExtensions.cs +++ b/src/LinFu.AOP/Extensions/ExceptionHandlerInterceptionExtensions.cs @@ -13,38 +13,18 @@ public static class ExceptionHandlerInterceptionExtensions /// Enables exception interception on the given type. /// /// The target type. - public static void InterceptAllExceptions(this IReflectionVisitable visitable) + public static void InterceptAllExceptions(this object visitable) { Func filter = GetMethodFilter(); InterceptExceptions(visitable, filter); } - /// - /// Enables exception interception on the given type. - /// - /// The target type. - public static void InterceptAllExceptions(this IReflectionStructureVisitable visitable) - { - Func filter = GetMethodFilter(); - InterceptExceptions(visitable, filter); - } - - /// - /// Enables exception interception on the given type. - /// - /// The target type. - /// The instance that will determine which methods should support exception interception. - public static void InterceptExceptions(this IReflectionVisitable visitable, IMethodFilter methodFilter) - { - visitable.InterceptExceptions(methodFilter.ShouldWeave); - } - /// /// Enables exception interception on the given type. /// /// The target type. /// The instance that will determine which methods should support exception interception. - public static void InterceptExceptions(this IReflectionStructureVisitable visitable, IMethodFilter methodFilter) + public static void InterceptExceptions(this object visitable, IMethodFilter methodFilter) { visitable.InterceptExceptions(methodFilter.ShouldWeave); } @@ -54,22 +34,7 @@ public static void InterceptExceptions(this IReflectionStructureVisitable visita /// /// The target type. /// The method filter functor that will determine which methods should support exception interception. - public static void InterceptExceptions(this IReflectionStructureVisitable visitable, - Func methodFilter) - { - if (visitable == null) - throw new ArgumentNullException("visitable"); - - var catchAllThrownExceptions = new CatchAllThrownExceptions(); - visitable.WeaveWith(catchAllThrownExceptions, methodFilter); - } - - /// - /// Enables exception interception on the given type. - /// - /// The target type. - /// The method filter functor that will determine which methods should support exception interception. - public static void InterceptExceptions(this IReflectionVisitable visitable, + public static void InterceptExceptions(this object visitable, Func methodFilter) { if (visitable == null) diff --git a/src/LinFu.AOP/Extensions/FieldInterceptionExtensions.cs b/src/LinFu.AOP/Extensions/FieldInterceptionExtensions.cs index 0f61e99..3219426 100644 --- a/src/LinFu.AOP/Extensions/FieldInterceptionExtensions.cs +++ b/src/LinFu.AOP/Extensions/FieldInterceptionExtensions.cs @@ -13,7 +13,7 @@ public static class FieldInterceptionExtensions /// Adds field interception support to the target type. /// /// The type that will be modified. - public static void InterceptAllFields(this IReflectionStructureVisitable targetType) + public static void InterceptAllFields(this object targetType) { Func methodFilter = GetMethodFilter(); targetType.InterceptFields(methodFilter, f => true); @@ -23,7 +23,7 @@ public static void InterceptAllFields(this IReflectionStructureVisitable targetT /// Adds field interception support intercepting all instance fields on the target type. /// /// The type that will be modified. - public static void InterceptAllInstanceFields(this IReflectionStructureVisitable targetType) + public static void InterceptAllInstanceFields(this object targetType) { Func methodFilter = GetMethodFilter(); Func fieldFilter = GetFieldFilter(f => !f.IsStatic); @@ -35,7 +35,7 @@ public static void InterceptAllInstanceFields(this IReflectionStructureVisitable /// Adds field interception support intercepting all static fields on the target type. /// /// The type that will be modified. - public static void InterceptAllStaticFields(this IReflectionStructureVisitable targetType) + public static void InterceptAllStaticFields(this object targetType) { Func methodFilter = GetMethodFilter(); Func fieldFilter = GetFieldFilter(f => f.IsStatic); @@ -43,47 +43,13 @@ public static void InterceptAllStaticFields(this IReflectionStructureVisitable t targetType.InterceptFields(methodFilter, fieldFilter); } - /// - /// Adds field interception support to the target type. - /// - /// The type that will be modified. - public static void InterceptAllFields(this IReflectionVisitable targetType) - { - Func methodFilter = GetMethodFilter(); - targetType.InterceptFields(methodFilter, f => true); - } - - /// - /// Adds field interception support intercepting all instance fields on the target type. - /// - /// The type that will be modified. - public static void InterceptAllInstanceFields(this IReflectionVisitable targetType) - { - Func methodFilter = GetMethodFilter(); - Func fieldFilter = GetFieldFilter(f => !f.IsStatic); - - targetType.InterceptFields(methodFilter, fieldFilter); - } - - /// - /// Adds field interception support intercepting all static fields on the target type. - /// - /// The type that will be modified. - public static void InterceptAllStaticFields(this IReflectionVisitable targetType) - { - Func methodFilter = GetMethodFilter(); - Func fieldFilter = GetFieldFilter(actualField => actualField.IsStatic); - - targetType.InterceptFields(methodFilter, fieldFilter); - } - /// /// Adds field interception support to the target type. /// /// The type that will be modified. /// The filter that determines which methods on the target type will be modified to support field interception. /// The filter that determines which fields should be intercepted. - public static void InterceptFields(this IReflectionVisitable targetType, + public static void InterceptFields(this object targetType, Func methodFilter, Func fieldFilter) { @@ -100,7 +66,7 @@ public static void InterceptFields(this IReflectionVisitable targetType, /// The type that will be modified. /// The filter that determines the host types to be modified. /// The field filter that determines the fields that will be intercepted. - public static void InterceptFields(this IReflectionStructureVisitable targetType, ITypeFilter hostTypeFilter, + public static void InterceptFields(this object targetType, ITypeFilter hostTypeFilter, IFieldFilter fieldFilter) { var typeWeaver = new ImplementFieldInterceptionHostWeaver(hostTypeFilter.ShouldWeave); @@ -110,23 +76,6 @@ public static void InterceptFields(this IReflectionStructureVisitable targetType targetType.Accept(typeWeaver); } - /// - /// Adds field interception support to the target type. - /// - /// The type that will be modified. - /// The filter that determines which methods on the target type will be modified to support field interception. - /// The filter that determines which fields should be intercepted. - public static void InterceptFields(this IReflectionStructureVisitable targetType, - Func methodFilter, - Func fieldFilter) - { - var typeWeaver = new ImplementFieldInterceptionHostWeaver(t => true); - var fieldWeaver = new InterceptFieldAccess(fieldFilter); - - targetType.WeaveWith(fieldWeaver, methodFilter); - targetType.Accept(typeWeaver); - } - private static Func GetMethodFilter() { return m => true; diff --git a/src/LinFu.AOP/Extensions/MethodBodyInterceptionExtensions.cs b/src/LinFu.AOP/Extensions/MethodBodyInterceptionExtensions.cs index 0579a48..97ff547 100644 --- a/src/LinFu.AOP/Extensions/MethodBodyInterceptionExtensions.cs +++ b/src/LinFu.AOP/Extensions/MethodBodyInterceptionExtensions.cs @@ -13,16 +13,7 @@ public static class MethodBodyInterceptionExtensions /// Intercepts all method bodies on the target item. /// /// The target to be modified. - public static void InterceptAllMethodBodies(this IReflectionStructureVisitable target) - { - target.InterceptMethodBody(m => true); - } - - /// - /// Intercepts all method bodies on the target item. - /// - /// The target to be modified. - public static void InterceptAllMethodBodies(this IReflectionVisitable target) + public static void InterceptAllMethodBodies(this object target) { target.InterceptMethodBody(m => true); } @@ -32,7 +23,7 @@ public static void InterceptAllMethodBodies(this IReflectionVisitable target) /// /// The target to be modified. /// The method filter that will determine the methods that will be modified. - public static void InterceptMethodBody(this IReflectionVisitable target, IMethodFilter methodFilter) + public static void InterceptMethodBody(this object target, IMethodFilter methodFilter) { target.InterceptMethodBody(methodFilter.ShouldWeave); } @@ -42,32 +33,7 @@ public static void InterceptMethodBody(this IReflectionVisitable target, IMethod /// /// The target to be modified. /// The method filter that will determine the methods that will be modified. - public static void InterceptMethodBody(this IReflectionStructureVisitable target, IMethodFilter methodFilter) - { - target.InterceptMethodBody(methodFilter.ShouldWeave); - } - - /// - /// Intercepts all method bodies on the target item. - /// - /// The target to be modified. - /// The method filter that will determine the methods that will be modified. - public static void InterceptMethodBody(this IReflectionStructureVisitable target, - Func methodFilter) - { - Func typeFilter = GetTypeFilter(); - target.Accept(new ImplementModifiableType(typeFilter)); - - var interceptMethodBody = new InterceptMethodBody(methodFilter); - target.WeaveWith(interceptMethodBody, methodFilter); - } - - /// - /// Intercepts all method bodies on the target item. - /// - /// The target to be modified. - /// The method filter that will determine the methods that will be modified. - public static void InterceptMethodBody(this IReflectionVisitable target, + public static void InterceptMethodBody(this object target, Func methodFilter) { Func typeFilter = GetTypeFilter(); diff --git a/src/LinFu.AOP/Extensions/MethodCallInterceptionExtensions.cs b/src/LinFu.AOP/Extensions/MethodCallInterceptionExtensions.cs index 0c3d72b..5d8e814 100644 --- a/src/LinFu.AOP/Extensions/MethodCallInterceptionExtensions.cs +++ b/src/LinFu.AOP/Extensions/MethodCallInterceptionExtensions.cs @@ -13,29 +13,29 @@ public static class MethodCallInterceptionExtensions /// Modifies the current to support third-party method call interception for all method calls made inside the target. /// /// The target object. - public static void InterceptAllMethodCalls(this IReflectionStructureVisitable target) + public static void InterceptAllMethodCalls(this object target) { target.InterceptMethodCalls(GetDefaultTypeFilter()); } - +/* /// /// Modifies the current to support third-party method call interception for all method calls made inside the target. /// /// The target object. - public static void InterceptAllMethodCalls(this IReflectionVisitable target) + public static void InterceptAllMethodCalls(this object target) { Func hostMethodFilter = GetHostMethodFilter(); Func methodCallFilter = m => true; InterceptMethodCalls(target, GetDefaultTypeFilter(), hostMethodFilter, methodCallFilter); } - +*/ /// /// Modifies the current to support third-party method call interception for all method calls made inside the target. /// /// The target object. /// The type filter that determines which types will be modified for interception. - public static void InterceptMethodCalls(this IReflectionStructureVisitable target, + public static void InterceptMethodCalls(this object target, Func typeFilter) { Func hostMethodFilter = GetHostMethodFilter(); @@ -43,42 +43,27 @@ public static void InterceptMethodCalls(this IReflectionStructureVisitable targe InterceptMethodCalls(target, typeFilter, hostMethodFilter, methodCallFilter); } - +/* /// /// Modifies the current to support third-party method call interception for all method calls made inside the target. /// /// The target object. /// The type filter that determines the types that will be modified. - public static void InterceptMethodCalls(this IReflectionVisitable target, Func typeFilter) + public static void InterceptMethodCalls(this object target, Func typeFilter) { Func hostMethodFilter = GetHostMethodFilter(); Func methodCallFilter = m => true; InterceptMethodCalls(target, typeFilter, hostMethodFilter, methodCallFilter); } - - /// - /// Modifies the current to support third-party method call interception for all method calls made inside the target. - /// - /// The target object. - /// The instance that determines the method calls that will be intercepted. - /// The instance that determines the host method calls that will be modified - public static void InterceptMethodCalls(this IReflectionVisitable target, IMethodCallFilter methodCallFilter, - IMethodFilter hostMethodFilter) - { - var rewriter = new InterceptMethodCalls(methodCallFilter); - target.Accept(new ImplementModifiableType(GetDefaultTypeFilter())); - target.WeaveWith(rewriter, hostMethodFilter.ShouldWeave); - } - +*/ /// /// Modifies the current to support third-party method call interception for all method calls made inside the target. /// /// The target object. /// The instance that determines the method calls that will be intercepted. /// The instance that determines the host method calls that will be modified - public static void InterceptMethodCalls(this IReflectionStructureVisitable target, - IMethodCallFilter methodCallFilter, + public static void InterceptMethodCalls(this object target, IMethodCallFilter methodCallFilter, IMethodFilter hostMethodFilter) { var rewriter = new InterceptMethodCalls(methodCallFilter); @@ -93,7 +78,7 @@ public static void InterceptMethodCalls(this IReflectionStructureVisitable targe /// The filter that will determine the target types that will be modified. /// The filter that will determine the methods that will be modified on the target type. /// The filter that will determine which third-party methods will be intercepted on the target type. - public static void InterceptMethodCalls(this IReflectionStructureVisitable target, + public static void InterceptMethodCalls(this object target, Func typeFilter, Func hostMethodFilter, Func methodCallFilter) @@ -103,22 +88,6 @@ public static void InterceptMethodCalls(this IReflectionStructureVisitable targe target.WeaveWith(rewriter, hostMethodFilter); } - /// - /// Modifies the current to support third-party method call interception. - /// - /// The target object. - /// The filter that will determine the target types that will be modified. - /// The filter that will determine the methods that will be modified on the target type. - /// The filter that will determine which third-party methods will be intercepted on the target type. - public static void InterceptMethodCalls(this IReflectionVisitable target, Func typeFilter, - Func hostMethodFilter, - Func methodCallFilter) - { - var rewriter = new InterceptMethodCalls(hostMethodFilter, methodCallFilter); - target.Accept(new ImplementModifiableType(typeFilter)); - target.WeaveWith(rewriter, hostMethodFilter); - } - private static Func GetDefaultTypeFilter() { return type => diff --git a/src/LinFu.AOP/Extensions/MethodRewriterExtensions.cs b/src/LinFu.AOP/Extensions/MethodRewriterExtensions.cs index c6675c6..8a62d1b 100644 --- a/src/LinFu.AOP/Extensions/MethodRewriterExtensions.cs +++ b/src/LinFu.AOP/Extensions/MethodRewriterExtensions.cs @@ -15,20 +15,7 @@ public static class MethodRewriterExtensions /// The transformation target. /// The method rewriter. /// The method filter that determines which methods will be rewritten. - public static void WeaveWith(this IReflectionStructureVisitable target, IMethodRewriter rewriter, - Func filter) - { - var weaver = new MethodWeaver(rewriter, filter); - target.Accept(weaver); - } - - /// - /// Transforms the methods in the using the given method rewriter. - /// - /// The transformation target. - /// The method rewriter. - /// The method filter that determines which methods will be rewritten. - public static void WeaveWith(this IReflectionVisitable target, IMethodRewriter rewriter, + public static void WeaveWith(this object target, IMethodRewriter rewriter, Func filter) { var weaver = new MethodWeaver(rewriter, filter); diff --git a/src/LinFu.AOP/Extensions/NewOperatorInterceptionExtensions.cs b/src/LinFu.AOP/Extensions/NewOperatorInterceptionExtensions.cs index 6b40796..fdd419f 100644 --- a/src/LinFu.AOP/Extensions/NewOperatorInterceptionExtensions.cs +++ b/src/LinFu.AOP/Extensions/NewOperatorInterceptionExtensions.cs @@ -13,21 +13,12 @@ public static class NewOperatorInterceptionExtensions /// Modifies a to support intercepting all calls to the 'new' operator. /// /// The assembly to be modified. - public static void InterceptAllNewInstances(this IReflectionStructureVisitable target) + public static void InterceptAllNewInstances(this object target) { Func typeFilter = GetTypeFilter(); target.InterceptNewInstances(typeFilter); } - /// - /// Modifies a to support intercepting all calls to the 'new' operator. - /// - /// The assembly to be modified. - public static void InterceptAllNewInstances(this IReflectionVisitable target) - { - Func typeFilter = GetTypeFilter(); - target.InterceptNewInstances(typeFilter); - } /// /// Modifies a to support intercepting calls to the 'new' operator. @@ -43,7 +34,7 @@ public static void InterceptAllNewInstances(this IReflectionVisitable target) /// concreteType => concreteType.Name == "Foo"; /// /// - public static void InterceptNewInstances(this IReflectionVisitable target, Func typeFilter, + public static void InterceptNewInstances(this object target, Func typeFilter, Func methodFilter) { Func constructorFilter = @@ -58,32 +49,6 @@ public static void InterceptNewInstances(this IReflectionVisitable target, Func< } - /// - /// Modifies a assembly to support intercepting calls to the 'new' operator. - /// - /// The assembly to be modified. - /// The functor that determines which type instantiations should be intercepted. - /// The filter that determines which host methods will be modified - /// - /// The type filter determines which concrete types and constructors should be intercepted at runtime. - /// For example, the following functor code intercepts types named "Foo": - /// - /// Func<MethodReference, TypeReference, bool> filter = - /// (constructor, concreteType, hostMethod) => concreteType.Name == "Foo"; - /// - /// - public static void InterceptNewInstances(this IReflectionStructureVisitable target, - Func constructorFilter, - Func methodFilter) - { - Func filter = - (ctor, declaringType, declaringMethod) => - constructorFilter(ctor, declaringType) && methodFilter(declaringMethod); - - var redirector = new RedirectNewInstancesToActivator(filter); - target.InterceptNewInstancesWith(redirector, methodFilter); - } - /// /// Modifies a assembly to support intercepting calls to the 'new' operator. /// @@ -97,85 +62,12 @@ public static void InterceptNewInstances(this IReflectionStructureVisitable targ /// concreteType => concreteType.Name == "Foo"; /// /// - public static void InterceptNewInstances(this IReflectionStructureVisitable target, + public static void InterceptNewInstances(this object target, Func typeFilter) { target.InterceptNewInstances(typeFilter, m => true); } - /// - /// Modifies a assembly to support intercepting calls to the 'new' operator. - /// - /// The assembly to be modified. - /// The functor that determines which type instantiations should be intercepted. - /// - /// The type filter determines the concrete types that should be intercepted at runtime. - /// For example, the following functor code intercepts types named "Foo": - /// - /// Func<TypeReference, bool> filter = - /// concreteType => concreteType.Name == "Foo"; - /// - /// - public static void InterceptNewInstances(this IReflectionVisitable target, Func typeFilter) - { - target.InterceptNewInstances(typeFilter, m => true); - } - - /// - /// Modifies the to support intercepting calls to the 'new' operator. - /// - /// The item to be modified. - /// The filter that determines which host methods will be modified - /// The filter that determines which types will be modified. - /// - /// The type filter determines which concrete types and constructors should be intercepted at runtime. - /// For example, the following functor code intercepts types named "Foo": - /// - /// Func<MethodReference, TypeReference, bool> filter = - /// (constructor, concreteType, hostMethod) => concreteType.Name == "Foo"; - /// - /// - public static void InterceptNewInstances(this IReflectionStructureVisitable target, - Func typeFilter, - Func methodFilter) - { - Func constructorFilter = - (constructor, declaringType) => methodFilter(constructor) && typeFilter(declaringType); - - Func filter = - (ctor, declaringType, declaringMethod) => - constructorFilter(ctor, declaringType) && methodFilter(declaringMethod); - - var redirector = new RedirectNewInstancesToActivator(filter); - target.InterceptNewInstancesWith(redirector, methodFilter); - } - - - /// - /// Modifies the to support intercepting calls to the 'new' operator. - /// - /// The item to be modified. - /// The functor that determines which type instantiations should be intercepted. - /// The filter that determines which host methods will be modified - /// - /// The type filter determines which concrete types and constructors should be intercepted at runtime. - /// For example, the following functor code intercepts types named "Foo": - /// - /// Func<MethodReference, TypeReference, bool> filter = - /// (constructor, concreteType, hostMethod) => concreteType.Name == "Foo"; - /// - /// - public static void InterceptNewInstances(this IReflectionVisitable target, - Func constructorFilter, - Func methodFilter) - { - Func filter = - (ctor, declaringType, declaringMethod) => - constructorFilter(ctor, declaringType) && methodFilter(declaringMethod); - - var redirector = new RedirectNewInstancesToActivator(filter); - target.InterceptNewInstancesWith(redirector, methodFilter); - } /// /// Modifies the to support intercepting calls to the 'new' operator. @@ -183,51 +75,26 @@ public static void InterceptNewInstances(this IReflectionVisitable target, /// The item to be modified. /// The filter that will determine which constructor calls should be intercepted. /// The filter that will determine which host methods should be modified to support new instance interception. - public static void InterceptNewInstances(this IReflectionStructureVisitable target, + public static void InterceptNewInstances(this object target, INewInstanceFilter newInstanceFilter, IMethodFilter methodFilter) { var redirector = new RedirectNewInstancesToActivator(newInstanceFilter); target.InterceptNewInstancesWith(redirector, methodFilter.ShouldWeave); } - /// - /// Modifies the to support intercepting calls to the 'new' operator. - /// - /// The item to be modified. - /// The filter that will determine which constructor calls should be intercepted. - /// The filter that will determine which host methods should be modified to support new instance interception. - public static void InterceptNewInstances(this IReflectionVisitable target, INewInstanceFilter newInstanceFilter, - IMethodFilter methodFilter) - { - var redirector = new RedirectNewInstancesToActivator(newInstanceFilter); - target.InterceptNewInstancesWith(redirector, methodFilter.ShouldWeave); - } - /// /// Modifies the methods in the given using the custom instance. /// /// The host that contains the methods that will be modified. /// The custom that will replace all calls to the new operator with the custom code emitted by the given weaver. /// The method filter that will determine which methods should be modified. - public static void InterceptNewInstancesWith(this IReflectionStructureVisitable target, INewObjectWeaver weaver, + public static void InterceptNewInstancesWith(this object target, INewObjectWeaver weaver, Func filter) { var interceptNewCalls = new InterceptNewCalls(weaver); target.WeaveWith(interceptNewCalls, filter); } - /// - /// Modifies the methods in the given using the custom instance. - /// - /// The host that contains the methods that will be modified. - /// The custom that will replace all calls to the new operator with the custom code emitted by the given weaver. - /// The method filter that will determine which methods should be modified. - public static void InterceptNewInstancesWith(this IReflectionVisitable target, INewObjectWeaver weaver, - Func filter) - { - var interceptNewCalls = new InterceptNewCalls(weaver); - target.WeaveWith(interceptNewCalls, filter); - } private static Func GetTypeFilter() { diff --git a/src/LinFu.AOP/FieldInterception/ImplementFieldInterceptionHostWeaver.cs b/src/LinFu.AOP/FieldInterception/ImplementFieldInterceptionHostWeaver.cs index eff82c3..134f2c0 100644 --- a/src/LinFu.AOP/FieldInterception/ImplementFieldInterceptionHostWeaver.cs +++ b/src/LinFu.AOP/FieldInterception/ImplementFieldInterceptionHostWeaver.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Linq; using LinFu.AOP.Cecil.Interfaces; using LinFu.AOP.Interfaces; using LinFu.Reflection.Emit; @@ -52,12 +53,12 @@ public bool ShouldWeave(TypeDefinition item) public void Weave(TypeDefinition type) { // Implement IActivatorHost only once - if (type.Interfaces.Contains(_hostInterfaceType)) + if (type.Interfaces.Any(typeReference => typeReference.FullName == _hostInterfaceType.FullName)) return; type.AddProperty("FieldInterceptor", _interceptorPropertyType); - if (!type.Interfaces.Contains(_hostInterfaceType)) + if (!type.Interfaces.Any(typeReference => typeReference.FullName == _hostInterfaceType.FullName)) type.Interfaces.Add(_hostInterfaceType); } diff --git a/src/LinFu.AOP/FieldInterception/InterceptFieldAccess.cs b/src/LinFu.AOP/FieldInterception/InterceptFieldAccess.cs index 122169a..f03c729 100644 --- a/src/LinFu.AOP/FieldInterception/InterceptFieldAccess.cs +++ b/src/LinFu.AOP/FieldInterception/InterceptFieldAccess.cs @@ -111,8 +111,8 @@ protected override bool ShouldReplace(Instruction oldInstruction, MethodDefiniti /// /// The instruction currently being evaluated. /// The method that contains the target instruction. - /// The CilWorker that will be used to emit the method body instructions. - protected override void Replace(Instruction oldInstruction, MethodDefinition hostMethod, CilWorker IL) + /// The ILProcessor that will be used to emit the method body instructions. + protected override void Replace(Instruction oldInstruction, MethodDefinition hostMethod, ILProcessor IL) { var targetField = (FieldReference) oldInstruction.Operand; TypeReference fieldType = targetField.FieldType; diff --git a/src/LinFu.AOP/ImplementMethodReplacementHost.cs b/src/LinFu.AOP/ImplementMethodReplacementHost.cs index 3882da7..4193a2a 100644 --- a/src/LinFu.AOP/ImplementMethodReplacementHost.cs +++ b/src/LinFu.AOP/ImplementMethodReplacementHost.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Linq; using LinFu.AOP.Cecil.Interfaces; using LinFu.AOP.Interfaces; using LinFu.Reflection.Emit; @@ -27,7 +28,7 @@ public virtual bool ShouldWeave(TypeDefinition item) return false; // Implement the host interface once and only once - if (item.Interfaces.Contains(_hostType)) + if (item.Interfaces.Any(typeReference => typeReference.FullName == _hostType.FullName)) return false; if (_filter != null) @@ -38,7 +39,7 @@ public virtual bool ShouldWeave(TypeDefinition item) public virtual void Weave(TypeDefinition item) { - if (item.Interfaces.Contains(_hostType)) + if (item.Interfaces.Any(typeReference => typeReference.FullName == _hostType.FullName)) return; item.Interfaces.Add(_hostType); diff --git a/src/LinFu.AOP/ImplementModifiableType.cs b/src/LinFu.AOP/ImplementModifiableType.cs index a97743a..d6b26c4 100644 --- a/src/LinFu.AOP/ImplementModifiableType.cs +++ b/src/LinFu.AOP/ImplementModifiableType.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Linq; using LinFu.AOP.Interfaces; using LinFu.Reflection.Emit; using Mono.Cecil; @@ -27,7 +28,7 @@ public override bool ShouldWeave(TypeDefinition item) bool shouldWeave = base.ShouldWeave(item); // Make sure that the IModifiableType interface is only implemented once - shouldWeave &= !item.Interfaces.Contains(_modifiableInterfaceType); + shouldWeave &= !item.Interfaces.Any(typeReference => typeReference.FullName == _modifiableInterfaceType.FullName); bool isStaticClass = item.IsAbstract && item.IsSealed; shouldWeave &= !isStaticClass; @@ -40,7 +41,7 @@ public override void Weave(TypeDefinition item) base.Weave(item); // Implement IModifiableType - if (item.Interfaces.Contains(_modifiableInterfaceType)) + if (item.Interfaces.Any(typeReference => typeReference.FullName == _modifiableInterfaceType.FullName)) return; item.Interfaces.Add(_modifiableInterfaceType); diff --git a/src/LinFu.AOP/InstructionSwapper.cs b/src/LinFu.AOP/InstructionSwapper.cs index 5fa2574..70ea964 100644 --- a/src/LinFu.AOP/InstructionSwapper.cs +++ b/src/LinFu.AOP/InstructionSwapper.cs @@ -14,9 +14,9 @@ public abstract class InstructionSwapper : BaseMethodRewriter /// Rewrites the instructions in the target method body. /// /// The target method. - /// The instance that represents the method body. + /// The instance that represents the method body. /// The IL instructions of the original method body. - protected override void RewriteMethodBody(MethodDefinition method, CilWorker IL, + protected override void RewriteMethodBody(MethodDefinition method, ILProcessor IL, IEnumerable oldInstructions) { var newInstructions = new Queue(); @@ -45,7 +45,7 @@ protected override void RewriteMethodBody(MethodDefinition method, CilWorker IL, /// /// The instruction currently being evaluated. /// The method that contains the target instruction. - /// The CilWorker for the target method body. - protected abstract void Replace(Instruction oldInstruction, MethodDefinition hostMethod, CilWorker IL); + /// The ILProcessor for the target method body. + protected abstract void Replace(Instruction oldInstruction, MethodDefinition hostMethod, ILProcessor IL); } } \ No newline at end of file diff --git a/src/LinFu.AOP/Interfaces/IInstructionEmitter.cs b/src/LinFu.AOP/Interfaces/IInstructionEmitter.cs index 0cd20df..a5c3af2 100644 --- a/src/LinFu.AOP/Interfaces/IInstructionEmitter.cs +++ b/src/LinFu.AOP/Interfaces/IInstructionEmitter.cs @@ -8,9 +8,9 @@ namespace LinFu.AOP.Cecil.Interfaces public interface IInstructionEmitter { /// - /// Emits a set of instructions to the given CilWorker. + /// Emits a set of instructions to the given ILProcessor. /// - /// The responsible for the target method body. - void Emit(CilWorker IL); + /// The responsible for the target method body. + void Emit(ILProcessor IL); } } \ No newline at end of file diff --git a/src/LinFu.AOP/Interfaces/IMethodBodyRewriter.cs b/src/LinFu.AOP/Interfaces/IMethodBodyRewriter.cs index e5af1e1..7bae98b 100644 --- a/src/LinFu.AOP/Interfaces/IMethodBodyRewriter.cs +++ b/src/LinFu.AOP/Interfaces/IMethodBodyRewriter.cs @@ -10,11 +10,11 @@ namespace LinFu.AOP.Cecil.Interfaces public interface IMethodBodyRewriter { /// - /// Rewrites a target method using the given CilWorker. + /// Rewrites a target method using the given ILProcessor. /// /// The target method. - /// The CilWorker that will be used to rewrite the target method. + /// The ILProcessor that will be used to rewrite the target method. /// The original instructions from the target method body. - void Rewrite(MethodDefinition method, CilWorker IL, IEnumerable oldInstructions); + void Rewrite(MethodDefinition method, ILProcessor IL, IEnumerable oldInstructions); } } \ No newline at end of file diff --git a/src/LinFu.AOP/Interfaces/INewObjectWeaver.cs b/src/LinFu.AOP/Interfaces/INewObjectWeaver.cs index eba38a6..bb91583 100644 --- a/src/LinFu.AOP/Interfaces/INewObjectWeaver.cs +++ b/src/LinFu.AOP/Interfaces/INewObjectWeaver.cs @@ -30,10 +30,10 @@ public interface INewObjectWeaver : IHostWeaver /// the . /// /// The method that contains the activation request. - /// The CilWorker that will be used to replace the existing instructions in the method body. + /// The ILProcessor that will be used to replace the existing instructions in the method body. /// The constructor that is currently being used to instantiate the concrete type. /// The that describes the object type that needs to be instantiated. - void EmitNewObject(MethodDefinition hostMethod, CilWorker IL, MethodReference targetConstructor, + void EmitNewObject(MethodDefinition hostMethod, ILProcessor IL, MethodReference targetConstructor, TypeReference concreteType); } } \ No newline at end of file diff --git a/src/LinFu.AOP/Interfaces/ISurroundMethodBody.cs b/src/LinFu.AOP/Interfaces/ISurroundMethodBody.cs index 8b0739b..3f5e88f 100644 --- a/src/LinFu.AOP/Interfaces/ISurroundMethodBody.cs +++ b/src/LinFu.AOP/Interfaces/ISurroundMethodBody.cs @@ -10,13 +10,13 @@ public interface ISurroundMethodBody /// /// Adds a prolog to the given method body. /// - /// The that points to the given method body. - void AddProlog(CilWorker IL); + /// The that points to the given method body. + void AddProlog(ILProcessor IL); /// /// Adds an epilog to the given method body. /// - /// The that points to the given method body. - void AddEpilog(CilWorker IL); + /// The that points to the given method body. + void AddEpilog(ILProcessor IL); } } \ No newline at end of file diff --git a/src/LinFu.AOP/InvocationInfoEmitter.cs b/src/LinFu.AOP/InvocationInfoEmitter.cs index 3e44636..9b7a29f 100644 --- a/src/LinFu.AOP/InvocationInfoEmitter.cs +++ b/src/LinFu.AOP/InvocationInfoEmitter.cs @@ -80,7 +80,7 @@ public void Emit(MethodDefinition targetMethod, MethodReference interceptedMetho VariableDefinition typeArguments = MethodDefinitionExtensions.AddLocal(targetMethod, typeof (Type[])); TypeReference systemType = ModuleDefinitionExtensions.ImportType(module, typeof (Type)); - CilWorker IL = MethodDefinitionExtensions.GetILGenerator(targetMethod); + ILProcessor IL = MethodDefinitionExtensions.GetILGenerator(targetMethod); #region Initialize the InvocationInfo constructor arguments @@ -152,7 +152,7 @@ public void Emit(MethodDefinition targetMethod, MethodReference interceptedMetho // Save the return type MethodReference getTypeFromHandle = module.Import(_getTypeFromHandle); - TypeReference returnType = targetMethod.ReturnType.ReturnType; + TypeReference returnType = targetMethod.ReturnType; IL.Emit(OpCodes.Ldtoken, returnType); IL.Emit(OpCodes.Call, getTypeFromHandle); diff --git a/src/LinFu.AOP/LinFu.AOP.Cecil.csproj b/src/LinFu.AOP/LinFu.AOP.Cecil.csproj index 73c32da..f10df7e 100644 --- a/src/LinFu.AOP/LinFu.AOP.Cecil.csproj +++ b/src/LinFu.AOP/LinFu.AOP.Cecil.csproj @@ -1,169 +1,170 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {613B6547-DCBB-4505-82B8-B4179BFC95CE} - Library - Properties - LinFu.AOP.Cecil - LinFu.AOP.Cecil - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - ..\..\build\Debug\LinFu.AOP.Cecil.xml - - - pdbonly - true - ..\..\build\Release\ - TRACE - prompt - 4 - ..\..\build\Release\LinFu.AOP.Cecil.xml - - - - False - ..\..\lib\Mono.Cecil.dll - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - CommonAssemblyInfo.cs - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - LinFu.AOP.Interfaces - - - {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} - LinFu.IoC.Common - - - {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} - LinFu.Reflection.Emit - - - {22EEB00F-F471-486C-A6AD-60F088821C78} - LinFu.Reflection - - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {613B6547-DCBB-4505-82B8-B4179BFC95CE} + Library + Properties + LinFu.AOP.Cecil + LinFu.AOP.Cecil + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + ..\..\build\Debug\LinFu.AOP.Cecil.xml + + + pdbonly + true + ..\..\build\Release\ + TRACE + prompt + 4 + ..\..\build\Release\LinFu.AOP.Cecil.xml + + + + False + ..\..\lib\Mono.Cecil.dll + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + CommonAssemblyInfo.cs + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + LinFu.AOP.Interfaces + + + {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} + LinFu.IoC.Common + + + {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} + LinFu.Reflection.Emit + + + {22EEB00F-F471-486C-A6AD-60F088821C78} + LinFu.Reflection + + + + + + + \ No newline at end of file diff --git a/src/LinFu.AOP/Loaders/JITWeaver.cs b/src/LinFu.AOP/Loaders/JITWeaver.cs index c34639a..4a6d76e 100644 --- a/src/LinFu.AOP/Loaders/JITWeaver.cs +++ b/src/LinFu.AOP/Loaders/JITWeaver.cs @@ -58,9 +58,9 @@ public virtual IList> AssemblyWeavers /// /// The filename of the target assembly. /// A valid assembly. - public override Assembly Load(string assemblyFile) - { - AssemblyDefinition targetAssembly = AssemblyFactory.GetAssembly(assemblyFile); + public override Assembly Load(string assemblyFile) + { + AssemblyDefinition targetAssembly = AssemblyDefinition.ReadAssembly(assemblyFile); // Strongly-named assemblies cannot be modified if (targetAssembly.Name.HasPublicKey) @@ -91,7 +91,7 @@ public override Assembly Load(string assemblyFile) PdbLoader.SaveSymbols(targetAssembly); // Save the modifed assembly - AssemblyFactory.SaveAssembly(targetAssembly, memoryStream); + targetAssembly.Write(memoryStream); if (PdbLoader == null || !hasSymbols) return targetAssembly.ToAssembly(); diff --git a/src/LinFu.AOP/Loaders/PdbLoader.cs b/src/LinFu.AOP/Loaders/PdbLoader.cs index b08a9b7..19d12e1 100644 --- a/src/LinFu.AOP/Loaders/PdbLoader.cs +++ b/src/LinFu.AOP/Loaders/PdbLoader.cs @@ -33,7 +33,7 @@ public Assembly LoadAssembly(byte[] assemblyArray, byte[] pdbBytes) public void LoadSymbols(AssemblyDefinition assembly) { ModuleDefinition mainModule = assembly.MainModule; - mainModule.LoadSymbols(); +// mainModule.LoadSymbols(); } /// @@ -43,7 +43,7 @@ public void LoadSymbols(AssemblyDefinition assembly) public void SaveSymbols(AssemblyDefinition targetAssembly) { // Update the debug symbols - targetAssembly.MainModule.SaveSymbols(); +// targetAssembly.MainModule.SaveSymbols(); } #endregion diff --git a/src/LinFu.AOP/MethodBodyInterception/InterceptAndSurroundMethodBody.cs b/src/LinFu.AOP/MethodBodyInterception/InterceptAndSurroundMethodBody.cs index c1a56c8..0dc04d4 100644 --- a/src/LinFu.AOP/MethodBodyInterception/InterceptAndSurroundMethodBody.cs +++ b/src/LinFu.AOP/MethodBodyInterception/InterceptAndSurroundMethodBody.cs @@ -54,16 +54,16 @@ public InterceptAndSurroundMethodBody(IEmitInvocationInfo emitter, #region IMethodBodyRewriter Members /// - /// Rewrites a target method using the given CilWorker. + /// Rewrites a target method using the given ILProcessor. /// /// The target method. - /// The CilWorker that will be used to rewrite the target method. + /// The ILProcessor that will be used to rewrite the target method. /// The original instructions from the target method body. - public void Rewrite(MethodDefinition method, CilWorker IL, + public void Rewrite(MethodDefinition method, ILProcessor IL, IEnumerable oldInstructions) { MethodDefinition targetMethod = _parameters.TargetMethod; - CilWorker worker = targetMethod.GetILGenerator(); + ILProcessor worker = targetMethod.GetILGenerator(); ModuleDefinition module = worker.GetModule(); @@ -91,14 +91,14 @@ public void Rewrite(MethodDefinition method, CilWorker IL, _getClassMethodReplacementProvider.Emit(worker); - TypeReference returnType = targetMethod.ReturnType.ReturnType; + TypeReference returnType = targetMethod.ReturnType; _addMethodReplacement.Emit(worker); // Save the return value TypeReference voidType = module.Import(typeof (void)); _surroundMethodBody.AddEpilog(worker); - if (returnType != voidType) + if (returnType.FullName != voidType.FullName) worker.Emit(OpCodes.Ldloc, _parameters.ReturnValue); worker.Emit(OpCodes.Ret); diff --git a/src/LinFu.AOP/MethodBodyInterception/InterceptMethodBody.cs b/src/LinFu.AOP/MethodBodyInterception/InterceptMethodBody.cs index 2c3f349..98b3a39 100644 --- a/src/LinFu.AOP/MethodBodyInterception/InterceptMethodBody.cs +++ b/src/LinFu.AOP/MethodBodyInterception/InterceptMethodBody.cs @@ -39,9 +39,9 @@ protected override bool ShouldRewrite(MethodDefinition targetMethod) /// Rewrites the instructions in the target method body. /// /// The target method. - /// The instance that represents the method body. + /// The instance that represents the method body. /// The IL instructions of the original method body. - protected override void RewriteMethodBody(MethodDefinition method, CilWorker IL, + protected override void RewriteMethodBody(MethodDefinition method, ILProcessor IL, IEnumerable oldInstructions) { if (IsExcluded(method)) @@ -66,8 +66,8 @@ protected override void RewriteMethodBody(MethodDefinition method, CilWorker IL, oldInstructions, interceptionDisabled, invocationInfo, returnValue, - aroundInvokeProvider, methodReplacementProvider, + aroundInvokeProvider, classMethodReplacementProvider, getInstanceMethodReplacementProviderMethod, typeof (AroundMethodBodyRegistry)); @@ -99,7 +99,7 @@ protected override void RewriteMethodBody(MethodDefinition method, CilWorker IL, rewriter.Rewrite(method, IL, oldInstructions); } - private void AddOriginalInstructions(CilWorker IL, IEnumerable oldInstructions) + private void AddOriginalInstructions(ILProcessor IL, IEnumerable oldInstructions) { foreach (Instruction instruction in oldInstructions) { diff --git a/src/LinFu.AOP/MethodBodyInterception/MethodBodyRewriterParameters.cs b/src/LinFu.AOP/MethodBodyInterception/MethodBodyRewriterParameters.cs index bf6a8ea..2ab6f43 100644 --- a/src/LinFu.AOP/MethodBodyInterception/MethodBodyRewriterParameters.cs +++ b/src/LinFu.AOP/MethodBodyInterception/MethodBodyRewriterParameters.cs @@ -13,7 +13,7 @@ namespace LinFu.AOP.Cecil public class MethodBodyRewriterParameters : IMethodBodyRewriterParameters { private readonly VariableDefinition _aroundInvokeProvider; - private readonly CilWorker _cilWorker; + private readonly ILProcessor _cilWorker; private readonly VariableDefinition _classMethodReplacementProvider; private readonly Func _getMethodReplacementProviderMethod; private readonly VariableDefinition _interceptionDisabled; @@ -26,7 +26,7 @@ public class MethodBodyRewriterParameters : IMethodBodyRewriterParameters /// /// Initializes a new instance of the class. /// - /// The CilWorker that is responsible for the current method body. + /// The ILProcessor that is responsible for the current method body. /// The value indicating the list of old instructions in the current method body. /// The value that determines whether or not interception is disabled. /// The local variable that will store the instance. @@ -36,7 +36,7 @@ public class MethodBodyRewriterParameters : IMethodBodyRewriterParameters /// The class-level instance. /// The functor that resolves the GetMethodReplacementProvider method. /// The interception registry type that will be responsible for handling class-level interception events. - public MethodBodyRewriterParameters(CilWorker IL, IEnumerable oldInstructions, + public MethodBodyRewriterParameters(ILProcessor IL, IEnumerable oldInstructions, VariableDefinition interceptionDisabled, VariableDefinition invocationInfo, VariableDefinition returnValue, @@ -51,8 +51,17 @@ public MethodBodyRewriterParameters(CilWorker IL, IEnumerable oldIn _interceptionDisabled = interceptionDisabled; _invocationInfo = invocationInfo; _returnValue = returnValue; + + if (methodReplacementProvider.VariableType.FullName != typeof(IMethodReplacementProvider).FullName) { + throw new InvalidProgramException(); + } _methodReplacementProvider = methodReplacementProvider; + + if (aroundInvokeProvider.VariableType.FullName != typeof(IAroundInvokeProvider).FullName) { + throw new InvalidProgramException(); + } _aroundInvokeProvider = aroundInvokeProvider; + _classMethodReplacementProvider = classMethodReplacementProvider; _getMethodReplacementProviderMethod = getMethodReplacementProviderMethod; _registryType = registryType; diff --git a/src/LinFu.AOP/MethodCallInterception/InterceptMethodCalls.cs b/src/LinFu.AOP/MethodCallInterception/InterceptMethodCalls.cs index 5a25e85..511bb20 100644 --- a/src/LinFu.AOP/MethodCallInterception/InterceptMethodCalls.cs +++ b/src/LinFu.AOP/MethodCallInterception/InterceptMethodCalls.cs @@ -114,12 +114,12 @@ public override void AddLocals(MethodDefinition hostMethod) } protected override void Replace(Instruction oldInstruction, MethodDefinition hostMethod, - CilWorker IL) + ILProcessor IL) { var targetMethod = (MethodReference) oldInstruction.Operand; Instruction callOriginalMethod = IL.Create(OpCodes.Nop); - TypeReference returnType = targetMethod.ReturnType.ReturnType; + TypeReference returnType = targetMethod.ReturnType; Instruction endLabel = IL.Create(OpCodes.Nop); ModuleDefinition module = hostMethod.DeclaringType.Module; @@ -151,7 +151,7 @@ protected override void Replace(Instruction oldInstruction, MethodDefinition hos surroundMethodBody.AddEpilog(IL); } - private void IgnoreLocal(CilWorker IL, VariableDefinition targetVariable, ModuleDefinition module) + private void IgnoreLocal(ILProcessor IL, VariableDefinition targetVariable, ModuleDefinition module) { IL.Emit(OpCodes.Ldloc, targetVariable); @@ -159,10 +159,10 @@ private void IgnoreLocal(CilWorker IL, VariableDefinition targetVariable, Module IL.Emit(OpCodes.Call, addInstance); } - private void Replace(CilWorker IL, Instruction oldInstruction, MethodReference targetMethod, + private void Replace(ILProcessor IL, Instruction oldInstruction, MethodReference targetMethod, MethodDefinition hostMethod, Instruction endLabel, Instruction callOriginalMethod) { - TypeReference returnType = targetMethod.ReturnType.ReturnType; + TypeReference returnType = targetMethod.ReturnType; ModuleDefinition module = hostMethod.DeclaringType.Module; if (!hostMethod.IsStatic) GetInstanceProvider(IL); @@ -237,7 +237,7 @@ private void Replace(CilWorker IL, Instruction oldInstruction, MethodReference t IL.Append(oldInstruction); } - private void GetInstanceProvider(CilWorker IL) + private void GetInstanceProvider(ILProcessor IL) { Instruction skipInstanceProvider = IL.Create(OpCodes.Nop); @@ -255,7 +255,7 @@ private void GetInstanceProvider(CilWorker IL) IL.Append(skipInstanceProvider); } - private void ReconstructMethodArguments(CilWorker IL, MethodReference targetMethod) + private void ReconstructMethodArguments(ILProcessor IL, MethodReference targetMethod) { if (targetMethod.HasThis) IL.Emit(OpCodes.Ldloc, _target); @@ -269,7 +269,7 @@ private void ReconstructMethodArguments(CilWorker IL, MethodReference targetMeth } } - private void SaveInvocationInfo(CilWorker IL, MethodReference targetMethod, ModuleDefinition module, + private void SaveInvocationInfo(ILProcessor IL, MethodReference targetMethod, ModuleDefinition module, TypeReference returnType) { // If the target method is an instance method, then the remaining item on the stack @@ -342,12 +342,12 @@ private void SaveInvocationInfo(CilWorker IL, MethodReference targetMethod, Modu IgnoreLocal(IL, _invocationInfo, module); } - private void PushStackTrace(CilWorker IL, ModuleDefinition module) + private void PushStackTrace(ILProcessor IL, ModuleDefinition module) { IL.PushStackTrace(module); } - private void EmitInterceptorCall(CilWorker IL) + private void EmitInterceptorCall(ILProcessor IL) { // var result = replacement.Intercept(info); IL.Emit(OpCodes.Ldloc, _replacement); @@ -355,7 +355,7 @@ private void EmitInterceptorCall(CilWorker IL) IL.Emit(OpCodes.Callvirt, _intercept); } - private void EmitCanReplace(CilWorker IL, IMethodSignature hostMethod, VariableDefinition provider) + private void EmitCanReplace(ILProcessor IL, IMethodSignature hostMethod, VariableDefinition provider) { Instruction skipGetProvider = IL.Create(OpCodes.Nop); @@ -374,7 +374,7 @@ private void EmitCanReplace(CilWorker IL, IMethodSignature hostMethod, VariableD IL.Append(skipGetProvider); } - private void EmitGetMethodReplacement(CilWorker IL, IMethodSignature hostMethod, VariableDefinition provider) + private void EmitGetMethodReplacement(ILProcessor IL, IMethodSignature hostMethod, VariableDefinition provider) { // var replacement = MethodReplacementProvider.GetReplacement(info); IL.Emit(OpCodes.Ldloc, provider); diff --git a/src/LinFu.AOP/MethodRewriter.cs b/src/LinFu.AOP/MethodRewriter.cs index 5ff7a90..bb85eab 100644 --- a/src/LinFu.AOP/MethodRewriter.cs +++ b/src/LinFu.AOP/MethodRewriter.cs @@ -15,12 +15,12 @@ public abstract class MethodRewriter : IMethodRewriter #region IMethodRewriter Members /// - /// Rewrites a target method using the given CilWorker. + /// Rewrites a target method using the given ILProcessor. /// /// The target method. - /// The CilWorker that will be used to rewrite the target method. + /// The ILProcessor that will be used to rewrite the target method. /// The original instructions from the target method body. - public void Rewrite(MethodDefinition method, CilWorker IL, IEnumerable oldInstructions) + public void Rewrite(MethodDefinition method, ILProcessor IL, IEnumerable oldInstructions) { TypeDefinition declaringType = method.DeclaringType; ModuleDefinition module = declaringType.Module; @@ -93,7 +93,7 @@ public virtual void AddLocals(MethodDefinition hostMethod) /// /// The instruction currently being evaluated. /// The method that contains the target instruction. - /// The CilWorker for the target method body. - protected abstract void Replace(Instruction oldInstruction, MethodDefinition hostMethod, CilWorker IL); + /// The ILProcessor for the target method body. + protected abstract void Replace(Instruction oldInstruction, MethodDefinition hostMethod, ILProcessor IL); } } \ No newline at end of file diff --git a/src/LinFu.AOP/MethodWeaver.cs b/src/LinFu.AOP/MethodWeaver.cs index 5d12996..3a4bc37 100644 --- a/src/LinFu.AOP/MethodWeaver.cs +++ b/src/LinFu.AOP/MethodWeaver.cs @@ -68,7 +68,7 @@ public bool ShouldWeave(MethodDefinition item) public void Weave(MethodDefinition method) { MethodBody body = method.Body; - CilWorker IL = body.CilWorker; + ILProcessor IL = body.GetILProcessor(); // Skip empty methods int instructionCount = body.Instructions.Count; @@ -113,7 +113,7 @@ public void ImportReferences(ModuleDefinition module) private void Rewrite(MethodDefinition method) { MethodBody body = method.Body; - CilWorker IL = body.CilWorker; + ILProcessor IL = body.GetILProcessor(); IEnumerable oldInstructions = _instructionProvider.GetInstructions(method); diff --git a/src/LinFu.AOP/NewOperatorInterception/ImplementActivatorHostWeaver.cs b/src/LinFu.AOP/NewOperatorInterception/ImplementActivatorHostWeaver.cs index ba9b94e..0a0db35 100644 --- a/src/LinFu.AOP/NewOperatorInterception/ImplementActivatorHostWeaver.cs +++ b/src/LinFu.AOP/NewOperatorInterception/ImplementActivatorHostWeaver.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Linq; using LinFu.AOP.Cecil.Interfaces; using LinFu.AOP.Interfaces; using LinFu.Reflection.Emit; @@ -40,13 +41,12 @@ public bool ShouldWeave(TypeDefinition item) public void Weave(TypeDefinition type) { - // Implement IActivatorHost only once - if (type.Interfaces.Contains(_hostInterfaceType)) + if (type.Interfaces.Any(typeReference => typeReference.FullName == _hostInterfaceType.FullName)) return; type.AddProperty("Activator", _activatorPropertyType); - if (!type.Interfaces.Contains(_hostInterfaceType)) + if (!type.Interfaces.Any(typeReference => typeReference.FullName == _hostInterfaceType.FullName)) type.Interfaces.Add(_hostInterfaceType); } diff --git a/src/LinFu.AOP/NewOperatorInterception/InterceptNewCalls.cs b/src/LinFu.AOP/NewOperatorInterception/InterceptNewCalls.cs index 532e1d3..b1dfebf 100644 --- a/src/LinFu.AOP/NewOperatorInterception/InterceptNewCalls.cs +++ b/src/LinFu.AOP/NewOperatorInterception/InterceptNewCalls.cs @@ -33,11 +33,11 @@ public override void ImportReferences(ModuleDefinition module) _emitter.ImportReferences(module); } - protected override void Replace(Instruction currentInstruction, MethodDefinition method, CilWorker IL) + protected override void Replace(Instruction currentInstruction, MethodDefinition method, ILProcessor IL) { var constructor = (MethodReference) currentInstruction.Operand; TypeReference concreteType = constructor.DeclaringType; - ParameterDefinitionCollection parameters = constructor.Parameters; + var parameters = constructor.Parameters; if (!_emitter.ShouldIntercept(constructor, concreteType, method)) { diff --git a/src/LinFu.AOP/NewOperatorInterception/RedirectNewInstancesToActivator.cs b/src/LinFu.AOP/NewOperatorInterception/RedirectNewInstancesToActivator.cs index 7494333..20dfbb3 100644 --- a/src/LinFu.AOP/NewOperatorInterception/RedirectNewInstancesToActivator.cs +++ b/src/LinFu.AOP/NewOperatorInterception/RedirectNewInstancesToActivator.cs @@ -1,226 +1,227 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using LinFu.AOP.Cecil.Extensions; -using LinFu.AOP.Cecil.Interfaces; -using LinFu.AOP.Interfaces; -using LinFu.Reflection.Emit; -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace LinFu.AOP.Cecil -{ - internal class RedirectNewInstancesToActivator : INewObjectWeaver - { - private readonly INewInstanceFilter _filter; - private MethodReference _addMethod; - private MethodReference _canActivate; - - private VariableDefinition _constructorArguments; - private MethodReference _createInstance; - private VariableDefinition _currentActivator; - private VariableDefinition _currentArgument; - private MethodReference _getItem; - private MethodReference _getStaticActivator; - private MethodReference _getTypeFromHandle; - private MethodReference _methodActivationContextCtor; - private VariableDefinition _methodContext; - private MethodReference _objectListCtor; - private MethodReference _reverseMethod; - private MethodReference _toArrayMethod; - - public RedirectNewInstancesToActivator(INewInstanceFilter filter) - { - _filter = filter; - } - - public RedirectNewInstancesToActivator(Func filter) - { - _filter = new NewInstanceInterceptionAdapter(filter); - } - - #region INewObjectWeaver Members - - public bool ShouldIntercept(MethodReference constructor, TypeReference concreteType, MethodReference hostMethod) - { - // Intercept all types by default - if (_filter == null) - return true; - - return _filter.ShouldWeave(constructor, concreteType, hostMethod); - } - - public void AddAdditionalMembers(TypeDefinition host) - { - // Make sure the type implements IActivatorHost - var interfaceWeaver = new ImplementActivatorHostWeaver(); +using System; +using System.Collections.Generic; +using System.Reflection; +using LinFu.AOP.Cecil.Extensions; +using LinFu.AOP.Cecil.Interfaces; +using LinFu.AOP.Interfaces; +using LinFu.Reflection.Emit; +using Mono.Cecil; +using Mono.Cecil.Cil; +using Mono.Collections.Generic; + +namespace LinFu.AOP.Cecil +{ + internal class RedirectNewInstancesToActivator : INewObjectWeaver + { + private readonly INewInstanceFilter _filter; + private MethodReference _addMethod; + private MethodReference _canActivate; + + private VariableDefinition _constructorArguments; + private MethodReference _createInstance; + private VariableDefinition _currentActivator; + private VariableDefinition _currentArgument; + private MethodReference _getItem; + private MethodReference _getStaticActivator; + private MethodReference _getTypeFromHandle; + private MethodReference _methodActivationContextCtor; + private VariableDefinition _methodContext; + private MethodReference _objectListCtor; + private MethodReference _reverseMethod; + private MethodReference _toArrayMethod; + + public RedirectNewInstancesToActivator(INewInstanceFilter filter) + { + _filter = filter; + } + + public RedirectNewInstancesToActivator(Func filter) + { + _filter = new NewInstanceInterceptionAdapter(filter); + } + + #region INewObjectWeaver Members + + public bool ShouldIntercept(MethodReference constructor, TypeReference concreteType, MethodReference hostMethod) + { + // Intercept all types by default + if (_filter == null) + return true; + + return _filter.ShouldWeave(constructor, concreteType, hostMethod); + } + + public void AddAdditionalMembers(TypeDefinition host) + { + // Make sure the type implements IActivatorHost + var interfaceWeaver = new ImplementActivatorHostWeaver(); host.Accept(interfaceWeaver); - } - - public void ImportReferences(ModuleDefinition module) - { - // Static method imports - _getStaticActivator = module.ImportMethod("GetActivator", typeof (TypeActivatorRegistry), - BindingFlags.Public | BindingFlags.Static); - _getTypeFromHandle = module.ImportMethod("GetTypeFromHandle", - BindingFlags.Public | BindingFlags.Static); - - // Constructor imports - _methodActivationContextCtor = module.ImportConstructor(typeof (object), - typeof (MethodBase), - typeof (Type), - typeof (object[])); - - // Instance method imports - _objectListCtor = module.ImportConstructor>(new Type[0]); - _toArrayMethod = module.ImportMethod>("ToArray", new Type[0]); - _addMethod = module.ImportMethod>("Add", new[] {typeof (object)}); - _reverseMethod = module.ImportMethod>("Reverse", new Type[0]); - _canActivate = module.ImportMethod("CanActivate"); - _getItem = module.ImportMethod>("get_Item", new[] {typeof (int)}); - - MethodInfo createInstanceMethod = typeof (IActivator).GetMethod("CreateInstance"); - - _createInstance = module.Import(createInstanceMethod); - } - - public void EmitNewObject(MethodDefinition hostMethod, CilWorker IL, MethodReference targetConstructor, - TypeReference concreteType) - { - ParameterDefinitionCollection parameters = targetConstructor.Parameters; - Instruction skipInterception = IL.Create(OpCodes.Nop); - - SaveConstructorArguments(IL, parameters); - EmitCreateMethodActivationContext(hostMethod, IL, concreteType); - - // Skip the interception if an activator cannot be found - EmitGetActivator(hostMethod, IL, skipInterception); - - IL.Emit(OpCodes.Stloc, _currentActivator); - - IL.Emit(OpCodes.Ldloc, _currentActivator); - IL.Emit(OpCodes.Brfalse, skipInterception); - - // Determine if the activator can instantiate the method from the - // current context - IL.Emit(OpCodes.Ldloc, _currentActivator); - IL.Emit(OpCodes.Ldloc, _methodContext); - IL.Emit(OpCodes.Callvirt, _canActivate); - IL.Emit(OpCodes.Brfalse, skipInterception); - - // Use the activator to create the object instance - EmitCreateInstance(IL); - - // } - Instruction endCreate = IL.Create(OpCodes.Nop); - IL.Emit(OpCodes.Br, endCreate); - // else { - IL.Append(skipInterception); - - // Restore the arguments that were popped off the stack - // by the list of constructor arguments - int parameterCount = parameters.Count; - for (int index = 0; index < parameterCount; index++) - { - ParameterDefinition currentParameter = parameters[index]; - - IL.Emit(OpCodes.Ldloc, _constructorArguments); - IL.Emit(OpCodes.Ldc_I4, index); - IL.Emit(OpCodes.Callvirt, _getItem); - IL.Emit(OpCodes.Unbox_Any, currentParameter.ParameterType); - } - - IL.Emit(OpCodes.Newobj, targetConstructor); - // } - - IL.Append(endCreate); - } - - public void AddLocals(MethodDefinition hostMethod) - { - _constructorArguments = hostMethod.AddLocal>(); - _currentArgument = hostMethod.AddLocal(); - _methodContext = hostMethod.AddLocal(); - _currentActivator = hostMethod.AddLocal(); - } - - #endregion - - private void EmitCreateInstance(CilWorker IL) - { - // T instance = this.Activator.CreateInstance(context); - IL.Emit(OpCodes.Ldloc, _currentActivator); - IL.Emit(OpCodes.Ldloc, _methodContext); - IL.Emit(OpCodes.Callvirt, _createInstance); - } - - private void EmitCreateMethodActivationContext(MethodDefinition method, CilWorker IL, TypeReference concreteType) - { - // TODO: Add static method support - Instruction pushThis = method.IsStatic ? IL.Create(OpCodes.Ldnull) : IL.Create(OpCodes.Ldarg_0); - - // Push the 'this' pointer onto the stack - IL.Append(pushThis); - - ModuleDefinition module = method.DeclaringType.Module; - - // Push the current method onto the stack - IL.PushMethod(method, module); - - // Push the concrete type onto the stack - IL.Emit(OpCodes.Ldtoken, concreteType); - IL.Emit(OpCodes.Call, _getTypeFromHandle); - - IL.Emit(OpCodes.Ldloc, _constructorArguments); - IL.Emit(OpCodes.Callvirt, _toArrayMethod); - IL.Emit(OpCodes.Newobj, _methodActivationContextCtor); - - // var context = new MethodActivationContext(this, currentMethod, concreteType, args); - IL.Emit(OpCodes.Stloc, _methodContext); - } - - private void SaveConstructorArguments(CilWorker IL, ParameterDefinitionCollection parameters) - { - int parameterCount = parameters.Count; - - IL.Emit(OpCodes.Newobj, _objectListCtor); - IL.Emit(OpCodes.Stloc, _constructorArguments); - - int index = parameterCount - 1; - while (index >= 0) - { - ParameterDefinition param = parameters[index]; - - SaveConstructorArgument(IL, param); - - index--; - } - - // Reverse the constructor arguments so that they appear in the correct order - IL.Emit(OpCodes.Ldloc, _constructorArguments); - IL.Emit(OpCodes.Callvirt, _reverseMethod); - } - - private void SaveConstructorArgument(CilWorker IL, ParameterDefinition param) - { - // Box the type if necessary - TypeReference parameterType = param.ParameterType; - if (parameterType.IsValueType || parameterType is GenericParameter) - IL.Emit(OpCodes.Box, parameterType); - - // Save the current argument - IL.Emit(OpCodes.Stloc, _currentArgument); - - // Add the item to the item to the collection - IL.Emit(OpCodes.Ldloc, _constructorArguments); - IL.Emit(OpCodes.Ldloc, _currentArgument); - IL.Emit(OpCodes.Callvirt, _addMethod); - } - - private void EmitGetActivator(MethodDefinition method, CilWorker IL, Instruction skipInterception) - { - IL.Emit(OpCodes.Ldloc, _methodContext); - IL.Emit(OpCodes.Call, _getStaticActivator); - } - } + } + + public void ImportReferences(ModuleDefinition module) + { + // Static method imports + _getStaticActivator = module.ImportMethod("GetActivator", typeof (TypeActivatorRegistry), + BindingFlags.Public | BindingFlags.Static); + _getTypeFromHandle = module.ImportMethod("GetTypeFromHandle", + BindingFlags.Public | BindingFlags.Static); + + // Constructor imports + _methodActivationContextCtor = module.ImportConstructor(typeof (object), + typeof (MethodBase), + typeof (Type), + typeof (object[])); + + // Instance method imports + _objectListCtor = module.ImportConstructor>(new Type[0]); + _toArrayMethod = module.ImportMethod>("ToArray", new Type[0]); + _addMethod = module.ImportMethod>("Add", new[] {typeof (object)}); + _reverseMethod = module.ImportMethod>("Reverse", new Type[0]); + _canActivate = module.ImportMethod("CanActivate"); + _getItem = module.ImportMethod>("get_Item", new[] {typeof (int)}); + + MethodInfo createInstanceMethod = typeof (IActivator).GetMethod("CreateInstance"); + + _createInstance = module.Import(createInstanceMethod); + } + + public void EmitNewObject(MethodDefinition hostMethod, ILProcessor IL, MethodReference targetConstructor, + TypeReference concreteType) + { + var parameters = targetConstructor.Parameters; + Instruction skipInterception = IL.Create(OpCodes.Nop); + + SaveConstructorArguments(IL, parameters); + EmitCreateMethodActivationContext(hostMethod, IL, concreteType); + + // Skip the interception if an activator cannot be found + EmitGetActivator(hostMethod, IL, skipInterception); + + IL.Emit(OpCodes.Stloc, _currentActivator); + + IL.Emit(OpCodes.Ldloc, _currentActivator); + IL.Emit(OpCodes.Brfalse, skipInterception); + + // Determine if the activator can instantiate the method from the + // current context + IL.Emit(OpCodes.Ldloc, _currentActivator); + IL.Emit(OpCodes.Ldloc, _methodContext); + IL.Emit(OpCodes.Callvirt, _canActivate); + IL.Emit(OpCodes.Brfalse, skipInterception); + + // Use the activator to create the object instance + EmitCreateInstance(IL); + + // } + Instruction endCreate = IL.Create(OpCodes.Nop); + IL.Emit(OpCodes.Br, endCreate); + // else { + IL.Append(skipInterception); + + // Restore the arguments that were popped off the stack + // by the list of constructor arguments + int parameterCount = parameters.Count; + for (int index = 0; index < parameterCount; index++) + { + ParameterDefinition currentParameter = parameters[index]; + + IL.Emit(OpCodes.Ldloc, _constructorArguments); + IL.Emit(OpCodes.Ldc_I4, index); + IL.Emit(OpCodes.Callvirt, _getItem); + IL.Emit(OpCodes.Unbox_Any, currentParameter.ParameterType); + } + + IL.Emit(OpCodes.Newobj, targetConstructor); + // } + + IL.Append(endCreate); + } + + public void AddLocals(MethodDefinition hostMethod) + { + _constructorArguments = hostMethod.AddLocal>(); + _currentArgument = hostMethod.AddLocal(); + _methodContext = hostMethod.AddLocal(); + _currentActivator = hostMethod.AddLocal(); + } + + #endregion + + private void EmitCreateInstance(ILProcessor IL) + { + // T instance = this.Activator.CreateInstance(context); + IL.Emit(OpCodes.Ldloc, _currentActivator); + IL.Emit(OpCodes.Ldloc, _methodContext); + IL.Emit(OpCodes.Callvirt, _createInstance); + } + + private void EmitCreateMethodActivationContext(MethodDefinition method, ILProcessor IL, TypeReference concreteType) + { + // TODO: Add static method support + Instruction pushThis = method.IsStatic ? IL.Create(OpCodes.Ldnull) : IL.Create(OpCodes.Ldarg_0); + + // Push the 'this' pointer onto the stack + IL.Append(pushThis); + + ModuleDefinition module = method.DeclaringType.Module; + + // Push the current method onto the stack + IL.PushMethod(method, module); + + // Push the concrete type onto the stack + IL.Emit(OpCodes.Ldtoken, concreteType); + IL.Emit(OpCodes.Call, _getTypeFromHandle); + + IL.Emit(OpCodes.Ldloc, _constructorArguments); + IL.Emit(OpCodes.Callvirt, _toArrayMethod); + IL.Emit(OpCodes.Newobj, _methodActivationContextCtor); + + // var context = new MethodActivationContext(this, currentMethod, concreteType, args); + IL.Emit(OpCodes.Stloc, _methodContext); + } + + private void SaveConstructorArguments(ILProcessor IL, Collection parameters) + { + int parameterCount = parameters.Count; + + IL.Emit(OpCodes.Newobj, _objectListCtor); + IL.Emit(OpCodes.Stloc, _constructorArguments); + + int index = parameterCount - 1; + while (index >= 0) + { + ParameterDefinition param = parameters[index]; + + SaveConstructorArgument(IL, param); + + index--; + } + + // Reverse the constructor arguments so that they appear in the correct order + IL.Emit(OpCodes.Ldloc, _constructorArguments); + IL.Emit(OpCodes.Callvirt, _reverseMethod); + } + + private void SaveConstructorArgument(ILProcessor IL, ParameterDefinition param) + { + // Box the type if necessary + TypeReference parameterType = param.ParameterType; + if (parameterType.IsValueType || parameterType is GenericParameter) + IL.Emit(OpCodes.Box, parameterType); + + // Save the current argument + IL.Emit(OpCodes.Stloc, _currentArgument); + + // Add the item to the item to the collection + IL.Emit(OpCodes.Ldloc, _constructorArguments); + IL.Emit(OpCodes.Ldloc, _currentArgument); + IL.Emit(OpCodes.Callvirt, _addMethod); + } + + private void EmitGetActivator(MethodDefinition method, ILProcessor IL, Instruction skipInterception) + { + IL.Emit(OpCodes.Ldloc, _methodContext); + IL.Emit(OpCodes.Call, _getStaticActivator); + } + } } \ No newline at end of file diff --git a/src/LinFu.Finders/LinFu.Finders.csproj b/src/LinFu.Finders/LinFu.Finders.csproj index 099f097..627c1ba 100644 --- a/src/LinFu.Finders/LinFu.Finders.csproj +++ b/src/LinFu.Finders/LinFu.Finders.csproj @@ -1,73 +1,74 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {020A9D4F-3C8C-48B5-830F-2EDAB07E0D97} - Library - Properties - LinFu.Finders - LinFu.Finders - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - ..\..\build\Debug\LinFu.Finders.XML - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - bin\Release\LinFu.Finders.XML - - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - Properties\CommonAssemblyInfo.cs - - - - - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {020A9D4F-3C8C-48B5-830F-2EDAB07E0D97} + Library + Properties + LinFu.Finders + LinFu.Finders + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + ..\..\build\Debug\LinFu.Finders.XML + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + bin\Release\LinFu.Finders.XML + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + + + --> \ No newline at end of file diff --git a/src/LinFu.IoC.Common/LinFu.IoC.Common.csproj b/src/LinFu.IoC.Common/LinFu.IoC.Common.csproj index a556329..cd84c7b 100644 --- a/src/LinFu.IoC.Common/LinFu.IoC.Common.csproj +++ b/src/LinFu.IoC.Common/LinFu.IoC.Common.csproj @@ -1,87 +1,88 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} - Library - Properties - LinFu.IoC.Common - LinFu.IoC.Common - v3.5 - 512 - true - LinFu.snk - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - ..\..\build\Debug\LinFu.IoC.Common.XML - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - - - - - - - - - - - - - - - - - {22EEB00F-F471-486C-A6AD-60F088821C78} - LinFu.Reflection - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} + Library + Properties + LinFu.IoC.Common + LinFu.IoC.Common + v4.0 + 512 + true + LinFu.snk + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + ..\..\build\Debug\LinFu.IoC.Common.XML + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + + + + + + + + + + + + + {22EEB00F-F471-486C-A6AD-60F088821C78} + LinFu.Reflection + + + + + + + --> \ No newline at end of file diff --git a/src/LinFu.IoC/LinFu.IoC.csproj b/src/LinFu.IoC/LinFu.IoC.csproj index 0c1828e..b3e9150 100644 --- a/src/LinFu.IoC/LinFu.IoC.csproj +++ b/src/LinFu.IoC/LinFu.IoC.csproj @@ -1,207 +1,208 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {FA3D5517-EFF4-4363-AFF2-EF67B981334E} - Library - Properties - LinFu.IoC - LinFu.IoC - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - ..\..\build\Debug\LinFu.IoC.XML - - - pdbonly - true - ..\..\build\Release\ - TRACE - prompt - 4 - ..\..\build\Release\LinFu.IoC.XML - - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - Properties\CommonAssemblyInfo.cs - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - LinFu.AOP.Interfaces - - - {020A9D4F-3C8C-48B5-830F-2EDAB07E0D97} - LinFu.Finders - - - {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} - LinFu.IoC.Common - - - {6C29A409-6148-49AC-A192-DC6F33F3304C} - LinFu.Proxy.Interfaces - - - {22EEB00F-F471-486C-A6AD-60F088821C78} - LinFu.Reflection - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {FA3D5517-EFF4-4363-AFF2-EF67B981334E} + Library + Properties + LinFu.IoC + LinFu.IoC + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + ..\..\build\Debug\LinFu.IoC.XML + + + pdbonly + true + ..\..\build\Release\ + TRACE + prompt + 4 + ..\..\build\Release\LinFu.IoC.XML + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + LinFu.AOP.Interfaces + + + {020A9D4F-3C8C-48B5-830F-2EDAB07E0D97} + LinFu.Finders + + + {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} + LinFu.IoC.Common + + + {6C29A409-6148-49AC-A192-DC6F33F3304C} + LinFu.Proxy.Interfaces + + + {22EEB00F-F471-486C-A6AD-60F088821C78} + LinFu.Reflection + + + + --> \ No newline at end of file diff --git a/src/LinFu.Proxy.Extensions/LinFu.Proxy.Extensions.csproj b/src/LinFu.Proxy.Extensions/LinFu.Proxy.Extensions.csproj index 308ab16..47554f0 100644 --- a/src/LinFu.Proxy.Extensions/LinFu.Proxy.Extensions.csproj +++ b/src/LinFu.Proxy.Extensions/LinFu.Proxy.Extensions.csproj @@ -1,90 +1,91 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {DC20B330-B410-481D-A192-D82A07D2C7EF} - Library - Properties - LinFu.Proxy.Extensions - LinFu.Proxy.Extensions - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - ..\..\build\Debug\LinFu.Proxy.Extensions.XML - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - - - - - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - LinFu.AOP.Interfaces - - - {613B6547-DCBB-4505-82B8-B4179BFC95CE} - LinFu.AOP.Cecil - - - {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} - LinFu.IoC.Common - - - {FA3D5517-EFF4-4363-AFF2-EF67B981334E} - LinFu.IoC - - - {6C29A409-6148-49AC-A192-DC6F33F3304C} - LinFu.Proxy.Interfaces - - - {54DA7856-9026-439A-8FFE-88BBF99577B8} - LinFu.Proxy - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {DC20B330-B410-481D-A192-D82A07D2C7EF} + Library + Properties + LinFu.Proxy.Extensions + LinFu.Proxy.Extensions + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + ..\..\build\Debug\LinFu.Proxy.Extensions.XML + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + LinFu.AOP.Interfaces + + + {613B6547-DCBB-4505-82B8-B4179BFC95CE} + LinFu.AOP.Cecil + + + {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} + LinFu.IoC.Common + + + {FA3D5517-EFF4-4363-AFF2-EF67B981334E} + LinFu.IoC + + + {6C29A409-6148-49AC-A192-DC6F33F3304C} + LinFu.Proxy.Interfaces + + + {54DA7856-9026-439A-8FFE-88BBF99577B8} + LinFu.Proxy + + + + --> \ No newline at end of file diff --git a/src/LinFu.Proxy.Interfaces/LinFu.Proxy.Interfaces.csproj b/src/LinFu.Proxy.Interfaces/LinFu.Proxy.Interfaces.csproj index d4a28a2..f3f189e 100644 --- a/src/LinFu.Proxy.Interfaces/LinFu.Proxy.Interfaces.csproj +++ b/src/LinFu.Proxy.Interfaces/LinFu.Proxy.Interfaces.csproj @@ -1,88 +1,89 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {6C29A409-6148-49AC-A192-DC6F33F3304C} - Library - Properties - LinFu.Proxy.Interfaces - LinFu.Proxy.Interfaces - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - ..\..\build\Debug\LinFu.Proxy.Interfaces.xml - - - pdbonly - true - ..\..\build\Release\ - TRACE - prompt - 4 - ..\..\build\Release\LinFu.Proxy.Interfaces.XML - - - - False - ..\..\lib\Mono.Cecil.dll - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - Properties\CommonAssemblyInfo.cs - - - - - - - - - - - - - - - - - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - LinFu.AOP.Interfaces - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {6C29A409-6148-49AC-A192-DC6F33F3304C} + Library + Properties + LinFu.Proxy.Interfaces + LinFu.Proxy.Interfaces + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + ..\..\build\Debug\LinFu.Proxy.Interfaces.xml + + + pdbonly + true + ..\..\build\Release\ + TRACE + prompt + 4 + ..\..\build\Release\LinFu.Proxy.Interfaces.XML + + + + False + ..\..\lib\Mono.Cecil.dll + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + + + + + + + + + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + LinFu.AOP.Interfaces + + + + --> \ No newline at end of file diff --git a/src/LinFu.Proxy/LinFu.Proxy.csproj b/src/LinFu.Proxy/LinFu.Proxy.csproj index 2a418b8..9501b6c 100644 --- a/src/LinFu.Proxy/LinFu.Proxy.csproj +++ b/src/LinFu.Proxy/LinFu.Proxy.csproj @@ -1,107 +1,108 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {54DA7856-9026-439A-8FFE-88BBF99577B8} - Library - Properties - LinFu.Proxy - LinFu.Proxy - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - ..\..\build\Debug\LinFu.Proxy.xml - - - pdbonly - true - ..\..\build\Release\ - TRACE - prompt - 4 - ..\..\build\Release\LinFu.Proxy.XML - - - - False - ..\..\lib\Mono.Cecil.dll - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - Properties\CommonAssemblyInfo.cs - - - - - - - - - - - - - - - - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - LinFu.AOP.Interfaces - - - {613B6547-DCBB-4505-82B8-B4179BFC95CE} - LinFu.AOP.Cecil - - - {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} - LinFu.IoC.Common - - - {6C29A409-6148-49AC-A192-DC6F33F3304C} - LinFu.Proxy.Interfaces - - - {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} - LinFu.Reflection.Emit - - - {22EEB00F-F471-486C-A6AD-60F088821C78} - LinFu.Reflection - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {54DA7856-9026-439A-8FFE-88BBF99577B8} + Library + Properties + LinFu.Proxy + LinFu.Proxy + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + ..\..\build\Debug\LinFu.Proxy.xml + + + pdbonly + true + ..\..\build\Release\ + TRACE + prompt + 4 + ..\..\build\Release\LinFu.Proxy.XML + + + + False + ..\..\lib\Mono.Cecil.dll + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + + + + + + + + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + LinFu.AOP.Interfaces + + + {613B6547-DCBB-4505-82B8-B4179BFC95CE} + LinFu.AOP.Cecil + + + {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} + LinFu.IoC.Common + + + {6C29A409-6148-49AC-A192-DC6F33F3304C} + LinFu.Proxy.Interfaces + + + {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} + LinFu.Reflection.Emit + + + {22EEB00F-F471-486C-A6AD-60F088821C78} + LinFu.Reflection + + + + --> \ No newline at end of file diff --git a/src/LinFu.Proxy/MethodBodyEmitter.cs b/src/LinFu.Proxy/MethodBodyEmitter.cs index 0ef7872..1b20438 100644 --- a/src/LinFu.Proxy/MethodBodyEmitter.cs +++ b/src/LinFu.Proxy/MethodBodyEmitter.cs @@ -74,7 +74,7 @@ public void Emit(MethodInfo originalMethod, MethodDefinition targetMethod) VariableDefinition arguments = targetMethod.AddLocal(); // if (!(this is IProxy)) - CilWorker IL = targetMethod.GetILGenerator(); + ILProcessor IL = targetMethod.GetILGenerator(); IL.Emit(OpCodes.Ldarg_0); IL.Emit(OpCodes.Isinst, proxyType); @@ -104,7 +104,7 @@ public void Emit(MethodInfo originalMethod, MethodDefinition targetMethod) // Determine the return type TypeReference returnType = targetMethod.ReturnType != null - ? targetMethod.ReturnType.ReturnType + ? targetMethod.ReturnType : voidType; IL.PackageReturnValue(module, returnType); @@ -127,10 +127,10 @@ public void Emit(MethodInfo originalMethod, MethodDefinition targetMethod) /// /// Emits the IL instructions to obtain an instance for the proxy type. /// - /// The responsible for emitting the method body. + /// The responsible for emitting the method body. /// The proxy type. /// The getter method for the interceptor. - protected virtual void EmitGetInterceptorInstruction(CilWorker IL, TypeReference proxyType, + protected virtual void EmitGetInterceptorInstruction(ILProcessor IL, TypeReference proxyType, MethodReference getInterceptorMethod) { IL.Emit(OpCodes.Ldarg_0); @@ -139,13 +139,13 @@ protected virtual void EmitGetInterceptorInstruction(CilWorker IL, TypeReference } /// - /// Causes the to make the method throw a + /// Causes the to make the method throw a /// if the method cannot be found. /// - /// The responsible for emitting the method body. - protected virtual void ImplementNotFound(CilWorker IL) + /// The responsible for emitting the method body. + protected virtual void ImplementNotFound(ILProcessor IL) { - MethodBody body = IL.GetBody(); + MethodBody body = IL.Body; TypeDefinition declaringType = body.Method.DeclaringType; ModuleDefinition module = declaringType.Module; @@ -160,14 +160,14 @@ protected virtual void ImplementNotFound(CilWorker IL) /// from the /// object. /// - /// The that will emit the method body. + /// The that will emit the method body. /// The parameters of the target method. /// The local variable that contains the instance. /// The local variable that will store the arguments from the instance. - private static void SaveRefArguments(CilWorker IL, IEnumerable parameters, + private static void SaveRefArguments(ILProcessor IL, IEnumerable parameters, VariableDefinition invocationInfo, VariableDefinition arguments) { - MethodBody body = IL.GetBody(); + MethodBody body = IL.Body; MethodDefinition targetMethod = body.Method; TypeDefinition declaringType = targetMethod.DeclaringType; ModuleDefinition module = declaringType.Module; @@ -196,7 +196,7 @@ private static void SaveRefArguments(CilWorker IL, IEnumerable types = null; diff --git a/src/LinFu.Proxy/ProxyImplementor.cs b/src/LinFu.Proxy/ProxyImplementor.cs index 3c1b68d..5cdec95 100644 --- a/src/LinFu.Proxy/ProxyImplementor.cs +++ b/src/LinFu.Proxy/ProxyImplementor.cs @@ -1,4 +1,5 @@ -using LinFu.AOP.Interfaces; +using System.Linq; +using LinFu.AOP.Interfaces; using LinFu.IoC.Configuration; using LinFu.Proxy.Interfaces; using LinFu.Reflection.Emit; @@ -29,7 +30,7 @@ public void Construct(ModuleDefinition module, TypeDefinition targetType) TypeReference interceptorType = module.Import(typeof (IInterceptor)); // Implement the IProxy interface only once - if (targetType.Interfaces.Contains(proxyInterfaceType)) + if (targetType.Interfaces.Any(typeReference => typeReference.FullName == proxyInterfaceType.FullName)) return; targetType.Interfaces.Add(proxyInterfaceType); diff --git a/src/LinFu.Proxy/SerializableProxyBuilder.cs b/src/LinFu.Proxy/SerializableProxyBuilder.cs index 099165a..2ad01b0 100644 --- a/src/LinFu.Proxy/SerializableProxyBuilder.cs +++ b/src/LinFu.Proxy/SerializableProxyBuilder.cs @@ -37,7 +37,7 @@ public override void Construct(Type originalBaseType, IEnumerable baseInte interfaces.Add(typeof (ISerializable)); TypeReference serializableInterfaceType = module.ImportType(); - if (!targetType.Interfaces.Contains(serializableInterfaceType)) + if (!targetType.Interfaces.Any(typeReference => typeReference.FullName == serializableInterfaceType.FullName)) targetType.Interfaces.Add(serializableInterfaceType); // Create the proxy type @@ -57,7 +57,7 @@ public override void Construct(Type originalBaseType, IEnumerable baseInte PropertyDefinition interceptorGetterProperty = (from PropertyDefinition m in targetType.Properties where m.Name == "Interceptor" && - m.PropertyType == interceptorType + m.PropertyType.FullName == interceptorType.FullName select m).First(); } @@ -80,7 +80,7 @@ private static void DefineSerializationConstructor(ModuleDefinition module, Type MethodBody body = serializationCtor.Body; body.InitLocals = true; - CilWorker IL = serializationCtor.GetILGenerator(); + ILProcessor IL = serializationCtor.GetILGenerator(); IL.Emit(OpCodes.Ldtoken, interceptorInterfaceType); IL.Emit(OpCodes.Call, getTypeFromHandle); @@ -116,7 +116,7 @@ where m.Name.Contains("ISerializable.GetObjectData") body.Instructions.Clear(); body.InitLocals = true; - CilWorker IL = getObjectDataMethod.GetILGenerator(); + ILProcessor IL = getObjectDataMethod.GetILGenerator(); TypeReference proxyInterfaceType = module.ImportType(); MethodReference getTypeFromHandle = module.ImportMethod("GetTypeFromHandle", diff --git a/src/LinFu.Reflection.Emit/AssemblyDefinitionExtensions.cs b/src/LinFu.Reflection.Emit/AssemblyDefinitionExtensions.cs index 966c825..6e90b3a 100644 --- a/src/LinFu.Reflection.Emit/AssemblyDefinitionExtensions.cs +++ b/src/LinFu.Reflection.Emit/AssemblyDefinitionExtensions.cs @@ -1,54 +1,55 @@ -using System.IO; -using System.Reflection; -using Mono.Cecil; - -namespace LinFu.Reflection.Emit -{ - /// - /// A class that adds extension methods to the - /// class. - /// - public static class AssemblyDefinitionExtensions - { - /// - /// Converts an - /// into a running . - /// - /// The to convert. - /// An that represents the instance. - /// - public static Assembly ToAssembly(this AssemblyDefinition definition) - { - Assembly result = null; - using (var stream = new MemoryStream()) - { - // Persist the assembly to the stream - AssemblyFactory.SaveAssembly(definition, stream); - byte[] buffer = stream.GetBuffer(); - result = Assembly.Load(buffer); - } - - return result; - } - - /// - /// Saves the assembly to disk. - /// - /// The target assembly definition. - /// The output file name. - public static void Save(this AssemblyDefinition definition, string filename) - { - AssemblyFactory.SaveAssembly(definition, filename); - } - - /// - /// Saves the assembly to disk. - /// - /// The target assembly definition. - /// The destination file stream. - public static void Save(this AssemblyDefinition definition, Stream outputStream) - { - AssemblyFactory.SaveAssembly(definition, outputStream); - } - } +using System.IO; +using System.Reflection; +using Mono.Cecil; + +namespace LinFu.Reflection.Emit +{ + /// + /// A class that adds extension methods to the + /// class. + /// + public static class AssemblyDefinitionExtensions + { + /// + /// Converts an + /// into a running . + /// + /// The to convert. + /// An that represents the instance. + /// + public static Assembly ToAssembly(this AssemblyDefinition definition) + { + Assembly result = null; + using (var stream = new MemoryStream()) + { + // Persist the assembly to the stream + definition.Write(stream); + byte[] buffer = stream.GetBuffer(); + result = Assembly.Load(buffer); + } + definition.Write(@"D:\tools\LinFu\source\LinFu\build\Debug\UnitTests\new.dll"); + + return result; + } + + /// + /// Saves the assembly to disk. + /// + /// The target assembly definition. + /// The output file name. + public static void Save(this AssemblyDefinition definition, string filename) + { + definition.Write(filename); + } + + /// + /// Saves the assembly to disk. + /// + /// The target assembly definition. + /// The destination file stream. + public static void Save(this AssemblyDefinition definition, Stream outputStream) + { + definition.Write(outputStream); + } + } } \ No newline at end of file diff --git a/src/LinFu.Reflection.Emit/CilWorkerExtensions.cs b/src/LinFu.Reflection.Emit/CilWorkerExtensions.cs index a00baeb..ddac674 100644 --- a/src/LinFu.Reflection.Emit/CilWorkerExtensions.cs +++ b/src/LinFu.Reflection.Emit/CilWorkerExtensions.cs @@ -9,7 +9,7 @@ namespace LinFu.Reflection.Emit { /// - /// A class that extends the class + /// A class that extends the class /// with helper methods that make it easier to save /// information about the method currently being implemented. /// @@ -37,13 +37,13 @@ static CilWorkerExtensions() } /// - /// Emits a Console.WriteLine call to using the current CilWorker that will only be called if the contents + /// Emits a Console.WriteLine call to using the current ILProcessor that will only be called if the contents /// of the target variable are null at runtime. /// - /// The target CilWorker. + /// The target ILProcessor. /// The text that will be written to the console. /// The target variable that will be checked for null at runtime. - public static void EmitWriteLineIfNull(this CilWorker IL, string text, VariableDefinition targetVariable) + public static void EmitWriteLineIfNull(this ILProcessor IL, string text, VariableDefinition targetVariable) { Instruction skipWrite = IL.Create(OpCodes.Nop); IL.Emit(OpCodes.Ldloc, targetVariable); @@ -53,13 +53,13 @@ public static void EmitWriteLineIfNull(this CilWorker IL, string text, VariableD } /// - /// Emits a Console.WriteLine call using the current CilWorker. + /// Emits a Console.WriteLine call using the current ILProcessor. /// - /// The target CilWorker. + /// The target ILProcessor. /// The text that will be written to the console. - public static void EmitWriteLine(this CilWorker IL, string text) + public static void EmitWriteLine(this ILProcessor IL, string text) { - MethodBody body = IL.GetBody(); + MethodBody body = IL.Body; MethodDefinition method = body.Method; TypeDefinition declaringType = method.DeclaringType; ModuleDefinition module = declaringType.Module; @@ -74,10 +74,10 @@ public static void EmitWriteLine(this CilWorker IL, string text) /// /// Pushes the current onto the stack. /// - /// The that will be used to create the instructions. + /// The that will be used to create the instructions. /// The method that represents the that will be pushed onto the stack. /// The module that contains the host method. - public static void PushMethod(this CilWorker IL, MethodReference method, ModuleDefinition module) + public static void PushMethod(this ILProcessor IL, MethodReference method, ModuleDefinition module) { MethodReference getMethodFromHandle = module.ImportMethod("GetMethodFromHandle", typeof (RuntimeMethodHandle), @@ -118,10 +118,10 @@ public static TypeReference GetDeclaringType(this MethodReference method) /// /// Pushes a instance onto the stack. /// - /// The that will be used to create the instructions. + /// The that will be used to create the instructions. /// The type that represents the that will be pushed onto the stack. /// The module that contains the host method. - public static void PushType(this CilWorker IL, TypeReference type, ModuleDefinition module) + public static void PushType(this ILProcessor IL, TypeReference type, ModuleDefinition module) { MethodReference getTypeFromHandle = module.ImportMethod("GetTypeFromHandle", typeof (RuntimeTypeHandle)); @@ -136,10 +136,10 @@ public static void PushType(this CilWorker IL, TypeReference type, ModuleDefinit /// /// Pushes the current onto the stack. /// - /// The that will be used to create the instructions. + /// The that will be used to create the instructions. /// The field that represents the that will be pushed onto the stack. /// The module that contains the target field. - public static void PushField(this CilWorker IL, FieldReference field, ModuleDefinition module) + public static void PushField(this ILProcessor IL, FieldReference field, ModuleDefinition module) { MethodReference getFieldFromHandle = module.ImportMethod("GetFieldFromHandle", typeof (RuntimeFieldHandle), @@ -155,11 +155,11 @@ public static void PushField(this CilWorker IL, FieldReference field, ModuleDefi /// /// Pushes the arguments of a method onto the stack. /// - /// The that will be used to create the instructions. + /// The that will be used to create the instructions. /// The module that contains the host method. /// The target method. /// The local variable that will hold the array of arguments. - public static void PushArguments(this CilWorker IL, IMethodSignature method, ModuleDefinition module, + public static void PushArguments(this ILProcessor IL, IMethodSignature method, ModuleDefinition module, VariableDefinition arguments) { TypeReference objectType = module.ImportType(typeof (object)); @@ -182,9 +182,9 @@ public static void PushArguments(this CilWorker IL, IMethodSignature method, Mod /// /// Pushes the stack trace of the currently executing method onto the stack. /// - /// The that will be used to create the instructions. + /// The that will be used to create the instructions. /// The module that contains the host method. - public static void PushStackTrace(this CilWorker IL, ModuleDefinition module) + public static void PushStackTrace(this ILProcessor IL, ModuleDefinition module) { ConstructorInfo stackTraceConstructor = typeof (StackTrace).GetConstructor(new[] {typeof (int), typeof (bool)}); @@ -199,18 +199,18 @@ public static void PushStackTrace(this CilWorker IL, ModuleDefinition module) /// /// Saves the generic type arguments that were used to construct the method. /// - /// The that will be used to create the instructions. + /// The that will be used to create the instructions. /// The target method whose generic type arguments (if any) will be saved into the local variable. /// The module that contains the host method. /// The local variable that will store the resulting array of objects. - public static void PushGenericArguments(this CilWorker IL, IGenericParameterProvider method, + public static void PushGenericArguments(this ILProcessor IL, IGenericParameterProvider method, ModuleDefinition module, VariableDefinition typeArguments) { MethodReference getTypeFromHandle = module.ImportMethod("GetTypeFromHandle", BindingFlags.Public | BindingFlags.Static); int genericParameterCount = method.GenericParameters.Count; - GenericParameterCollection genericParameters = method.GenericParameters; + var genericParameters = method.GenericParameters; for (int index = 0; index < genericParameterCount; index++) { GenericParameter current = genericParameters[index]; @@ -229,11 +229,11 @@ public static void PushGenericArguments(this CilWorker IL, IGenericParameterProv /// signature of methods with generic type parameters or methods that have /// parameters that are generic parameters specified by the type itself. /// - /// The that will be used to create the instructions. + /// The that will be used to create the instructions. /// The target method whose generic type arguments (if any) will be saved into the local variable . /// The module that contains the host method. /// The local variable that will store the current method signature. - public static void SaveParameterTypes(this CilWorker IL, MethodReference method, ModuleDefinition module, + public static void SaveParameterTypes(this ILProcessor IL, MethodReference method, ModuleDefinition module, VariableDefinition parameterTypes) { MethodReference getTypeFromHandle = module.ImportMethod("GetTypeFromHandle", @@ -254,13 +254,13 @@ public static void SaveParameterTypes(this CilWorker IL, MethodReference method, /// Converts the return value of a method into the target type. /// If the target type is void, then the value will simply be popped from the stack. /// - /// The that will be used to create the instructions. + /// The that will be used to create the instructions. /// The module that contains the host method. /// The method return type itself. - public static void PackageReturnValue(this CilWorker IL, ModuleDefinition module, TypeReference returnType) + public static void PackageReturnValue(this ILProcessor IL, ModuleDefinition module, TypeReference returnType) { TypeReference voidType = module.ImportType(typeof (void)); - if (returnType == voidType) + if (returnType.FullName == voidType.FullName) { IL.Emit(OpCodes.Pop); return; @@ -272,9 +272,9 @@ public static void PackageReturnValue(this CilWorker IL, ModuleDefinition module /// /// Emits the proper Stind (store indirect) IL instruction for the . /// - /// The target that will emit the IL. + /// The target that will emit the IL. /// The type of data being stored. - public static void Stind(this CilWorker IL, TypeReference currentType) + public static void Stind(this ILProcessor IL, TypeReference currentType) { string typeName = currentType.Name; OpCode opCode = OpCodes.Nop; @@ -290,11 +290,11 @@ public static void Stind(this CilWorker IL, TypeReference currentType) /// Stores the current parameter value /// into the array of method . /// - /// The that will be used to create the instructions. + /// The that will be used to create the instructions. /// The local variable that will store the method arguments. /// The array index that indicates where the parameter value will be stored in the array of arguments. /// The current argument value being stored. - private static void PushParameter(this CilWorker IL, int index, VariableDefinition arguments, + private static void PushParameter(this ILProcessor IL, int index, VariableDefinition arguments, ParameterDefinition param) { TypeReference parameterType = param.ParameterType; @@ -318,24 +318,24 @@ private static void PushParameter(this CilWorker IL, int index, VariableDefiniti } /// - /// Obtains the method definition that contains the current . + /// Obtains the method definition that contains the current . /// - /// The responsible for the method body. + /// The responsible for the method body. /// A method definition. - public static MethodDefinition GetMethod(this CilWorker IL) + public static MethodDefinition GetMethod(this ILProcessor IL) { - MethodBody body = IL.GetBody(); + MethodBody body = IL.Body; MethodDefinition targetMethod = body.Method; return targetMethod; } /// - /// Obtains the module that contains the current . + /// Obtains the module that contains the current . /// - /// The responsible for the method body. + /// The responsible for the method body. /// The host module. - public static ModuleDefinition GetModule(this CilWorker IL) + public static ModuleDefinition GetModule(this ILProcessor IL) { MethodDefinition method = IL.GetMethod(); TypeDefinition declaringType = method.DeclaringType; diff --git a/src/LinFu.Reflection.Emit/LinFu.Reflection.Emit.csproj b/src/LinFu.Reflection.Emit/LinFu.Reflection.Emit.csproj index 7299c6f..fb02adc 100644 --- a/src/LinFu.Reflection.Emit/LinFu.Reflection.Emit.csproj +++ b/src/LinFu.Reflection.Emit/LinFu.Reflection.Emit.csproj @@ -1,77 +1,81 @@ - - - - Debug - AnyCPU - 9.0.21022 - 2.0 - {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} - Library - Properties - LinFu.Reflection.Emit - LinFu.Reflection.Emit - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - ..\..\build\Debug\LinFu.Reflection.Emit.XML - - - pdbonly - true - ..\..\build\Release\ - TRACE - prompt - 4 - ..\..\build\Release\LinFu.Reflection.Emit.XML - - - - False - ..\..\lib\Mono.Cecil.dll - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - Properties\CommonAssemblyInfo.cs - - - - - - - - - - + + + + Debug + AnyCPU + 9.0.21022 + 2.0 + {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} + Library + Properties + LinFu.Reflection.Emit + LinFu.Reflection.Emit + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + ..\..\build\Debug\LinFu.Reflection.Emit.XML + + + pdbonly + true + ..\..\build\Release\ + TRACE + prompt + 4 + ..\..\build\Release\LinFu.Reflection.Emit.XML + + + + False + ..\..\lib\Mono.Cecil.dll + + + ..\..\lib\Mono.Cecil.Rocks.dll + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + + + --> \ No newline at end of file diff --git a/src/LinFu.Reflection.Emit/MethodDefinitionExtensions.cs b/src/LinFu.Reflection.Emit/MethodDefinitionExtensions.cs index 42700f0..4c551e0 100644 --- a/src/LinFu.Reflection.Emit/MethodDefinitionExtensions.cs +++ b/src/LinFu.Reflection.Emit/MethodDefinitionExtensions.cs @@ -14,14 +14,14 @@ namespace LinFu.Reflection.Emit public static class MethodDefinitionExtensions { /// - /// Returns the instance + /// Returns the instance /// associated with the body of the target method. /// /// The target method to be modified. - /// The instance that points to the instructions of the method body. - public static CilWorker GetILGenerator(this MethodDefinition method) + /// The instance that points to the instructions of the method body. + public static ILProcessor GetILGenerator(this MethodDefinition method) { - return method.Body.CilWorker; + return method.Body.GetILProcessor(); } /// @@ -70,9 +70,8 @@ public static VariableDefinition AddLocal(this MethodDefinition method, string v if (newLocal == null) { MethodBody body = method.Body; - int index = body.Variables.Count; - newLocal = new VariableDefinition(variableName, index, method, localType); + newLocal = new VariableDefinition(variableName, localType); body.Variables.Add(newLocal); } @@ -123,7 +122,7 @@ public static void SetReturnType(this MethodDefinition method, Type returnType) else actualReturnType = module.Import(returnType); - method.ReturnType.ReturnType = actualReturnType; + method.ReturnType = actualReturnType; } diff --git a/src/LinFu.Reflection.Emit/ModuleDefinitionExtensions.cs b/src/LinFu.Reflection.Emit/ModuleDefinitionExtensions.cs index f42e671..9d4b350 100644 --- a/src/LinFu.Reflection.Emit/ModuleDefinitionExtensions.cs +++ b/src/LinFu.Reflection.Emit/ModuleDefinitionExtensions.cs @@ -25,7 +25,7 @@ public static TypeDefinition DefineClass(this ModuleDefinition mainModule, string typeName, string namespaceName, TypeAttributes attributes, TypeReference baseType) { - var resultType = new TypeDefinition(typeName, namespaceName, + var resultType = new TypeDefinition(namespaceName, typeName, attributes, baseType); mainModule.Types.Add(resultType); diff --git a/src/LinFu.Reflection.Emit/ParameterDefinitionExtensions.cs b/src/LinFu.Reflection.Emit/ParameterDefinitionExtensions.cs index c3db8cb..97288cd 100644 --- a/src/LinFu.Reflection.Emit/ParameterDefinitionExtensions.cs +++ b/src/LinFu.Reflection.Emit/ParameterDefinitionExtensions.cs @@ -15,7 +15,7 @@ public static class ParameterDefinitionExtensions /// public static bool IsByRef(this ParameterDefinition parameter) { - return parameter.ParameterType != null && parameter.ParameterType is ReferenceType; + return parameter.ParameterType != null && parameter.ParameterType.IsByReference; } } } \ No newline at end of file diff --git a/src/LinFu.Reflection.Emit/TypeDefinitionExtensions.cs b/src/LinFu.Reflection.Emit/TypeDefinitionExtensions.cs index e721397..737c3b7 100644 --- a/src/LinFu.Reflection.Emit/TypeDefinitionExtensions.cs +++ b/src/LinFu.Reflection.Emit/TypeDefinitionExtensions.cs @@ -1,315 +1,315 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Mono.Cecil; -using Mono.Cecil.Cil; -using FieldAttributes = Mono.Cecil.FieldAttributes; -using MethodAttributes = Mono.Cecil.MethodAttributes; -using MethodImplAttributes = Mono.Cecil.MethodImplAttributes; -using PropertyAttributes = Mono.Cecil.PropertyAttributes; - -namespace LinFu.Reflection.Emit -{ - /// - /// A class that extends the - /// class with features similar to the features in the - /// System.Reflection.Emit namespace. - /// - public static class TypeDefinitionExtensions - { - /// - /// Adds a new method to the target type. - /// - /// The type that will hold the newly-created method. - /// The parameter that describes the characteristics of the method. - /// The name to be given to the new method. - /// The method return type. - /// The calling convention of the method being created. - /// The list of argument types that will be used to define the method signature. - /// A instance that represents the newly-created method. - public static MethodDefinition DefineMethod(this TypeDefinition typeDef, string methodName, - MethodAttributes attributes, - MethodCallingConvention callingConvention, Type returnType, - params Type[] parameterTypes) - { - var method = new MethodDefinition(methodName, attributes, null) - { - CallingConvention = callingConvention - }; - - typeDef.Methods.Add(method); - - // Match the parameter types - method.AddParameters(parameterTypes); - - // Match the return type - method.SetReturnType(returnType); - - return method; - } - - /// - /// Adds a new method to the target type. - /// - /// The type that will hold the newly-created method. - /// The parameter that describes the characteristics of the method. - /// The name to be given to the new method. - /// The method return type. - /// The list of argument types that will be used to define the method signature. - /// The list of generic argument types that will be used to define the method signature. - /// A instance that represents the newly-created method. - public static MethodDefinition DefineMethod(this TypeDefinition typeDef, string methodName, - MethodAttributes attributes, Type returnType, Type[] parameterTypes, - Type[] genericParameterTypes) - { - var method = new MethodDefinition(methodName, attributes, null); - - typeDef.Methods.Add(method); - - //Match the generic parameter types - foreach (Type genericParameterType in genericParameterTypes) - { - method.AddGenericParameter(genericParameterType); - } - - // Match the parameter types - method.AddParameters(parameterTypes); - - // Match the return type - method.SetReturnType(returnType); - - return method; - } - - /// - /// Adds a default constructor to the target type. - /// - /// The type that will contain the default constructor. - /// The default constructor. - public static MethodDefinition AddDefaultConstructor(this TypeDefinition targetType) - { - Type parentType = typeof (object); - - return AddDefaultConstructor(targetType, parentType); - } - - /// - /// Adds a default constructor to the target type. - /// - /// The base class that contains the default constructor that will be used for constructor chaining.. - /// The type that will contain the default constructor. - /// The default constructor. - public static MethodDefinition AddDefaultConstructor(this TypeDefinition targetType, Type parentType) - { - ModuleDefinition module = targetType.Module; - TypeReference voidType = module.Import(typeof (void)); - MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig - | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; - - - BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; - ConstructorInfo objectConstructor = parentType.GetConstructor(flags, null, new Type[0], null); - - // Revert to the System.Object constructor - // if the parent type does not have a default constructor - if (objectConstructor == null) - objectConstructor = typeof (object).GetConstructor(new Type[0]); - - MethodReference baseConstructor = module.Import(objectConstructor); - - // Define the default constructor - var ctor = new MethodDefinition(".ctor", methodAttributes, voidType) - { - CallingConvention = MethodCallingConvention.StdCall, - ImplAttributes = (MethodImplAttributes.IL | MethodImplAttributes.Managed) - }; - - CilWorker IL = ctor.Body.CilWorker; - - // Call the constructor for System.Object, and exit - IL.Emit(OpCodes.Ldarg_0); - IL.Emit(OpCodes.Call, baseConstructor); - IL.Emit(OpCodes.Ret); - - targetType.Constructors.Add(ctor); - - return ctor; - } - - /// - /// Adds a rewritable property to the target type. - /// - /// The target type that will hold the newly-created property. - /// The name of the property itself. - /// The instance that describes the property type. - public static void AddProperty(this TypeDefinition typeDef, string propertyName, Type propertyType) - { - ModuleDefinition module = typeDef.Module; - TypeReference typeRef = module.Import(propertyType); - typeDef.AddProperty(propertyName, typeRef); - } - - /// - /// Adds a rewritable property to the target type. - /// - /// The target type that will hold the newly-created property. - /// The name of the property itself. - /// The instance that describes the property type. - public static void AddProperty(this TypeDefinition typeDef, string propertyName, - TypeReference propertyType) - { - #region Add the backing field - - string fieldName = string.Format("__{0}_backingField", propertyName); - var actualField = new FieldDefinition(fieldName, - propertyType, FieldAttributes.Private); - - - typeDef.Fields.Add(actualField); - - #endregion - - FieldReference backingField = actualField; - if (typeDef.GenericParameters.Count > 0) - backingField = GetBackingField(fieldName, typeDef, propertyType); - - string getterName = string.Format("get_{0}", propertyName); - string setterName = string.Format("set_{0}", propertyName); - - - const MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig | - MethodAttributes.SpecialName | MethodAttributes.NewSlot | - MethodAttributes.Virtual; - - ModuleDefinition module = typeDef.Module; - TypeReference voidType = module.Import(typeof (void)); - - // Implement the getter and the setter - MethodDefinition getter = AddPropertyGetter(propertyType, getterName, attributes, backingField); - MethodDefinition setter = AddPropertySetter(propertyType, attributes, backingField, setterName, voidType); - - typeDef.AddProperty(propertyName, propertyType, getter, setter); - } - - /// - /// Adds a rewriteable property to the target type - /// using an existing and . - /// - /// The target type that will hold the newly-created property. - /// The name of the property itself. - /// The instance that describes the property type. - /// The property getter method. - /// The property setter method. - public static void AddProperty(this TypeDefinition typeDef, string propertyName, TypeReference propertyType, - MethodDefinition getter, MethodDefinition setter) - { - var newProperty = new PropertyDefinition(propertyName, - propertyType, PropertyAttributes.Unused) - { - GetMethod = getter, - SetMethod = setter - }; - - typeDef.Methods.Add(getter); - typeDef.Methods.Add(setter); - typeDef.Properties.Add(newProperty); - } - - /// - /// Retrieves the method that matches the given . - /// - /// The target type to search. - /// The name of the target method. - /// A method that matches the given . If the method is not found, then it will return a null value. - public static MethodDefinition GetMethod(this TypeDefinition typeDef, string methodName) - { - IEnumerable result = from MethodDefinition m in typeDef.Methods - where m.Name == methodName - select m; - - return result.FirstOrDefault(); - } - - /// - /// Resolves the backing field for a generic type declaration. - /// - /// The name of the field to reference. - /// The type that holds the actual field. - /// The that describes the property type being referenced. - /// A that points to the actual backing field. - private static FieldReference GetBackingField(string fieldName, TypeDefinition typeDef, - TypeReference propertyType) - { - // If the current type is a generic type, - // the current generic type must be resolved before - // using the actual field - var declaringType = new GenericInstanceType(typeDef); - foreach (GenericParameter parameter in typeDef.GenericParameters) - { - declaringType.GenericArguments.Add(parameter); - } - - return new FieldReference(fieldName, declaringType, propertyType); - ; - } - - /// - /// Creates a property getter method implementation with the - /// as the return type. - /// - /// Represents the return type for the getter method. - /// The getter method name. - /// The method attributes associated with the getter method. - /// The field that will store the instance that the getter method will retrieve. - /// A representing the getter method itself. - private static MethodDefinition AddPropertyGetter(TypeReference propertyType, - string getterName, MethodAttributes attributes, - FieldReference backingField) - { - var getter = new MethodDefinition(getterName, attributes, propertyType) - { - IsPublic = true, - ImplAttributes = (MethodImplAttributes.Managed | MethodImplAttributes.IL) - }; - - CilWorker IL = getter.GetILGenerator(); - IL.Emit(OpCodes.Ldarg_0); - IL.Emit(OpCodes.Ldfld, backingField); - IL.Emit(OpCodes.Ret); - - return getter; - } - - /// - /// Creates a property setter method implementation with the - /// as the setter parameter. - /// - /// Represents the parameter type for the setter method. - /// The method attributes associated with the setter method. - /// The field that will store the instance for the setter method. - /// The method name of the setter method. - /// The that represents . - /// A that represents the setter method itself. - private static MethodDefinition AddPropertySetter(TypeReference propertyType, MethodAttributes attributes, - FieldReference backingField, string setterName, - TypeReference voidType) - { - var setter = new MethodDefinition(setterName, attributes, voidType) - { - IsPublic = true, - ImplAttributes = (MethodImplAttributes.Managed | MethodImplAttributes.IL) - }; - - setter.Parameters.Add(new ParameterDefinition(propertyType)); - - CilWorker IL = setter.GetILGenerator(); - IL.Emit(OpCodes.Ldarg_0); - IL.Emit(OpCodes.Ldarg_1); - IL.Emit(OpCodes.Stfld, backingField); - IL.Emit(OpCodes.Ret); - - return setter; - } - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Mono.Cecil; +using Mono.Cecil.Cil; +using FieldAttributes = Mono.Cecil.FieldAttributes; +using MethodAttributes = Mono.Cecil.MethodAttributes; +using MethodImplAttributes = Mono.Cecil.MethodImplAttributes; +using PropertyAttributes = Mono.Cecil.PropertyAttributes; + +namespace LinFu.Reflection.Emit +{ + /// + /// A class that extends the + /// class with features similar to the features in the + /// System.Reflection.Emit namespace. + /// + public static class TypeDefinitionExtensions + { + /// + /// Adds a new method to the target type. + /// + /// The type that will hold the newly-created method. + /// The parameter that describes the characteristics of the method. + /// The name to be given to the new method. + /// The method return type. + /// The calling convention of the method being created. + /// The list of argument types that will be used to define the method signature. + /// A instance that represents the newly-created method. + public static MethodDefinition DefineMethod(this TypeDefinition typeDef, string methodName, + MethodAttributes attributes, + MethodCallingConvention callingConvention, Type returnType, + params Type[] parameterTypes) + { + var method = new MethodDefinition(methodName, attributes, null) + { + CallingConvention = callingConvention + }; + + typeDef.Methods.Add(method); + + // Match the parameter types + method.AddParameters(parameterTypes); + + // Match the return type + method.SetReturnType(returnType); + + return method; + } + + /// + /// Adds a new method to the target type. + /// + /// The type that will hold the newly-created method. + /// The parameter that describes the characteristics of the method. + /// The name to be given to the new method. + /// The method return type. + /// The list of argument types that will be used to define the method signature. + /// The list of generic argument types that will be used to define the method signature. + /// A instance that represents the newly-created method. + public static MethodDefinition DefineMethod(this TypeDefinition typeDef, string methodName, + MethodAttributes attributes, Type returnType, Type[] parameterTypes, + Type[] genericParameterTypes) + { + var method = new MethodDefinition(methodName, attributes, typeDef.Module.Import(typeof(void))); + + typeDef.Methods.Add(method); + + //Match the generic parameter types + foreach (Type genericParameterType in genericParameterTypes) + { + method.AddGenericParameter(genericParameterType); + } + + // Match the parameter types + method.AddParameters(parameterTypes); + + // Match the return type + method.SetReturnType(returnType); + + return method; + } + + /// + /// Adds a default constructor to the target type. + /// + /// The type that will contain the default constructor. + /// The default constructor. + public static MethodDefinition AddDefaultConstructor(this TypeDefinition targetType) + { + Type parentType = typeof (object); + + return AddDefaultConstructor(targetType, parentType); + } + + /// + /// Adds a default constructor to the target type. + /// + /// The base class that contains the default constructor that will be used for constructor chaining.. + /// The type that will contain the default constructor. + /// The default constructor. + public static MethodDefinition AddDefaultConstructor(this TypeDefinition targetType, Type parentType) + { + ModuleDefinition module = targetType.Module; + TypeReference voidType = module.Import(typeof (void)); + MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig + | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; + + + BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; + ConstructorInfo objectConstructor = parentType.GetConstructor(flags, null, new Type[0], null); + + // Revert to the System.Object constructor + // if the parent type does not have a default constructor + if (objectConstructor == null) + objectConstructor = typeof (object).GetConstructor(new Type[0]); + + MethodReference baseConstructor = module.Import(objectConstructor); + + // Define the default constructor + var ctor = new MethodDefinition(".ctor", methodAttributes, voidType) + { + //CallingConvention = MethodCallingConvention.StdCall, + ImplAttributes = (MethodImplAttributes.IL | MethodImplAttributes.Managed) + }; + + ILProcessor IL = ctor.Body.GetILProcessor(); + + // Call the constructor for System.Object, and exit + IL.Emit(OpCodes.Ldarg_0); + IL.Emit(OpCodes.Call, baseConstructor); + IL.Emit(OpCodes.Ret); + + targetType.Methods.Add(ctor); + + return ctor; + } + + /// + /// Adds a rewritable property to the target type. + /// + /// The target type that will hold the newly-created property. + /// The name of the property itself. + /// The instance that describes the property type. + public static void AddProperty(this TypeDefinition typeDef, string propertyName, Type propertyType) + { + ModuleDefinition module = typeDef.Module; + TypeReference typeRef = module.Import(propertyType); + typeDef.AddProperty(propertyName, typeRef); + } + + /// + /// Adds a rewritable property to the target type. + /// + /// The target type that will hold the newly-created property. + /// The name of the property itself. + /// The instance that describes the property type. + public static void AddProperty(this TypeDefinition typeDef, string propertyName, + TypeReference propertyType) + { + #region Add the backing field + + string fieldName = string.Format("__{0}_backingField", propertyName); + var actualField = new FieldDefinition(fieldName, + FieldAttributes.Private, propertyType); + + + typeDef.Fields.Add(actualField); + + #endregion + + FieldReference backingField = actualField; + if (typeDef.GenericParameters.Count > 0) + backingField = GetBackingField(fieldName, typeDef, propertyType); + + string getterName = string.Format("get_{0}", propertyName); + string setterName = string.Format("set_{0}", propertyName); + + + const MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig | + MethodAttributes.SpecialName | MethodAttributes.NewSlot | + MethodAttributes.Virtual; + + ModuleDefinition module = typeDef.Module; + TypeReference voidType = module.Import(typeof (void)); + + // Implement the getter and the setter + MethodDefinition getter = AddPropertyGetter(propertyType, getterName, attributes, backingField); + MethodDefinition setter = AddPropertySetter(propertyType, attributes, backingField, setterName, voidType); + + typeDef.AddProperty(propertyName, propertyType, getter, setter); + } + + /// + /// Adds a rewriteable property to the target type + /// using an existing and . + /// + /// The target type that will hold the newly-created property. + /// The name of the property itself. + /// The instance that describes the property type. + /// The property getter method. + /// The property setter method. + public static void AddProperty(this TypeDefinition typeDef, string propertyName, TypeReference propertyType, + MethodDefinition getter, MethodDefinition setter) + { + var newProperty = new PropertyDefinition(propertyName, + PropertyAttributes.Unused, propertyType) + { + GetMethod = getter, + SetMethod = setter + }; + + typeDef.Methods.Add(getter); + typeDef.Methods.Add(setter); + typeDef.Properties.Add(newProperty); + } + + /// + /// Retrieves the method that matches the given . + /// + /// The target type to search. + /// The name of the target method. + /// A method that matches the given . If the method is not found, then it will return a null value. + public static MethodDefinition GetMethod(this TypeDefinition typeDef, string methodName) + { + IEnumerable result = from MethodDefinition m in typeDef.Methods + where m.Name == methodName + select m; + + return result.FirstOrDefault(); + } + + /// + /// Resolves the backing field for a generic type declaration. + /// + /// The name of the field to reference. + /// The type that holds the actual field. + /// The that describes the property type being referenced. + /// A that points to the actual backing field. + private static FieldReference GetBackingField(string fieldName, TypeDefinition typeDef, + TypeReference propertyType) + { + // If the current type is a generic type, + // the current generic type must be resolved before + // using the actual field + var declaringType = new GenericInstanceType(typeDef); + foreach (GenericParameter parameter in typeDef.GenericParameters) + { + declaringType.GenericArguments.Add(parameter); + } + + return new FieldReference(fieldName, declaringType, propertyType); + ; + } + + /// + /// Creates a property getter method implementation with the + /// as the return type. + /// + /// Represents the return type for the getter method. + /// The getter method name. + /// The method attributes associated with the getter method. + /// The field that will store the instance that the getter method will retrieve. + /// A representing the getter method itself. + private static MethodDefinition AddPropertyGetter(TypeReference propertyType, + string getterName, MethodAttributes attributes, + FieldReference backingField) + { + var getter = new MethodDefinition(getterName, attributes, propertyType) + { + IsPublic = true, + ImplAttributes = (MethodImplAttributes.Managed | MethodImplAttributes.IL) + }; + + ILProcessor IL = getter.GetILGenerator(); + IL.Emit(OpCodes.Ldarg_0); + IL.Emit(OpCodes.Ldfld, backingField); + IL.Emit(OpCodes.Ret); + + return getter; + } + + /// + /// Creates a property setter method implementation with the + /// as the setter parameter. + /// + /// Represents the parameter type for the setter method. + /// The method attributes associated with the setter method. + /// The field that will store the instance for the setter method. + /// The method name of the setter method. + /// The that represents . + /// A that represents the setter method itself. + private static MethodDefinition AddPropertySetter(TypeReference propertyType, MethodAttributes attributes, + FieldReference backingField, string setterName, + TypeReference voidType) + { + var setter = new MethodDefinition(setterName, attributes, voidType) + { + IsPublic = true, + ImplAttributes = (MethodImplAttributes.Managed | MethodImplAttributes.IL) + }; + + setter.Parameters.Add(new ParameterDefinition(propertyType)); + + ILProcessor IL = setter.GetILGenerator(); + IL.Emit(OpCodes.Ldarg_0); + IL.Emit(OpCodes.Ldarg_1); + IL.Emit(OpCodes.Stfld, backingField); + IL.Emit(OpCodes.Ret); + + return setter; + } + } } \ No newline at end of file diff --git a/src/LinFu.Reflection/LinFu.Reflection.csproj b/src/LinFu.Reflection/LinFu.Reflection.csproj index 16fb4d6..3e7cfaa 100644 --- a/src/LinFu.Reflection/LinFu.Reflection.csproj +++ b/src/LinFu.Reflection/LinFu.Reflection.csproj @@ -1,93 +1,94 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {22EEB00F-F471-486C-A6AD-60F088821C78} - Library - Properties - LinFu.Reflection - LinFu.Reflection - v3.5 - 512 - true - LinFu.snk - - - 3.5 - - - - true - full - false - ..\..\build\Debug\ - DEBUG;TRACE - prompt - 4 - bin\Debug\LinFu.Reflection.XML - - - pdbonly - true - ..\..\build\Release\ - TRACE - prompt - 4 - ..\..\build\Release\LinFu.Reflection.XML - - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - Properties\CommonAssemblyInfo.cs - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {22EEB00F-F471-486C-A6AD-60F088821C78} + Library + Properties + LinFu.Reflection + LinFu.Reflection + v4.0 + 512 + true + LinFu.snk + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\ + DEBUG;TRACE + prompt + 4 + bin\Debug\LinFu.Reflection.XML + + + pdbonly + true + ..\..\build\Release\ + TRACE + prompt + 4 + ..\..\build\Release\LinFu.Reflection.XML + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + Properties\CommonAssemblyInfo.cs + + + + + + + + + + + + + + + + + + + + + + + + + + + + + --> \ No newline at end of file diff --git a/src/PostWeaver/PostWeaver.csproj b/src/PostWeaver/PostWeaver.csproj index a5bb455..f2fe2f9 100644 --- a/src/PostWeaver/PostWeaver.csproj +++ b/src/PostWeaver/PostWeaver.csproj @@ -1,85 +1,89 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {D616982C-D439-4B5C-B1A8-79BD4E7044FB} - Exe - Properties - PostWeaver - PostWeaver - v3.5 - 512 - - - 3.5 - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - False - ..\..\lib\Mono.Cecil.dll - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - - - - - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - LinFu.AOP.Interfaces - - - {613B6547-DCBB-4505-82B8-B4179BFC95CE} - LinFu.AOP.Cecil - - - {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} - LinFu.Reflection.Emit - - - {22EEB00F-F471-486C-A6AD-60F088821C78} - LinFu.Reflection - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {D616982C-D439-4B5C-B1A8-79BD4E7044FB} + Exe + Properties + PostWeaver + PostWeaver + v4.0 + 512 + + + 3.5 + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + ..\..\lib\Mono.Cecil.dll + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + LinFu.AOP.Interfaces + + + {613B6547-DCBB-4505-82B8-B4179BFC95CE} + LinFu.AOP.Cecil + + + {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} + LinFu.Reflection.Emit + + + {22EEB00F-F471-486C-A6AD-60F088821C78} + LinFu.Reflection + + + + + + + --> \ No newline at end of file diff --git a/src/PostWeaver/Program.cs b/src/PostWeaver/Program.cs index 9cb402b..8e2210f 100644 --- a/src/PostWeaver/Program.cs +++ b/src/PostWeaver/Program.cs @@ -25,18 +25,18 @@ private static void Main(string[] args) if (!File.Exists(inputFile)) throw new FileNotFoundException(inputFile); - string targetFile = inputFile; - AssemblyDefinition assembly = AssemblyFactory.GetAssembly(targetFile); + string targetFile = inputFile; + AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(targetFile); string targetDirectory = Path.GetDirectoryName(targetFile); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(targetFile); string pdbFileName = string.Format("{0}.pdb", filenameWithoutExtension); bool pdbExists = File.Exists(pdbFileName); - ModuleDefinition module = assembly.MainModule; - - if (pdbExists) - module.LoadSymbols(); + ModuleDefinition module = assembly.MainModule; + + if (pdbExists) + module.ReadSymbols(); InterceptMethodCalls(assembly, targetDirectory); InterceptNewInstances(assembly, targetDirectory); @@ -46,14 +46,14 @@ private static void Main(string[] args) // Update the PDB info if it exists if (pdbExists) - module.SaveSymbols(); + module.Write(targetFile); assembly.Save(targetFile); Console.WriteLine("PostWeaving Assembly '{0}' -> '{0}'", targetFile); } - private static void InterceptExceptions(IReflectionStructureVisitable assembly, string targetDirectory) + private static void InterceptExceptions(AssemblyDefinition assembly, string targetDirectory) { var methodFilter = LoadFirstInstanceOf(targetDirectory); @@ -66,7 +66,7 @@ private static void InterceptExceptions(IReflectionStructureVisitable assembly, assembly.InterceptAllExceptions(); } - private static void InterceptFields(IReflectionStructureVisitable assembly, string targetDirectory) + private static void InterceptFields(AssemblyDefinition assembly, string targetDirectory) { var fieldFilter = LoadFirstInstanceOf(targetDirectory); var hostTypeFilter = LoadFirstInstanceOf(targetDirectory); @@ -80,7 +80,7 @@ private static void InterceptFields(IReflectionStructureVisitable assembly, stri assembly.InterceptAllFields(); } - private static void InterceptMethodBodies(IReflectionStructureVisitable assembly, string targetDirectory) + private static void InterceptMethodBodies(AssemblyDefinition assembly, string targetDirectory) { var methodFilter = LoadFirstInstanceOf(targetDirectory); if (methodFilter != null) @@ -92,7 +92,7 @@ private static void InterceptMethodBodies(IReflectionStructureVisitable assembly assembly.InterceptAllMethodBodies(); } - private static void InterceptNewInstances(IReflectionStructureVisitable assembly, string targetDirectory) + private static void InterceptNewInstances(AssemblyDefinition assembly, string targetDirectory) { var newInstanceFilter = LoadFirstInstanceOf(targetDirectory); var methodFilter = LoadFirstInstanceOf(targetDirectory); @@ -106,7 +106,7 @@ private static void InterceptNewInstances(IReflectionStructureVisitable assembly assembly.InterceptAllNewInstances(); } - private static void InterceptMethodCalls(IReflectionStructureVisitable assembly, string sourceDirectory) + private static void InterceptMethodCalls(AssemblyDefinition assembly, string sourceDirectory) { var methodCallFilter = LoadFirstInstanceOf(sourceDirectory); var hostMethodFilter = LoadFirstInstanceOf(sourceDirectory); diff --git a/src/PostWeaver/app.config b/src/PostWeaver/app.config new file mode 100644 index 0000000..cb2586b --- /dev/null +++ b/src/PostWeaver/app.config @@ -0,0 +1,3 @@ + + + diff --git a/src/SampleFileWatcherLibrary/SampleFileWatcherLibrary.csproj b/src/SampleFileWatcherLibrary/SampleFileWatcherLibrary.csproj index 656b0c2..3440d56 100644 --- a/src/SampleFileWatcherLibrary/SampleFileWatcherLibrary.csproj +++ b/src/SampleFileWatcherLibrary/SampleFileWatcherLibrary.csproj @@ -1,77 +1,78 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {C93694B2-A776-4D91-8C1A-2A73B2479133} - Library - Properties - SampleFileWatcherLibrary - SampleFileWatcherLibrary - v3.5 - 512 - - - 3.5 - - - - true - full - false - ..\..\build\Debug\UnitTests\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - - - - - {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} - LinFu.IoC.Common - - - {FA3D5517-EFF4-4363-AFF2-EF67B981334E} - LinFu.IoC - - - {DEFBEF97-B03C-448F-9518-394735439E2E} - SampleLibrary - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {C93694B2-A776-4D91-8C1A-2A73B2479133} + Library + Properties + SampleFileWatcherLibrary + SampleFileWatcherLibrary + v4.0 + 512 + + + 3.5 + + + + + true + full + false + ..\..\build\Debug\UnitTests\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} + LinFu.IoC.Common + + + {FA3D5517-EFF4-4363-AFF2-EF67B981334E} + LinFu.IoC + + + {DEFBEF97-B03C-448F-9518-394735439E2E} + SampleLibrary + + + + --> \ No newline at end of file diff --git a/src/SampleLibrary/SampleLibrary.csproj b/src/SampleLibrary/SampleLibrary.csproj index a6a912f..e2b9426 100644 --- a/src/SampleLibrary/SampleLibrary.csproj +++ b/src/SampleLibrary/SampleLibrary.csproj @@ -1,163 +1,164 @@ - - - - Debug - AnyCPU - 9.0.21022 - 2.0 - {DEFBEF97-B03C-448F-9518-394735439E2E} - Library - Properties - SampleLibrary - SampleLibrary - v3.5 - 512 - - - 3.5 - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - LinFu.AOP.Interfaces - - - {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} - LinFu.IoC.Common - - - {FA3D5517-EFF4-4363-AFF2-EF67B981334E} - LinFu.IoC - - - {DC20B330-B410-481D-A192-D82A07D2C7EF} - LinFu.Proxy.Extensions - - - {22EEB00F-F471-486C-A6AD-60F088821C78} - LinFu.Reflection - - - + + + + Debug + AnyCPU + 9.0.21022 + 2.0 + {DEFBEF97-B03C-448F-9518-394735439E2E} + Library + Properties + SampleLibrary + SampleLibrary + v4.0 + 512 + + + 3.5 + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + LinFu.AOP.Interfaces + + + {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} + LinFu.IoC.Common + + + {FA3D5517-EFF4-4363-AFF2-EF67B981334E} + LinFu.IoC + + + {DC20B330-B410-481D-A192-D82A07D2C7EF} + LinFu.Proxy.Extensions + + + {22EEB00F-F471-486C-A6AD-60F088821C78} + LinFu.Reflection + + + + --> \ No newline at end of file diff --git a/src/SampleStronglyNamedLibrary/SampleStronglyNamedLibrary.csproj b/src/SampleStronglyNamedLibrary/SampleStronglyNamedLibrary.csproj index ea218ea..2e844e8 100644 --- a/src/SampleStronglyNamedLibrary/SampleStronglyNamedLibrary.csproj +++ b/src/SampleStronglyNamedLibrary/SampleStronglyNamedLibrary.csproj @@ -1,68 +1,69 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {9D2FF6CF-308E-4E31-A0D2-DE98355123CF} - Library - Properties - SampleStronglyNamedLibrary - SampleStronglyNamedLibrary - v3.5 - 512 - true - SampleKey.snk - - - 3.5 - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - 3.5 - - - 3.5 - - - 3.5 - - - - - - - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {9D2FF6CF-308E-4E31-A0D2-DE98355123CF} + Library + Properties + SampleStronglyNamedLibrary + SampleStronglyNamedLibrary + v4.0 + 512 + true + SampleKey.snk + + + 3.5 + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + + + --> \ No newline at end of file diff --git a/src/UnitTests/AOP/FieldInterceptionTests.cs b/src/UnitTests/AOP/FieldInterceptionTests.cs index 0a91263..5030b09 100644 --- a/src/UnitTests/AOP/FieldInterceptionTests.cs +++ b/src/UnitTests/AOP/FieldInterceptionTests.cs @@ -38,7 +38,7 @@ public object SetValue(IFieldInterceptionContext context, object value) [Test] public void ShouldSetAndGetTheSameFieldValue() { - AssemblyDefinition myLibrary = AssemblyFactory.GetAssembly("SampleLibrary.dll"); + AssemblyDefinition myLibrary = AssemblyDefinition.ReadAssembly("SampleLibrary.dll"); ModuleDefinition module = myLibrary.MainModule; foreach (TypeDefinition type in myLibrary.MainModule.Types) diff --git a/src/UnitTests/AOP/MethodBodyInterceptionTests.cs b/src/UnitTests/AOP/MethodBodyInterceptionTests.cs index f1b4e5b..3c44ae4 100644 --- a/src/UnitTests/AOP/MethodBodyInterceptionTests.cs +++ b/src/UnitTests/AOP/MethodBodyInterceptionTests.cs @@ -26,7 +26,7 @@ private void Test(Action testInstance) private void Test(string libraryFileName, string typeName, Func methodFilter, Action testTargetType) { - AssemblyDefinition assembly = AssemblyFactory.GetAssembly(libraryFileName); + AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(libraryFileName); ModuleDefinition module = assembly.MainModule; TypeDefinition targetType = (from TypeDefinition t in module.Types diff --git a/src/UnitTests/AOP/NewOperatorInterceptionTests.cs b/src/UnitTests/AOP/NewOperatorInterceptionTests.cs index fdcfb4c..49a9447 100644 --- a/src/UnitTests/AOP/NewOperatorInterceptionTests.cs +++ b/src/UnitTests/AOP/NewOperatorInterceptionTests.cs @@ -28,7 +28,7 @@ public void DoSomething() [Test] public void ShouldInterceptObjectInstantiation() { - AssemblyDefinition assembly = AssemblyFactory.GetAssembly("SampleLibrary.dll"); + AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly("SampleLibrary.dll"); ModuleDefinition module = assembly.MainModule; string typeName = "SampleClassWithNewInstanceCall"; diff --git a/src/UnitTests/AOP/ParameterDefinitionExtensionTests.cs b/src/UnitTests/AOP/ParameterDefinitionExtensionTests.cs index c118a79..b2f6c3f 100644 --- a/src/UnitTests/AOP/ParameterDefinitionExtensionTests.cs +++ b/src/UnitTests/AOP/ParameterDefinitionExtensionTests.cs @@ -12,10 +12,10 @@ public class ParameterDefinitionExtensionTests : BaseTestFixture public void ShouldBeAbleToDetermineIfMethodIsByRef() { string location = typeof (SampleClassWithByRefMethod).Assembly.Location; - AssemblyDefinition assembly = AssemblyFactory.GetAssembly(location); + AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(location); ModuleDefinition module = assembly.MainModule; - TypeDefinition targetType = module.GetType("SampleClassWithByRefMethod"); + TypeDefinition targetType = module.GetType("SampleLibrary.AOP.SampleClassWithByRefMethod"); MethodDefinition byRefMethod = targetType.GetMethod("ByRefMethod"); MethodDefinition regularMethod = targetType.GetMethod("NonByRefMethod"); diff --git a/src/UnitTests/AOP/ThirdPartyMethodCallInterceptionTests.cs b/src/UnitTests/AOP/ThirdPartyMethodCallInterceptionTests.cs index 96958d7..895651e 100644 --- a/src/UnitTests/AOP/ThirdPartyMethodCallInterceptionTests.cs +++ b/src/UnitTests/AOP/ThirdPartyMethodCallInterceptionTests.cs @@ -35,7 +35,7 @@ private Type GetModifiedTargetType() private Type GetModifiedTargetType(Action modify) { - AssemblyDefinition assembly = AssemblyFactory.GetAssembly("SampleLibrary.dll"); + AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly("SampleLibrary.dll"); ModuleDefinition module = assembly.MainModule; // Intercept all calls to the System.Console.WriteLine method from the DoSomething method diff --git a/src/UnitTests/App.config b/src/UnitTests/App.config index 2772b57..9db177e 100644 --- a/src/UnitTests/App.config +++ b/src/UnitTests/App.config @@ -1,8 +1,7 @@ - - - - - - - - \ No newline at end of file + + + + + + + diff --git a/src/UnitTests/Reflection/ReflectionEmitTests.cs b/src/UnitTests/Reflection/ReflectionEmitTests.cs index 0253f77..7733258 100644 --- a/src/UnitTests/Reflection/ReflectionEmitTests.cs +++ b/src/UnitTests/Reflection/ReflectionEmitTests.cs @@ -1,132 +1,134 @@ -using System; -using System.IO; -using System.Linq; -using System.Reflection; -using LinFu.AOP.Cecil; -using LinFu.IoC; -using LinFu.IoC.Configuration; -using LinFu.IoC.Configuration.Interfaces; -using LinFu.Reflection.Emit; -using Mono.Cecil; -using NUnit.Framework; -using SampleStronglyNamedLibrary; - -namespace LinFu.UnitTests.Reflection -{ - [TestFixture] - public class ReflectionEmitTests : BasePEVerifyTestCase - { - private Loader loader; - private ServiceContainer container; - private string filename; - - protected override void OnInit() - { - if (File.Exists(filename)) - File.Delete(filename); - - // Initialize the loader - // with all of the default LinFu plugins - loader = new Loader(); - container = new ServiceContainer(); - filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}.dll", Guid.NewGuid())); - AutoDelete(filename); - - loader.LoadDirectory(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll"); - loader.LoadInto(container); - } - - protected override void OnTerm() - { - if (!File.Exists(filename)) - return; - - PEVerify(filename); - } - - - [Test] - public void AssemblyDefinitionMustBeConvertibleToActualAssembly() - { - AssemblyDefinition definition = AssemblyFactory.DefineAssembly("testAssembly", AssemblyKind.Dll); - - Assembly assembly = definition.ToAssembly(); - Assert.IsTrue(assembly != null); - } - - [Test] - public void CecilShouldExtractSampleClassFromSignedAssembly() - { - string location = typeof (SampleHelloClass).Assembly.Location; - - AssemblyDefinition sourceAssembly = AssemblyFactory.GetAssembly(location); - Assert.IsNotNull(sourceAssembly); - - AssemblyDefinition definition = AssemblyFactory.DefineAssembly("testAssembly", AssemblyKind.Dll); - ModuleDefinition targetModule = definition.MainModule; - foreach (TypeDefinition typeDef in sourceAssembly.MainModule.Types) - { - // Copy the source type to the target assembly - targetModule.Inject(typeDef); - } - - // Convert the new assemblyDef into an actual assembly - Assembly assembly = definition.ToAssembly(); - Assert.IsNotNull(assembly); - - Type[] types = assembly.GetTypes(); - Assert.IsTrue(types.Length > 0); - - // The imported type must match the original type - Type firstType = types.FirstOrDefault(); - Assert.IsNotNull(firstType); - Assert.AreEqual(firstType.Name, typeof (SampleHelloClass).Name); - - object instance = Activator.CreateInstance(firstType); - Assert.IsNotNull(instance); - - MethodInfo speakMethod = firstType.GetMethod("Speak"); - Assert.IsNotNull(speakMethod); - - speakMethod.Invoke(instance, new object[] {}); - } - - [Test] - public void CecilShouldRemoveStrongNameFromAssembly() - { - string location = typeof (SampleHelloClass).Assembly.Location; - - AssemblyDefinition sourceAssembly = AssemblyFactory.GetAssembly(location); - - - Assert.IsNotNull(sourceAssembly); - sourceAssembly.RemoveStrongName(); - - Assembly assembly = sourceAssembly.ToAssembly(); - Assert.IsNotNull(assembly); - - AssemblyName assemblyName = assembly.GetName(); - - // The public key should be empty - byte[] bytes = assemblyName.GetPublicKey(); - Assert.IsTrue(bytes.Length == 0); - return; - } - - [Test] - public void MethodInvokerShouldProperlyHandleReturnValues() - { - MethodInfo targetMethod = typeof (object).GetMethod("GetHashCode"); - var instance = new object(); - - int hash = instance.GetHashCode(); - container.AddDefaultServices(); - - var invoker = container.GetService>(); - Assert.IsNotNull(invoker); - - object result = invoker.Invoke(instance, targetMethod, new object[] {}); - Assert.AreEqual(result, hash); - } - } +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using LinFu.AOP.Cecil; +using LinFu.IoC; +using LinFu.IoC.Configuration; +using LinFu.IoC.Configuration.Interfaces; +using LinFu.Reflection.Emit; +using Mono.Cecil; +using NUnit.Framework; +using SampleStronglyNamedLibrary; + +namespace LinFu.UnitTests.Reflection +{ + [TestFixture] + public class ReflectionEmitTests : BasePEVerifyTestCase + { + private Loader loader; + private ServiceContainer container; + private string filename; + + protected override void OnInit() + { + if (File.Exists(filename)) + File.Delete(filename); + + // Initialize the loader + // with all of the default LinFu plugins + loader = new Loader(); + container = new ServiceContainer(); + filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}.dll", Guid.NewGuid())); + AutoDelete(filename); + + loader.LoadDirectory(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll"); + loader.LoadInto(container); + } + + protected override void OnTerm() + { + if (!File.Exists(filename)) + return; + + PEVerify(filename); + } + + + [Test] + public void AssemblyDefinitionMustBeConvertibleToActualAssembly() + { + ModuleDefinition definition = ModuleDefinition.CreateModule("testAssembly", ModuleKind.Dll); + + Assembly assembly = definition.Assembly.ToAssembly(); + Assert.IsTrue(assembly != null); + } + + [Test] + public void CecilShouldExtractSampleClassFromSignedAssembly() + { + string location = typeof (SampleHelloClass).Assembly.Location; + + AssemblyDefinition sourceAssembly = AssemblyDefinition.ReadAssembly(location); + Assert.IsNotNull(sourceAssembly); + + ModuleDefinition targetModule = ModuleDefinition.CreateModule("testAssembly", ModuleKind.Dll); + AssemblyDefinition definition = targetModule.Assembly; + + foreach (TypeDefinition typeDef in sourceAssembly.MainModule.Types) + { + // Copy the source type to the target assembly + throw new NotImplementedException(); + //targetModule.Inject(typeDef); + } + + // Convert the new assemblyDef into an actual assembly + Assembly assembly = definition.ToAssembly(); + Assert.IsNotNull(assembly); + + Type[] types = assembly.GetTypes(); + Assert.IsTrue(types.Length > 0); + + // The imported type must match the original type + Type firstType = types.FirstOrDefault(); + Assert.IsNotNull(firstType); + Assert.AreEqual(firstType.Name, typeof (SampleHelloClass).Name); + + object instance = Activator.CreateInstance(firstType); + Assert.IsNotNull(instance); + + MethodInfo speakMethod = firstType.GetMethod("Speak"); + Assert.IsNotNull(speakMethod); + + speakMethod.Invoke(instance, new object[] {}); + } + + [Test] + public void CecilShouldRemoveStrongNameFromAssembly() + { + string location = typeof (SampleHelloClass).Assembly.Location; + + AssemblyDefinition sourceAssembly = AssemblyDefinition.ReadAssembly(location); + + + Assert.IsNotNull(sourceAssembly); + sourceAssembly.RemoveStrongName(); + + Assembly assembly = sourceAssembly.ToAssembly(); + Assert.IsNotNull(assembly); + + AssemblyName assemblyName = assembly.GetName(); + + // The public key should be empty + byte[] bytes = assemblyName.GetPublicKey(); + Assert.IsTrue(bytes.Length == 0); + return; + } + + [Test] + public void MethodInvokerShouldProperlyHandleReturnValues() + { + MethodInfo targetMethod = typeof (object).GetMethod("GetHashCode"); + var instance = new object(); + + int hash = instance.GetHashCode(); + container.AddDefaultServices(); + + var invoker = container.GetService>(); + Assert.IsNotNull(invoker); + + object result = invoker.Invoke(instance, targetMethod, new object[] {}); + Assert.AreEqual(result, hash); + } + } } \ No newline at end of file diff --git a/src/UnitTests/Tools/PEVerifier.cs b/src/UnitTests/Tools/PEVerifier.cs index 006ecb6..7925384 100644 --- a/src/UnitTests/Tools/PEVerifier.cs +++ b/src/UnitTests/Tools/PEVerifier.cs @@ -28,7 +28,7 @@ internal PEVerifier(string filename) public void Verify(AssemblyDefinition assembly) { // Save the assembly to the temporary file and verify it - AssemblyFactory.SaveAssembly(assembly, location); + assembly.Write(location); PEVerify(location); } diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index 2d9106b..b9d2484 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -1,179 +1,180 @@ - - - - Debug - AnyCPU - 9.0.30729 - 9.0.21022 - 2.0 - {E3B1686C-DCB3-44E7-BB25-9483592EA942} - Library - Properties - LinFu.UnitTests - LinFu.UnitTests - v3.5 - 512 - - - 3.5 - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - true - full - false - ..\..\build\Debug\UnitTests\ - DEBUG;TRACE - prompt - 4 - - - - False - ..\..\lib\Mono.Cecil.dll - - - False - ..\..\lib\Moq.dll - - - False - ..\..\lib\NUnit.framework.dll - - - - - 3.5 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} - LinFu.AOP.Interfaces - - - {613B6547-DCBB-4505-82B8-B4179BFC95CE} - LinFu.AOP.Cecil - - - {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} - LinFu.IoC.Common - - - {DC20B330-B410-481D-A192-D82A07D2C7EF} - LinFu.Proxy.Extensions - - - {6C29A409-6148-49AC-A192-DC6F33F3304C} - LinFu.Proxy.Interfaces - - - {54DA7856-9026-439A-8FFE-88BBF99577B8} - LinFu.Proxy - - - {020A9D4F-3C8C-48B5-830F-2EDAB07E0D97} - LinFu.Finders - - - {FA3D5517-EFF4-4363-AFF2-EF67B981334E} - LinFu.IoC - - - {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} - LinFu.Reflection.Emit - - - {22EEB00F-F471-486C-A6AD-60F088821C78} - LinFu.Reflection - - - {C93694B2-A776-4D91-8C1A-2A73B2479133} - SampleFileWatcherLibrary - - - {DEFBEF97-B03C-448F-9518-394735439E2E} - SampleLibrary - - - {9D2FF6CF-308E-4E31-A0D2-DE98355123CF} - SampleStronglyNamedLibrary - - - - - - + + + + Debug + AnyCPU + 9.0.30729 + 9.0.21022 + 2.0 + {E3B1686C-DCB3-44E7-BB25-9483592EA942} + Library + Properties + LinFu.UnitTests + LinFu.UnitTests + v4.0 + 512 + + + 3.5 + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + full + false + ..\..\build\Debug\UnitTests\ + DEBUG;TRACE + prompt + 4 + + + + False + ..\..\lib\Mono.Cecil.dll + + + False + ..\..\lib\Moq.dll + + + False + ..\..\lib\NUnit.framework.dll + + + + + 3.5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {0F8C48B0-4AE9-4429-AB90-C5141D710C2C} + LinFu.AOP.Interfaces + + + {613B6547-DCBB-4505-82B8-B4179BFC95CE} + LinFu.AOP.Cecil + + + {D027A765-4D2E-48AE-9D83-C5F5AFA7D8C1} + LinFu.IoC.Common + + + {DC20B330-B410-481D-A192-D82A07D2C7EF} + LinFu.Proxy.Extensions + + + {6C29A409-6148-49AC-A192-DC6F33F3304C} + LinFu.Proxy.Interfaces + + + {54DA7856-9026-439A-8FFE-88BBF99577B8} + LinFu.Proxy + + + {020A9D4F-3C8C-48B5-830F-2EDAB07E0D97} + LinFu.Finders + + + {FA3D5517-EFF4-4363-AFF2-EF67B981334E} + LinFu.IoC + + + {22B3D63C-29E9-49D3-86CB-28FF7D2C70E7} + LinFu.Reflection.Emit + + + {22EEB00F-F471-486C-A6AD-60F088821C78} + LinFu.Reflection + + + {C93694B2-A776-4D91-8C1A-2A73B2479133} + SampleFileWatcherLibrary + + + {DEFBEF97-B03C-448F-9518-394735439E2E} + SampleLibrary + + + {9D2FF6CF-308E-4E31-A0D2-DE98355123CF} + SampleStronglyNamedLibrary + + + + + + + --> \ No newline at end of file