Code Cop Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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