{"comment":{"created_at":"2011-10-27T03:15:48Z","body":"Possibly. Specifically, what are you trying to do with the target DLL?\nPerhaps there's another way around it.\n\nOn Thu, Oct 27, 2011 at 2:29 AM, Eden <\nreply@reply.github.com>wrote:\n\n> I ran debugged PostWeaver and it doesn't appear to run again the assembly\n> or types twice. What I decided to do was run PostWeaver against\n> SampleLibrary.dll in the tests folder and I discovered that PEVerify also\n> throws up a lot of errors about that too. The errors I get look like this:\n>\n> <pre>\n> [IL]: Error: [SampleLibrary.dll :\n> SampleLibrary.SecondSingletonService::get_FieldInterceptor][offset\n> 0x0000007F] Unable to resolve token.\n> [IL]: Error: [SampleLibrary.dll :\n> SampleLibrary.SecondSingletonService::set_FieldInterceptor][offset\n> 0x0000009E] Unable to resolve token.\n> [IL]: Error: [SampleLibrary.dll :\n> SampleLibrary.SecondSingletonService::.ctor][offset 0x00000088] Unable to\n> resolve token.\n> 1155 Error(s) Verifying SampleLibrary.dll\n> </pre>\n>\n> Is it possible that my thinking/approach here is simply wrong?\n>\n> --\n> Reply to this email directly or view it on GitHub:\n> https://github.com/philiplaureano/LinFu/issues/18#issuecomment-2531704\n>","updated_at":"2011-10-27T03:15:48Z","url":"https://api.github.com/repos/philiplaureano/LinFu/issues/comments/2539258","id":2539258,"user":{"avatar_url":"https://secure.gravatar.com/avatar/b817dfce17e304f3c6da3e96a64d13fc?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b817dfce17e304f3c6da3e96a64d13fc","url":"https://api.github.com/users/philiplaureano","id":137219,"login":"philiplaureano"}},"action":"created","issue":{"number":18,"created_at":"2011-10-25T14:47:39Z","pull_request":{"diff_url":null,"patch_url":null,"html_url":null},"body":"I just wanted to start off by thanking you for all the hard work that has gone into this. I think this is a great project with a lot of potential. The ability to unlock, fix, extend, etc. in third party libraries is awesome.\r\n\r\nI am trying to intercept calls to a Microsoft library (Microsoft.Data.Schema.ScriptDom) to change the manner in which SQL is generated for SQL Azure. I have added a PostWeaveTask to intercept all methods, but this is having an undesirable side effect in the assembly that I am implementing the IMethodReplacementProvider and IInterceptor interfaces to replace the logic. Essentially the post weave is generating invalid IL in some method bodies preventing them from being run in a unit test or decompiled in ILSpy.\r\n\r\nIn my MethodReplacementProvider the CanReplace method is invalid when I uncomment the commented out lines below:\r\n\r\n        public bool CanReplace(object host, IInvocationInfo info)\r\n        {\r\n            var declaringType = info.TargetMethod.DeclaringType;\r\n            if (!declaringType.Equals(_targetType)) return false;\r\n\r\n            //bool isCorrectMethod = (info.TargetMethod.Name.Equals(\"ExplicitVisit\"));\r\n            //if (!isCorrectMethod) return false;\r\n\r\n            //object objectBeingVisited = info.Arguments[0];\r\n            //return (objectBeingVisited is RaiseErrorLegacyStatement);\r\n\r\n            return false;\r\n        }\r\n\r\nSo specifically if I just uncomment the bool isCorrectMethod line, it generates invalid IL. In another class, the following method also has problems:\r\n\r\n        public void FixForAzure(string inputSql, out string outputSql, out IList<ParseError> errors)\r\n        {\r\n            using (var sqlReader = new StringReader(inputSql))\r\n            using (var sqlWriter = new StringWriter())\r\n            {\r\n                FixForAzure(sqlReader, sqlWriter, out errors);\r\n                outputSql = sqlWriter.ToString();\r\n            }\r\n        }\r\n\r\nI tried to take Dénes Magyari's updated version and unfortunately that has not solved the problem either. I am wondering if there may be a work around by changing the PostWeaveTask to focus on 1 assembly. There would however appear to be an issue with the IL weave that leaves problems with some methods. Note that I am using .Net 4.0 as the MS libraries I'm using require them.\r\n\r\nI ran PEVerify on the altered DLL and this is the output:\r\n\r\n<pre style=\"font-size:8px\">\r\n\r\nMicrosoft (R) .NET Framework PE Verifier.  Version  4.0.30319.1\r\nCopyright (c) Microsoft Corporation.  All rights reserved.\r\n\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.AzureConverter::.ctor][offset 0x0000001F][found <uninitialized> ref ('this' ptr) 'SqlAzureConverter.Common.AzureConverter'] Uninitialized item on stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.AzureConverter::Parse][offset 0x0000001B][found address of ref ][expected ref 'System.Object'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.AzureConverter::Parse][offset 0x00000252][found ref 'System.Collections.Generic.IList`1[Microsoft.Data.Schema.ScriptDom.ParseError]'][expected address of ref ] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.AzureConverter::Parse][offset 0x00000252][found ref 'System.Object'][expected ref 'Microsoft.Data.Schema.ScriptDom.Parser'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.AzureConverter::FixForAzure][offset 0x00000016][found address of ref ][expected ref 'System.Object'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.AzureConverter::FixForAzure][offset 0x000004F5][found ref 'System.Object'][expected ref 'Microsoft.Data.Schema.ScriptDom.ScriptGenerator'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.AzureConverter::FixForAzure][offset 0x0000024D][found ref 'System.Collections.Generic.IList`1[Microsoft.Data.Schema.ScriptDom.ParseError]'][expected address of ref ] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.AzureConverter::FixForAzure][offset 0x0000024D][found ref 'System.Object'][expected ref 'SqlAzureConverter.Common.AzureConverter'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.AzureConverter::FixForAzure][offset 0x0000076E] jmp / exception into the middle of an instruction.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.RaiserrorConverter::.ctor][offset 0x00000013][found <uninitialized> ref ('this' ptr) 'SqlAzureConverter.Common.RaiserrorConverter'] Uninitialized item on stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.RaiseErrorLegacyStatementInterceptor::Intercept][offset 0x00001266][found ref 'System.Object'][expected ref 'System.Reflection.MethodBase'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.RaiseErrorLegacyStatementInterceptor::Intercept][offset 0x00000D23][found ref 'System.Object'][expected ref 'System.Reflection.MemberInfo'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.RaiseErrorLegacyStatementInterceptor::Intercept][offset 0x00000ADF][found ref 'System.Object'][expected ref 'LinFu.AOP.Interfaces.IInvocationInfo'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.RaiseErrorLegacyStatementInterceptor::Intercept][offset 0x000008A0][found ref 'System.Object'][expected ref 'LinFu.AOP.Interfaces.IInvocationInfo'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.RaiseErrorLegacyStatementInterceptor::Intercept][offset 0x00000661][found ref 'System.Object'][expected ref 'LinFu.AOP.Interfaces.IInvocationInfo'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.RaiseErrorLegacyStatementInterceptor::Intercept][offset 0x00000422][found ref 'System.Object'][expected ref 'System.Reflection.MemberInfo'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.RaiseErrorLegacyStatementInterceptor::Intercept][offset 0x000001E5][found ref 'System.Object'][expected ref 'LinFu.AOP.Interfaces.IInvocationInfo'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.RaiseErrorLegacyStatementInterceptor::.ctor][offset 0x00000013][found <uninitialized> ref ('this' ptr) 'SqlAzureConverter.Common.Replacements.RaiseErrorLegacyStatementInterceptor'] Uninitialized item on stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.SqlAzureScriptGeneratorVisitorProvider::CanReplace][offset 0x0000069A][found ref 'System.Object'][expected ref 'System.Type'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.SqlAzureScriptGeneratorVisitorProvider::CanReplace][offset 0x00000422][found ref 'System.Object'][expected ref 'System.Reflection.MemberInfo'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.SqlAzureScriptGeneratorVisitorProvider::CanReplace][offset 0x000001E5][found ref 'System.Object'][expected ref 'LinFu.AOP.Interfaces.IInvocationInfo'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.SqlAzureScriptGeneratorVisitorProvider::GetMethodReplacement][offset 0x00000454][found ref 'System.Object'][expected ref 'LinFu.AOP.Interfaces.IInvocationInfo'] Unexpected type on the stack.\r\n[IL]: Error: [SqlAzureConverter.Common.dll : SqlAzureConverter.Common.Replacements.SqlAzureScriptGeneratorVisitorProvider::.ctor][offset 0x00000013][found <uninitialized> ref ('this' ptr) 'SqlAzureConverter.Common.Replacements.SqlAzureScriptGeneratorVisitorProvider'] Uninitialized item on stack.\r\n23 Error(s) Verifying SqlAzureConverter.Common.dll\r\n\r\n</pre>","comments":5,"title":"System.InvalidProgramException - Method call replacement generating invalid IL","updated_at":"2011-10-27T03:15:48Z","url":"https://api.github.com/repos/philiplaureano/LinFu/issues/18","id":2045700,"assignee":null,"milestone":null,"closed_at":null,"user":{"avatar_url":"https://secure.gravatar.com/avatar/67206992659198aaab5d55ea4fdb54ed?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"67206992659198aaab5d55ea4fdb54ed","url":"https://api.github.com/users/EdenRidgway","id":308398,"login":"EdenRidgway"},"html_url":"https://github.com/philiplaureano/LinFu/issues/18","labels":[],"state":"open"},"legacy":{"issue_id":2045700,"comment_id":2539258}}