Saturday, December 10, 2016

Compile Time Check IsNullable and IsReferenceType


        public TimeSpan i { get; set; }
        public TimeSpan? j { get; set; }
        public string k { get; set; }
        public Status? Status { get; set; }
        public Data st1 { get; set; }
        public Data? st2 { get; set; }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            
            bool b1 = IsNullable(i);
            bool b2 = IsNullable(j);
            bool b3 = IsNullable(k);
            bool b4 = IsNullable(Status);
            bool b5 = IsNullable(st1);
            bool b6 = IsNullable(st2);
            bool b7 = IsReferenceType(k);
        }
         static bool IsNullable<T>(T t) { return false; }
         static bool IsNullable<T>(T? t) where T : struct { return true; }
         static bool IsReferenceType<T>(T t) where T : class { return true; }
    

    public enum Status
    {
        None,
        Ok,
        Error
    }

    public struct Data
    {

    }

No comments:

Post a Comment