Nullable type support in WCF service

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
njs123
Posts: 25
Joined: Sun Dec 25, 2016 11:10 pm

Nullable type support in WCF service

Postby njs123 » Fri Feb 10, 2017 1:10 am

Hi,

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

Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 2 guests