检测与反思本地对象 [英] detecting native objects with reflection

查看:101
本文介绍了检测与反思本地对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正与一个基于反射对象翻译。

I am working with a reflection based object translator.

它基本上遍历一个对象的属性,以及分配值属性具有相同名称/类型的翻译对象上。

it basically loops through properties of an object, and assigns the values to properties with the same name/type on the translated object.

ObjectA.Name =乔

语句:

ObjectB.Name =乔

我需要把一个特殊的情况下,当一个属性是一个自定义类,如:

I need to put a special case, for when a property is a custom class such as:

ObjectA.Address

我希望我能有IsClass标志属性类型

i was hoping i could detect such properties with IsClass flag of PropertyType

propInfo.PropertyType.IsClass

但这个标志也显得返回true字符串属性。

but this flag also appears to return true for string properties.

有另一种方式我可以验证属性是一种非原生类型的?

is there another way i could verify that the property is of a non native type?

推荐答案

我假设你要确定目标类型不是一颗灵长类动物。你也许可以使用键入code 对于这一点,例如:

I'm assuming you want to determine if the target type is not a primative. You can probably use TypeCode for that, for instance:

public bool IsNotCoreType(Type type)
{
    return (type != typeof(object) && Type.GetTypeCode(type) == TypeCode.Object);
}

任何非原始应该返回键入code.Object 的结果 Type.GetType code ,所以我们可以检查我们得到的,并且类型本身是不是 System.Object的

Any non-primitive should return TypeCode.Object as the result of Type.GetTypeCode, so we can check that we get that and that the type itself is not System.Object.

也许这将帮助?

更新:我已经改名方法的 IsNotCoreType 的,既包括原语和非原语,如字符串的DateTime 等(见下面的评论)。

UPDATE: I've renamed the method to IsNotCoreType to cover both primitives and non-primitives such as String, DateTime, etc. (see comments below).

这篇关于检测与反思本地对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆