为什么DateTime的GetType不是常量值

时间:2017-11-29 04:39:36

标签: c# oop c#-4.0 switch-statement c#-3.0

我正在处理带有类型检查的switch语句。以下代码适用于所有类型,但挑战在于Nullable类型。

  switch (Type.GetTypeCode( propertyInfos.PropertyType))
                    {
                        // Type code doesn't have reference with int, long, etc.,
                        case TypeCode.DateTime:
                            // Do the work for DateTime
                            break;
                        case TypeCode.Int32 :
                            // Do the work for Int32
                            break;
                        case TypeCode.Int64:
                            // Do the work for long
                            break;
                        case TypeCode.DateTime? :
                            break;
                        default:
                            break;
                    }

我已尝试将其更改为GetTypeDateTime.Today.GetType().ToString()会将System.DateTime作为字符串提供给我们。但是,使用时,编译器会抛出错误,因为它不是有效的Constant string。在任何给定的时间实例,DateTime.Today.GetType()总是会给我们System.DateTime,为什么编译器不接受这个?

2 个答案:

答案 0 :(得分:2)

我找到了this clever solution using a dictionary instead of a switch。使用该方法,这应该适合您:

if (ndkDirectory) {
  externalNativeBuild {
     ndkBuild {
       path 'src/main/jni/Android.mk'
    }
  }
}

答案 1 :(得分:0)

我使用过cannot resolve symbol foo方法。我已经为可空类型应用了类型检查,然后确定了可空类型,然后最终指定了可为空的通用类型。

Nullable.GetUnderlyingType