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)
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.
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.
In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties.
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.
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));
}
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With