Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;
using LinFu.AOP.Interfaces;
using LinFu.AOP.Cecil;
namespace CSLInjectionDemo
{
@philiplaureano
philiplaureano / gist:954549
Created May 4, 2011 00:45
A sample CSProj file modification for dynamically intercepting thrown exceptions with LinFu.AOP
<PropertyGroup>
<PostWeaveTaskLocation>$(MSBuildProjectDirectory)\$(OutputPath)\..\..\..\lib\LinFu.Core.dll</PostWeaveTaskLocation>
</PropertyGroup>
<UsingTask TaskName="PostWeaveTask" AssemblyFile="$(PostWeaveTaskLocation)" />
<Target Name="AfterBuild">
<PostWeaveTask TargetFile="$(MSBuildProjectDirectory)\$(OutputPath)$(MSBuildProjectName).dll" InterceptAllExceptions="true" />
</Target>
public class SampleExceptionHandler : IExceptionHandler
{
public bool CanCatch(IExceptionHandlerInfo exceptionHandlerInfo)
{
return true;
}
public void Catch(IExceptionHandlerInfo exceptionHandlerInfo)
{
var exception = exceptionHandlerInfo.Exception;
public interface IExceptionHandlerInfo
{
Exception Exception { get; }
IInvocationInfo InvocationInfo { get; }
object ReturnValue { get; set; }
bool ShouldSkipRethrow { get; set; }
}
protected override void ModifyTargetMethod(MethodDefinition targetMethod)
{
// In this example, we are going to redirect all Console.WriteLine calls
// to the FakeConsole.WriteLine() method
var module = targetMethod.Module;
var body = targetMethod.Body;
// In order to use FakeConsole.WriteLine(), we need to use Cecil to "import"
// the method into the modified assembly's module
var writeLine = module.Import(typeof (FakeConsole).GetMethod("WriteLine"));
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="sdkDir" value="C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin"/>
<add key="sdkDirUnderVista" value="C:\Program Files (x86)\Microsoft.NET\SDK\v2.0\Bin"/>
</appSettings>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
var testHarness = new TestHarness();
// The processors decide which methods should be replaced and how they should behave once the method is called
var consoleTestProcessor = new ConsoleTestProcessor();
testHarness.Processors.Add(consoleTestProcessor);
var fileSystemTestProcessor = new FileSystemTestProcessor();
fileSystemTestProcessor.AddMockFile(@"c:\foo.txt", "bar");
testHarness.Processors.Add(fileSystemTestProcessor);
@philiplaureano
philiplaureano / gist:5279046
Created March 31, 2013 00:58
An example of how you can use Design by Contract macros and Non-nullable type macros in Nemerle to write more reliable code
public FlushContentsTo([NotNull] outputStream : Stream) : uint
requires outputStream.CanWrite
{
def startPosition = 0;
_heap.Seek(startPosition);
outputStream.Seek(startPosition);
def bytes = _heap.ToArray();
def writer = BinaryWriter(outputStream);
writer.Write(bytes);
@philiplaureano
philiplaureano / gist:5422630
Created April 19, 2013 19:31
This sample shows how Tao uses hashing to verify all of its streams and byte arrays. It makes CLR metadata and bytecode verification possible since we can use it to incrementally verify small chunks of data against a stream of bytes from an assembly.
using Nemerle;
using Nemerle.Assertions;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
@philiplaureano
philiplaureano / gist:5423009
Created April 19, 2013 20:28
This extension method is what allows Tao to find the first byte mismatch within two given streams, regardless of their size.
public static ShouldMatch(this stream : Stream, other : Stream) : void
{
def hash = stream.GetHash();
def otherHash = other.GetHash();
mutable smallerStream = other;
when(other.Length > stream.Length)
{
smallerStream = stream;
}