繁体   English   中英

知道对象的字段/属性是“简单/原始”类型还是其他对象?

[英]Know if Fields/Properties of an object are “simple/primitive” types or other objects?

我得到以下生成DLL的代码(示例示例):

public class PluginClass
{
    private string _MyString;
    public string MyString
    {
        get { return _MyString; }
        set
        {
            _MyString = value;
            RaisePropertyChanged("MyString");
        }
    }

    public int MyInt;

    public SpeedGenerator SpeedGenerator1 = new SpeedGenerator();

    public GaugeValueGenerator GaugeValueGenerator1
    {
        get;
        set;
    }

    public PluginClass()
    {
        GaugeValueGenerator1 = new GaugeValueGenerator();
    }
}

如你所见,我有4个字段/属性。

1个原始字段(primitive是int / string / bool / etcetc ...):MyInt 1原始属性:MyString 1对象字段:SpeedGenerator1 1对象属性:GaugeValueGenerator1

当我解析我的DLL时,我得做一些函数中的代码:WriteProperty

var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
var props = type.GetProperties();

foreach (FieldInfo field in fields)
{
    WriteProperty(field.FieldType, field.Name, XXX);
}

foreach (PropertyInfo prop in props)
{
    WriteProperty(prop.PropertyType, prop.Name, XXX);
}

我的问题是关于XXX这是一个布尔值,表明我的字段/属性是否是“原始的”。 因此,如果它是一个对象,则必须设置为false。 我摔倒了,因为我尝试了一切,但我无法解决它......任何帮助都会非常感激!

(我的想法是打电话

var props = propertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

并认为它对于简单/原始类型应该是空的! 但是没有...例如在String上,这返回属性Chars(char [])和Length(int)...)

(nb:当然我不想对field / property.Name / FullName做一个字符串操作......就像

if ((propertyType.FullName).Contains("System."))

会非常非常糟糕......并且不准确)

这应该做到这一点。

 public static bool IsPrimate(this object obj)
    {
        return new[]
        {
            typeof (Enum),
            typeof (String),
            typeof (Char),
            typeof (Guid),
            typeof (Boolean),
            typeof (Byte),
            typeof (Int16),
            typeof (Int32),
            typeof (Int64),
            typeof (Single),
            typeof (Double),
            typeof (Decimal),
            typeof (SByte),
            typeof (UInt16),
            typeof (UInt32),
            typeof (UInt64),
            typeof (DateTime),
            typeof (DateTimeOffset),
            typeof (TimeSpan),
        }.Any(oo => oo.IsInstanceOfType(obj));
    }

为什么不使用Type类的IsPrimitive

XXX = field.FiledType.IsPrimitive

编辑:您必须将string视为一种特殊情况,因为IsPrimitive不会返回true

编辑2:你遇到的问题是你试图嫁给两个不匹配的原始定义。 因此,我只能看到两种选择:

  1. 使两个定义匹配,显然你不能改变CLR类型系统,并且可能无法改变你正在使用的框架。

  2. 做一些“嫁给”这两个定义的黑客 我没有看到硬编码与primitve类型的两个定义之一不匹配的特定异常的其他方法。

您可以使用field.FieldType.IsPrimitiveprop.PropertyType.IsPrimitive ,但如果您希望将stringdecimal等视为基元,那么您会感到失望。

为什么不能创建自己的一套, 认为是原语并核对类型?

WriteProperty(f.FieldType, f.Name, yourPrimitives.Contains(f.FieldType));

// ...

WriteProperty(p.PropertyType, p.Name, yourPrimitives.Contains(p.PropertyType));

// ...

private static readonly HashSet<Type> yourPrimitives = new HashSet<Type>
    {
        typeof(int), typeof(string), typeof(decimal)    // etc
    };

另一种选择是使用GetTypeCode ,然后检查结果不TypeCode.ObjectTypeCode.DBNull等,这真的取决于你的需求是什么 ,以及 究竟认为什么是基本类型。

暂无
暂无

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

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