繁体   English   中英

如何在C#中检查类型是否为字符串?

[英]How to check whether a type is string in C#?

我想查看一个类型的所有属性,并想检查一个属性类型是否不是字符串,我该怎么做?

我的班级是:

public class MarkerInfo
{
    public string Name { get; set; }
    public byte[] Color { get; set; }
    public TypeId Type { get; set; }
    public bool IsGUIVisible { get; set; }

    public MarkerInfo()
    {
        Color = new byte[4]; // A, R, G, B
        IsGUIVisible = true;
    }
}

我用来检查类型的代码是:

foreach (var property in typeof(MarkerInfo).GetProperties())
{               
    if (property.PropertyType is typeof(string))              
}

但是这段代码不起作用,知道怎么做吗?

if (property.PropertyType == typeof(string))

请改用以下内容:

foreach (var property in typeof(MarkerInfo).GetProperties())
{               
    if (property.PropertyType == typeof(string))              
}

使用==而不是is or is String (保留 typeof)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM