/// <summary> /// Discovers the type information. /// </summary> /// <param name="objectType">Type of the object.</param> /// <param name="objectMetaData">The object meta data.</param> private static void DiscoverTypeInfo(Type objectType, IObjectMetaData objectMetaData) { while (true) { if (objectType == null) { return; } objectMetaData.Type = objectType; var dataType = GetType(objectType); if (dataType != DataType.Undefined) { return; } var collectionElementType = GetElementType(objectType); if (collectionElementType != null) { objectMetaData.DataType = DataType.Collection; objectMetaData.CollectionItemType = collectionElementType; objectMetaData.ChildObjMetaData = new ObjectMetaData(); objectType = collectionElementType; objectMetaData = objectMetaData.ChildObjMetaData; continue; } objectMetaData.DataType = DataType.Object; objectMetaData.PropertiesMetaData = new List <IObjectMetaData>(); foreach (var propertyInfo in objectType.GetProperties().OrderBy(property => property.Name)) { if (!propertyInfo.PropertyType.IsPublicEx() || propertyInfo.GetSetMethod() == null) { continue; } var propertyMetaData = new ObjectMetaData { PropertyInfo = propertyInfo }; DiscoverTypeInfo(propertyInfo.PropertyType, propertyMetaData); objectMetaData.PropertiesMetaData.Add(propertyMetaData); } break; } }
/// <summary> /// Discovers the and decimal serialize. /// </summary> /// <param name="stream">The stream.</param> /// <param name="objectType">Type of the object.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">The <paramref name="stream"/> is <c>null</c>.</exception> /// <exception cref="System.ArgumentNullException">The <paramref name="objectType"/> is <c>null</c>.</exception> public static object DiscoverAndDeSerialize(Stream stream, Type objectType) { Argument.IsNotNull("stream", stream); Argument.IsNotNull("objectType", objectType); lock (Cache) { IObjectMetaData objectMetaData; if (Cache.Keys.Contains(objectType.FullName)) { objectMetaData = Cache[objectType.FullName]; } else { objectMetaData = new ObjectMetaData(); DiscoverTypeInfo(objectType, objectMetaData); Cache.Add(objectType.FullName, objectMetaData); } return DeSerialize(stream, objectMetaData); } }
/// <summary> /// Discovers the and decimal serialize. /// </summary> /// <param name="stream">The stream.</param> /// <param name="objectType">Type of the object.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">The <paramref name="stream"/> is <c>null</c>.</exception> /// <exception cref="System.ArgumentNullException">The <paramref name="objectType"/> is <c>null</c>.</exception> public static object DiscoverAndDeSerialize(Stream stream, Type objectType) { Argument.IsNotNull("stream", stream); Argument.IsNotNull("objectType", objectType); lock (Cache) { IObjectMetaData objectMetaData; if (Cache.Contains(objectType.FullName)) { objectMetaData = Cache[objectType.FullName]; } else { objectMetaData = new ObjectMetaData(); DiscoverTypeInfo(objectType, objectMetaData); Cache.Add(objectType.FullName, objectMetaData); } return(DeSerialize(stream, objectMetaData)); } }
/// <summary> /// Discovers the type information. /// </summary> /// <param name="objectType">Type of the object.</param> /// <param name="objectMetaData">The object meta data.</param> private static void DiscoverTypeInfo(Type objectType, IObjectMetaData objectMetaData) { while (true) { if (objectType == null) { return; } objectMetaData.Type = objectType; var dataType = GetType(objectType); if (dataType != DataType.Undefined) { return; } var collectionElementType = GetElementType(objectType); if (collectionElementType != null) { objectMetaData.DataType = DataType.Collection; objectMetaData.CollectionItemType = collectionElementType; objectMetaData.ChildObjMetaData = new ObjectMetaData(); objectType = collectionElementType; objectMetaData = objectMetaData.ChildObjMetaData; continue; } objectMetaData.DataType = DataType.Object; objectMetaData.PropertiesMetaData = new List<IObjectMetaData>(); foreach (var propertyInfo in objectType.GetProperties().OrderBy(property => property.Name)) { if (!propertyInfo.PropertyType.IsPublicEx() || propertyInfo.GetSetMethod() == null) { continue; } var propertyMetaData = new ObjectMetaData {PropertyInfo = propertyInfo}; DiscoverTypeInfo(propertyInfo.PropertyType, propertyMetaData); objectMetaData.PropertiesMetaData.Add(propertyMetaData); } break; } }