I am trying to create a template that can detect when a property has been set to null. Something similar to a class Nullable<T>, but a little more advanced. Let me call him MoreThanNullable<T>. Basically I need to do something different depending on the following three scenarios:
- The property has never been set.
- The property was set to null
- The property is assigned an instance of T.
I created my own class to do this using the “created” property, and it all works in a test script. Code example:
public struct MoreThanNullable<T>
{
private bool hasValue;
internal T value;
public bool Instantiated { get; set; }
public MoreThanNullable(T value)
{
this.value = value;
this.hasValue = true;
Instantiated = true;
}
And the test that passes, as I expect it:
[TestFixture]
public class MoreThanNullableTests
{
public class AccountViewModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public MoreThanNullable<string> Test1 { get; set; }
public MoreThanNullable<string> Test2 { get; set; }
public MoreThanNullable<string> Test3 { get; set; }
}
[Test]
public void Tests()
{
var myClass = new AccountViewModel();
Assert.AreEqual(false, myClass.Test1.Instantiated);
myClass.Test1 = null;
Assert.AreEqual(true, myClass.Test1.Instantiated);
}
}
Then, using this same view model, I connect it to POST on my REST service, and I pass the following JSON:
{
Name:"test",
Test1:null,
Test2:"test"
}
... . Test1, Test3 ! , , Test3 , Test1, , . , , REST. (: null)
- ? -API?
** **
, , , , Nullable<T> , :
public static implicit operator MoreThanNullable<T>(T value)
{
return new MoreThanNullable<T>(value);
}
, , ...