Last active
August 29, 2015 13:57
-
-
Save makomweb/9503718 to your computer and use it in GitHub Desktop.
Extension property playground: using a conversion operator
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 NUnit.Framework; | |
namespace Spikes | |
{ | |
public class Foo | |
{ | |
public Foo(string name) | |
{ | |
Name = name; | |
} | |
public string Name { get; private set; } | |
} | |
public class ExtendedFoo | |
{ | |
private readonly Foo _obj; | |
public ExtendedFoo(Foo obj) | |
{ | |
_obj = obj; | |
} | |
public static implicit operator ExtendedFoo(Foo obj) | |
{ | |
return new ExtendedFoo(obj); | |
} | |
public string Name | |
{ | |
get { return _obj.Name + "bar"; } | |
} | |
} | |
public class ExtendedPropertyTest | |
{ | |
[Test] | |
public void When_calling_property_then_should_deliver_name() | |
{ | |
var foo = new Foo("foo"); | |
Assert.That(foo.Name, Is.EqualTo("foo")); | |
} | |
[Test] | |
public void When_casting_then_should_deliver_extended_property() | |
{ | |
var foo = new Foo("foo"); | |
Assert.That(((ExtendedFoo)foo).Name, Is.EqualTo("foobar")); | |
} | |
[Test] | |
public void When_using_implicit_conversion_then_should_use_extended_property() | |
{ | |
var foo = new Foo("foo"); | |
AssertValue(foo); | |
} | |
private static void AssertValue(ExtendedFoo extended) | |
{ | |
Assert.That(extended.Name, Is.EqualTo("foobar")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment