开发者

detecting native objects with reflection

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 = "Joe"

translates to:

ObjectB.Name = "Joe"

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

ObjectA.Address

i was hoping i could detect such开发者_运维知识库 properties with IsClass flag of PropertyType

propInfo.PropertyType.IsClass

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?


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

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.

Perhaps that would help?

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


string is an exception, the only primitive type in .NET which is a reference type. You have to consider this exception in your code so that you check if IsClass is true and type is not the same as System.String.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜