detecting native objects with reflection

后端 未结 2 1400
既然无缘
既然无缘 2021-01-06 06:01

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/

相关标签:
2条回答
  • 2021-01-06 06:17

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

    0 讨论(0)
  • 2021-01-06 06:24

    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 讨论(0)
提交回复
热议问题