c# - C#中如何判断类型是否为字符串?

标签 c#

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

我的类(class)是:

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))

关于c# - C#中如何判断类型是否为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9517333/

相关文章:

c# - 开源金融库特别适合成熟

c# - 在 windows shell 中集成应用程序?

c# - StackExchange.Redis如何订阅多个 channel

c# - 正确转换为枚举

c# - 异步方法是否在单独的线程中运行?

c# - 从 Sqlite 中检索 ulong 会导致 NotSupportedException

c# - 没有语法 C# 的语音识别 API

c# - 在 XNA 项目中打开 Web 浏览器?

c# - 如何在 Xunit 中创建基测试类来测试接口(interface)实现?

c# - 运行 ibtool : object cannot be nil (key: family) 时出现 Xamarin.IOS 异常