[英]How to check whether a type is string in C#?
I want to go through all the properties of a type and want to check whether a property type is not a string, how can I do it ?我想查看一个类型的所有属性,并想检查一个属性类型是否不是字符串,我该怎么做?
My class is:我的班级是:
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;
}
}
the code I am using to check for type is:我用来检查类型的代码是:
foreach (var property in typeof(MarkerInfo).GetProperties())
{
if (property.PropertyType is typeof(string))
}
But this code is not working, any idea how to do that ?但是这段代码不起作用,知道怎么做吗?
if (property.PropertyType == typeof(string))
Use the following instead:请改用以下内容:
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.