philiplaureano/LinFu — Issue #18: System.InvalidProgramException - Method call replacement generating invalid IL
Author: EdenRidgway
Created: 2011-10-25 14:47:39+00:00
State: open

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.

I 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.

In my MethodReplacementProvider the CanReplace method is invalid when I uncomment the commented out lines below:

public bool CanReplace(object host, IInvocationInfo info)
{
var declaringType = info.TargetMethod.DeclaringType;
if (!declaringType.Equals(_targetType)) return false;

//bool isCorrectMethod = (info.TargetMethod.Name.Equals("ExplicitVisit"));
//if (!isCorrectMethod) return false;

//object objectBeingVisited = info.Arguments[0];
//return (objectBeingVisited is RaiseErrorLegacyStatement);

return false;
}

So specifically if I just uncomment the bool isCorrectMethod line, it generates invalid IL. In another class, the following method also has problems:

public void FixForAzure(string inputSql, out string outputSql, out IList errors)
{
using (var sqlReader = new StringReader(inputSql))
using (var sqlWriter = new StringWriter())
{
FixForAzure(sqlReader, sqlWriter, out errors);
outputSql = sqlWriter.ToString();
}
}

I 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.