private void ParseProperties(ComplexProperty property, TypeInfo typeInfo, object value) { var propertyInfos = _propertyProvider.GetProperties(typeInfo); foreach (var propertyInfo in propertyInfos) { var subValue = propertyInfo.GetValue(value, _emptyObjectArray); var subProperty = CreateProperty(propertyInfo.Name, subValue); property.Properties.Add(subProperty); } }
private void ParseCollectionItems(CollectionProperty property, TypeInfo info, object value) { property.ElementType = info.ElementType; var collection = (IEnumerable)value; foreach (var item in collection) { var itemProperty = CreateProperty(null, item); property.Items.Add(itemProperty); } }
private void ParseDictionaryItems(DictionaryProperty property, TypeInfo info, object value) { property.KeyType = info.KeyType; property.ValueType = info.ElementType; var dictionary = (IDictionary)value; foreach (DictionaryEntry entry in dictionary) { var keyProperty = CreateProperty(null, entry.Key); var valueProperty = CreateProperty(null, entry.Value); property.Items.Add(new KeyValueItem(keyProperty, valueProperty)); } }
private bool FillMultiDimensionalArrayProperty(MultiDimensionalArrayProperty property, TypeInfo info, object value) { if (property == null) { return false; } property.ElementType = info.ElementType; var analyzer = new ArrayAnalyzer(value); // DimensionInfos property.DimensionInfos = analyzer.ArrayInfo.DimensionInfos; // Items foreach (var indexSet in analyzer.GetIndexes()) { var subValue = ((Array)value).GetValue(indexSet); var itemProperty = CreateProperty(null, subValue); property.Items.Add(new MultiDimensionalArrayItem(indexSet, itemProperty)); } return true; }
private bool FillSingleDimensionalArrayProperty(SingleDimensionalArrayProperty property, TypeInfo info, object value) { if (property == null) { return false; } property.ElementType = info.ElementType; var analyzer = new ArrayAnalyzer(value); // Dimensionen var dimensionInfo = analyzer.ArrayInfo.DimensionInfos[0]; property.LowerBound = dimensionInfo.LowerBound; // Items foreach (var item in analyzer.GetValues()) { var itemProperty = CreateProperty(null, item); property.Items.Add(itemProperty); } return true; }
private bool FillComplexProperty(ComplexProperty property, TypeInfo typeInfo, object value) { if (property == null) { return false; } // Parsing properties ParseProperties(property, typeInfo, value); return true; }
private bool FillDictionaryProperty(DictionaryProperty property, TypeInfo info, object value) { if (property == null) { return false; } // Properties ParseProperties(property, info, value); // Items ParseDictionaryItems(property, info, value); return true; }
private bool FillCollectionProperty(CollectionProperty property, TypeInfo info, object value) { if (property == null) { return false; } // Parsing properties ParseProperties(property, info, value); // Parse Items ParseCollectionItems(property, info, value); return true; }
private static Property CreateSimpleProperty(string name, TypeInfo typeInfo, object value) { if (!typeInfo.IsSimple) { return null; } var result = new SimpleProperty(name, typeInfo.Type); result.Value = value; return result; }
private static ReferenceTargetProperty CreateReferenceTargetInstance(string name, TypeInfo typeInfo) { // Is it array? if (typeInfo.IsArray) { if (typeInfo.ArrayDimensionCount < 2) { // 1D-Array return new SingleDimensionalArrayProperty(name, typeInfo.Type); } // MultiD-Array return new MultiDimensionalArrayProperty(name, typeInfo.Type); } if (typeInfo.IsDictionary) { return new DictionaryProperty(name, typeInfo.Type); } if (typeInfo.IsCollection) { return new CollectionProperty(name, typeInfo.Type); } if (typeInfo.IsEnumerable) { // Actually it would be enough to check if the typeinfo.IsEnumerable is true... return new CollectionProperty(name, typeInfo.Type); } // If nothing was recognized, a complex type will be created return new ComplexProperty(name, typeInfo.Type); }
///<summary> ///</summary> ///<param name = "type"></param> ///<returns></returns> public static TypeInfo GetTypeInfo(Type type) { // check if Info is in cache var typeInfo = Cache.TryGetTypeInfo(type); if (typeInfo == null) { // no info in cache yet typeInfo = new TypeInfo(); typeInfo.Type = type; typeInfo.IsSimple = Tools.IsSimple(type); // new since v.2.16 // check if array of byte if (type == typeof (byte[])) { typeInfo.ElementType = typeof (byte); } // Only not simple types can be Collections if (!typeInfo.IsSimple) { // check if it is an Array typeInfo.IsArray = Tools.IsArray(type); if (typeInfo.IsArray) { // Array? What is its element type? if (type.HasElementType) { typeInfo.ElementType = type.GetElementType(); } // How many dimensions typeInfo.ArrayDimensionCount = type.GetArrayRank(); } else { // It is not Array, maybe Enumerable? typeInfo.IsEnumerable = Tools.IsEnumerable(type); if (typeInfo.IsEnumerable) { // it is Enumerable maybe Collection? typeInfo.IsCollection = Tools.IsCollection(type); if (typeInfo.IsCollection) { // Sure it is a Collection, but maybe Dictionary also? typeInfo.IsDictionary = Tools.IsDictionary(type); // Fill its key and value types, if the listing is generic bool elementTypeDefinitionFound; var examinedType = type; do { elementTypeDefinitionFound = FillKeyAndElementType(typeInfo, examinedType); examinedType = examinedType.BaseType; // until key and element definition was found, or the base typ is an object } while (!elementTypeDefinitionFound && examinedType != null && examinedType != typeof (object)); } } } } #if PORTABLE Cache.AddIfNotExists(typeInfo); #else Cache.Add(typeInfo); #endif } return typeInfo; }
/// <summary> /// /// </summary> /// <param name="typeInfo"></param> /// <param name="type"></param> /// <returns>true if the key and value definition was found</returns> private static bool FillKeyAndElementType(TypeInfo typeInfo, Type type) { if (type.IsGenericType) { var arguments = type.GetGenericArguments(); if (typeInfo.IsDictionary) { // in Dictionary there are keys and values typeInfo.KeyType = arguments[0]; typeInfo.ElementType = arguments[1]; } else { // In Collection there are only items typeInfo.ElementType = arguments[0]; } return arguments.Length > 0; } return false; }