Created
March 11, 2015 20:23
-
-
Save mstyura/4204b6beb5c1042bc53b to your computer and use it in GitHub Desktop.
Prints "this is null" without ildasm/asm round-trip
This file contains hidden or 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 System; | |
using System.Runtime.Remoting.Proxies; | |
namespace CtorReturningNull | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
new Devil().SayHello(); | |
} | |
} | |
[NullInstanceProxy] | |
class Devil : ContextBoundObject | |
{ | |
public void SayHello() | |
{ | |
if (this == null) | |
{ | |
Console.WriteLine("This is null"); | |
} | |
else | |
{ | |
Console.WriteLine("This is not null"); | |
} | |
} | |
} | |
[AttributeUsage(AttributeTargets.Class)] | |
class NullInstanceProxyAttribute : ProxyAttribute | |
{ | |
public override MarshalByRefObject CreateInstance(Type serverType) | |
{ | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment