Skip to content

Instantly share code, notes, and snippets.

@DewJunkie
Last active October 8, 2015 14:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save DewJunkie/393a6c6c5b0cf0c73398 to your computer and use it in GitHub Desktop.
Code Cop Testing
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<runtime>
<useLegacyJit enabled="1" />
</runtime>
</configuration>
g:\dev\test\CodeCop\CodeCopTutorial\Testing\bin\Debug>Testing.exe
Intercepting Void .ctor()
Intercepting System.String get_Name()
Intercepting Void set_Name(System.String)
Intercepting Void Greet(System.String)
Pre Void .ctor()
Post Void .ctor()
Pre Void set_Name(System.String)
Post Void set_Name(System.String)
Pre Void Greet(System.String)
Pre System.String get_Name()
Post System.String get_Name()
Hello Joe my name is Duane
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at CodeCop.Core.InterceptorExecuter.AfterExecute(Int32 token)
at Testing.TestClass.Greet(String greetee) in g:\dev\test\CodeCop\CodeCopTutorial\Testing\Program.cs:line 69
at Testing.Program.Main(String[] args) in g:\dev\test\CodeCop\CodeCopTutorial\Testing\Program.cs:line 23
g:\dev\test\CodeCop\CodeCopTutorial\Testing\bin\Debug>Testing.exe
Intercepting System.String get_Name()
Intercepting Void set_Name(System.String)
Intercepting Void Greet(System.String)
Pre Void set_Name(System.String)
Post Void set_Name(System.String)
Pre Void Greet(System.String)
Pre System.String get_Name()
Post System.String get_Name()
Hello Joe my name is Duane
Post Void Greet(System.String)
Pre Void set_Name(System.String)
Post Void set_Name(System.String)
Pre Void Greet(System.String)
Pre System.String get_Name()
Post System.String get_Name()
Hello Duane my name is Joe
Post Void Greet(System.String)
g:\dev\test\CodeCop\CodeCopTutorial\Testing\bin\Debug>
using CodeCop.Core;
using CodeCop.Core.Fluent;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Testing
{
class Program
{
static void Main(string[] args)
{
Cop.AsFluent();
InterceptAll<TestClass>();
Cop.Intercept();
var testclass = new TestClass();
testclass.Name = "Duane";
testclass.Greet("Joe");
testclass.Name = "Joe";
testclass.Greet("Duane");
Console.ReadKey();
}
public static void InterceptAll<T>()
{
// If I comment out the constructor interception, then the method interception works just fine.
// With both the constructor and the methods intercepted, then I get a null object reference.
var type = typeof(T);
foreach (var constructor in type.GetConstructors())
{
Console.WriteLine("Intercepting {0}", constructor);
constructor.DecorateBefore(context =>
{
Console.WriteLine("Pre {0}", context.InterceptedMethod);
});
constructor.DecorateAfter(context =>
{
Console.WriteLine("Post {0}", context.InterceptedMethod);
});
}
foreach (var method in type.GetMethods().Where(m => m.DeclaringType == type))
{
Console.WriteLine("Intercepting {0}", method);
method.DecorateBefore(context =>
{
Console.WriteLine("Pre {0}", context.InterceptedMethod);
});
method.DecorateAfter(context =>
{
Console.WriteLine("Post {0}", context.InterceptedMethod);
});
}
}
}
public class TestClass
{
public string Name { get; set; }
public void Greet(string greetee)
{
Console.WriteLine("Hello {0} my name is {1}", greetee, Name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment