Do you know if the fields / properties of an object are "simple / primitive" or other objects?

I got the following code that the DLL generates (example example):

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 have 4 fields / properties.

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

When I parsed my DLL, I had to make code that is in a 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 about XXX, which is a boolean indicating whether my field / property is "primitive". Therefore, it must be set to false if it is an object. I fell as if I tried everything, but I can’t solve it ... Any help would be greatly appreciated!

(My idea was to call

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

and note that it must be empty for simple / primitive types! But no ... for example, in String, this returns the properties Chars (char []) and Length (int) ...)

(nb: Of course, I do not want to do a line operation in the /property.Name/FullName field ... something like

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

will be very very juicy ... and inaccurate)

+6
source share
3 answers

Why not use the IsPrimitive Type class?

 XXX = field.FiledType.IsPrimitive 

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

EDIT 2: The problem you are facing is that you are trying to marry two primitive definitions that do not match. In this case, I see only two options:

  • Compare both definitions, which, obviously, you cannot change the system of the CLR type and, probably, cannot either change the framework used by you.

  • Make some sort of hack that β€œmarries” both definitions. I see no other way to hard-code specific exceptions that do not match one of the two definitions of primitive types.

+5
source

That 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)); } 
+4
source

You can use field.FieldType.IsPrimitive and prop.PropertyType.IsPrimitive , but you will be disappointed if you expect string , decimal , etc. will be considered primitives.

Why not create your own set of types that you consider primitive, and check for this?

 WriteProperty(f.FieldType, f.Name, yourPrimitives.Contains(f.FieldType)); // ... WriteProperty(p.PropertyType, p.Name, yourPrimitives.Contains(p.PropertyType)); // ... private static readonly HashSet<Type> yourPrimitives = new HashSet<Type> { typeof(int), typeof(string), typeof(decimal) // etc }; 

Another option is to use GetTypeCode and then verify that the result is not TypeCode.Object , TypeCode.DBNull , etc. It really depends on your requirements and what exactly you consider a primitive type.

+2
source

Source: https://habr.com/ru/post/904086/


All Articles