Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Know if Fields/Properties of an object are "simple/primitive" types or other objects?

I got the following code that generate a DLL (sample exemple) :

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

As you can see I got 4 fields/properties.

1 primitive field (primitive is int/string/bool/etcetc...) : MyInt 1 primitive property : MyString 1 object field : SpeedGenerator1 1 object property : GaugeValueGenerator1

When Im parsing my DLL I got to do some code that is in the function : 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);
}

My question is on XXX that is a boolean that indicate if my field/property is "primitive". So it must be set to false if it is an object. I fell like I tried quite everything but I can't resolve it... Any help will be very appreciate !

(My idea was to call

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

and consider that it should be empty for simple/primitive types ! But no... for example on String, this return the properties Chars (char[]) and Length (int)...)

(nb : Of course I don't wanna do a string operation on field/property.Name/FullName... something like

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

would be very very sucky... and inaccurate)

like image 811
Guillaume Slashy Avatar asked Dec 19 '11 15:12

Guillaume Slashy


People also ask

How do you know if a type is primitive?

Approach 1: In this approach, we check the type of the value using the typeof operator. If the type of the value is 'object' or 'function' then the value is not primitive otherwise the value is primitive. But the typeof operator shows the null to be an “object” but actually, the null is a Primitive.

What are primitive properties?

A Primitive Property holds a primitive value, or a collection of primitive values, of a specified type. The Primitive Property has an assigned data type, which may be a Built-in Primitive Type, a User-defined Primitive Type, an Enumeration or a Simple Type.

Is object a primitive type?

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties.

Are primitive types value types?

A value type is usually whatever type reside on the Stack . A primitive type is a type defined at the programming language level, often it is even a value type, directly supported by the compiler of the language.


2 Answers

This should do it.

 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));
    }
like image 54
Matas Vaitkevicius Avatar answered Oct 12 '22 23:10

Matas Vaitkevicius


Why not use IsPrimitive of the Type class?

XXX = field.FiledType.IsPrimitive

EDIT: You will have to treat string as a special case as IsPrimitive will not return true.

EDIT 2: The problem you are having is that you are trying to marry two primitve definitions wich don't match. Being that the case I can only see two options:

  1. Make both definitions match which obviously you can't do changing the CLR type system and probably can't do either changing the framework you are using.

  2. Make some hack that "marries" both definitions. I see no other way around hardcoding the specific exceptions that don't match one of the two definitions of primitve types.

like image 43
InBetween Avatar answered Oct 12 '22 22:10

InBetween