Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么DateTime的GetType不是常量值_C#_Oop_C# 4.0_Switch Statement_C# 3.0 - Fatal编程技术网

C# 为什么DateTime的GetType不是常量值

C# 为什么DateTime的GetType不是常量值,c#,oop,c#-4.0,switch-statement,c#-3.0,C#,Oop,C# 4.0,Switch Statement,C# 3.0,我正在做一个带有类型检查的switch语句。下面的代码对所有类型都非常有效,但是,挑战在于使用可为空的类型 switch (Type.GetTypeCode( propertyInfos.PropertyType)) { // Type code doesn't have reference with int, long, etc., case TypeCo

我正在做一个带有类型检查的switch语句。下面的代码对所有类型都非常有效,但是,挑战在于使用可为空的类型

  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;
                    }

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

我使用了
Nullable.getunderyingtype
方法。我对可空类型应用了类型检查,然后识别了可空类型,最后指定了可空类型的泛型类型

if (Nullable.GetUnderlyingType(propertyType) != null)
            {
                // It's nullable
                Console.WriteLine(propertyType.GetGenericArguments()[0]);

            }
我找到了。使用该方法,您应该可以:

public class Test {
    public DateTime A { get; set; }
    public Int32 B { get; set; }
    public Int64 C { get; set; }
    public DateTime? D { get; set; }
}

...Main...
        var @switch = new Dictionary<Type, Action> {
            { typeof(DateTime), () => Console.WriteLine("DateTime") },
            { typeof(Int32), () => Console.WriteLine("Int32") },
            { typeof(Int64), () => Console.WriteLine("Int64") },
            { typeof(DateTime?), () => Console.WriteLine("DateTime?") },
        };

        foreach (var prop in typeof(Test).GetProperties()) {
            @switch[prop.PropertyType]();
        }
公共类测试{
公共日期时间A{get;set;}
公共Int32 B{get;set;}
公共Int64 C{get;set;}
公共日期时间?D{get;set;}
}
…主要。。。
var@switch=新字典{
{typeof(DateTime),()=>Console.WriteLine(“DateTime”)},
{typeof(Int32),()=>Console.WriteLine(“Int32”)},
{typeof(Int64),()=>Console.WriteLine(“Int64”)},
{typeof(DateTime?,()=>Console.WriteLine(“DateTime?”)},
};
foreach(typeof(Test).GetProperties()中的var prop){
@开关[prop.PropertyType]();
}

您有两次DateTime。第一个案例和最后一个非默认案例都是DateTime。消息是“switch语句包含多个带有标签值“16”的案例”。投票结束。