Nullable type support in WCF service
Posted: Fri Feb 10, 2017 1:10 am
Hi,
I have wrote following class for CSHTML compatible wcf Nullable<ValueType> implementation:
it works partially like nullable types,
limitations: e.g.
1. it can be initialized using the constructor only
2. value cannot be returned as null
Let me know how can I make it exactly work like Nullable<> support in .net.
Kind regards
I have wrote following class for CSHTML compatible wcf Nullable<ValueType> implementation:
Code: Select all
[DataContract]
public class CustomNullable<T> where T : struct
{
private T value { get; set; }
[DataMember]
public bool HasValue { get; set; }
[DataMember]
public T Value
{
get
{
return GetValueOrDefault(default(T));
}
set
{
this.value = value;
this.HasValue = true;
}
}
public CustomNullable()
{
}
public CustomNullable(T value)
{
Value = value;
}
public T GetValueOrDefault()
{
return this.value;
}
public T GetValueOrDefault(T defaultValue)
{
if (!this.HasValue)
return defaultValue;
return this.value;
}
public override bool Equals(object other)
{
if (!this.HasValue)
return other == null;
if (other == null)
return false;
return this.value.Equals(other);
}
public override int GetHashCode()
{
if (!this.HasValue)
return 0;
return this.Value.GetHashCode();
}
public override string ToString()
{
if (!this.HasValue)
return "";
return this.Value.ToString();
}
}
it works partially like nullable types,
limitations: e.g.
1. it can be initialized using the constructor only
2. value cannot be returned as null
Let me know how can I make it exactly work like Nullable<> support in .net.
Kind regards