簡體   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