/// <summary> /// This method initialize TypeSingleton /// </summary> protected void Initialize <RootType>() { foreach (Type type in ScalarTypes.SCALARS_TYPE) { TypeMetadata newMetadata = ExtractTypeMetadata(type); this.SetType(newMetadata); } List <Type> result = new List <Type>(); RecursivelyExtractTypeMetadata(typeof(RootType), result); foreach (Type type in result) { if (types.ContainsKey(type.GUID)) { continue; } if (type.IsEnum && !enums.ContainsKey(type.Name)) { enums.Add(type.Name, type); } TypeMetadata tm = TypeSingleton.ExtractTypeMetadata(type); this.SetType(tm); } }
/// <summary> /// Method is used for creating ObjectWrapper. This method is created because /// DateTime and TimeSpan types. When we transforming this two object /// we need to get only number of ticks from them. All other cases we just /// need to put object to Data property and object will be transformed in a right way. /// </summary> /// <param name="typeOfdata">Type of data object</param> /// <param name="data"></param> /// <returns></returns> public static ObjectWrapper CreateObjectWrapper(Object data) { ObjectWrapper ow = new ObjectWrapper(); if (data == null) { ow.TypeName = null; ow.Data = null; return(ow); } Type typeOfdata = data.GetType(); ow.TypeName = TypeSingleton.ExtractPropertyType(typeOfdata);//typeOfdata.Name; if (typeOfdata.Equals(typeof(DateTime))) { ow.Data = ((DateTime)data).Ticks; } else { if (typeOfdata.Equals(typeof(TimeSpan))) { ow.Data = ((TimeSpan)data).Ticks; } else { ow.Data = data; } } return(ow); }
/// <summary> /// This method is used for transforming Node into NodeWrapper. /// </summary> /// <param name="node"> Node that need to be tranformed into NodeWrapper</param> /// <returns>NodeWrapper that is being result of transformation of Node</returns> public static NodeWrapper TransformeNode(Node <Guid, object, EdgeData> node) { if (node == null) { return(null); } NodeWrapper newNode = new NodeWrapper(); newNode.Commited = node.Commited; newNode.NodeType = node.NodeType; newNode.Previous = node.Previous; ObjectWrapper newData = null; if (node.NodeType == NodeType.Type) { //getting name of type of node data. newData = ObjectWrapper.CreateObjectWrapper(node.Data); //this is needed because data contains string that is contain data that //identifies type in .NET. Because of that we need to change type of objectWrapper newData.TypeName = TypeSingleton.ExtractPropertyType(Type.GetType((String)node.Data)); newNode.Data = newData; } else { //getting type name of data, in case node.Data is null type will be null newData = ObjectWrapper.CreateObjectWrapper(node.Data); newNode.Data = newData; } foreach (KeyValuePair <Guid, object> item in node.Values) { //making ObjectWrapper from object ObjectWrapper ow = ObjectWrapper.CreateObjectWrapper(item.Value); //adding new Guid-ObjectWrapper pair into Values newNode.Values.Add(item.Key.ToString(), ow); } foreach (KeyValuePair <EdgeData, Edge <Guid, EdgeData> > item in node.Edges) { //getting type of key and value Type typeOfObject = item.Key.GetType(); Type edgeDataType = typeof(EdgeData); if (typeOfObject.Equals(edgeDataType)) { EdgeDataWrapper edgeDataKey = EdgeDataWrapper.TransformeEdgeData(item.Key); EdgeWrapper edgeWraper = EdgeWrapper.TransformeEdge(item.Value); newNode.Edges.Add(new KeyValueWrapper(edgeDataKey, edgeWraper)); } else { throw new Exception("Unexpected type in node wraper!"); } } return(newNode); }
public static TypeSingleton GetInstance <RootType>() { lock (locker) { if (instance == null) { instance = new TypeSingleton(); instance.Initialize <RootType>(); } } return(instance); }
/// <summary> /// This method is used for finding all property types. It is used in way, in begining you should /// give this method RootType as type, and return list will contain all types that root type contains. /// </summary> /// <param name="type"> type that need to be processed</param> /// <param name="result"> list in which we will add all types that are found</param> /// <returns>list of all finded types</returns> public static List <Type> RecursivelyExtractTypeMetadata(Type type, List <Type> result) { if (type.GetInterfaces() != null && type.GetInterfaces().Length != 0) { foreach (Type t in type.GetInterfaces()) { if (!result.Contains(t)) { RecursivelyExtractTypeMetadata(t, result); } if (!result.Contains(t)) { result.Add(t); } } } Collection <PropertyInfo> propertiesOfType = new Collection <PropertyInfo>(); Utils.ExtractProperties(type, propertiesOfType); result.Insert(0, type); foreach (PropertyInfo pi in propertiesOfType) { Type propertyType = null; Type collectionType = null; Type dictionaryType = null; if (Utils.IsCollectionType(pi.PropertyType, ref collectionType)) { if (collectionType.IsGenericType) { propertyType = collectionType.GetGenericArguments()[0]; if (!result.Contains(propertyType) && !ScalarTypes.IsScalarType(propertyType)) { TypeSingleton.RecursivelyExtractTypeMetadata(propertyType, result); } result.Add(collectionType); } } else { if (Utils.IsDictionaryType(pi.PropertyType, ref dictionaryType)) { Type firstType = dictionaryType.GetGenericArguments()[0]; Type secondType = dictionaryType.GetGenericArguments()[1]; if (!result.Contains(firstType) && !ScalarTypes.IsScalarType(propertyType)) { TypeSingleton.RecursivelyExtractTypeMetadata(firstType, result); } if (!result.Contains(secondType) && !ScalarTypes.IsScalarType(propertyType)) { TypeSingleton.RecursivelyExtractTypeMetadata(secondType, result); } result.Add(dictionaryType); } else { propertyType = pi.PropertyType; if (!result.Contains(propertyType) && !ScalarTypes.IsScalarType(propertyType)) { TypeSingleton.RecursivelyExtractTypeMetadata(propertyType, result); } } } } return(result); }
/// <summary> /// This method is used to get value from object that is got from js client. /// objectToParse need to have data about type of value that it contians. /// </summary> /// <param name="objectToParse">is Object that need to be parsed</param> /// <returns>value of spcified type, or null</returns> public static object ParseObjectWrapper(Object objectToParse) { if (objectToParse == null) { return(null); } try { if (objectToParse is Dictionary <String, Object> ) { Dictionary <String, Object> dictionaryOfObject = (Dictionary <String, Object>)objectToParse; if (dictionaryOfObject.ContainsKey(IOG_TYPE) && dictionaryOfObject[IOG_TYPE] is Dictionary <String, Object> && dictionaryOfObject.ContainsKey(VALUE)) { Dictionary <String, Object> iogType = (Dictionary <String, Object>)dictionaryOfObject[IOG_TYPE]; if (iogType.ContainsKey(NAME) && iogType[NAME] is String) { String typeName = (String)iogType[NAME]; if (typeName == null) { return(null); } switch (typeName) { case BOOLEAN: return(Convert.ToBoolean(dictionaryOfObject[VALUE])); case INT32: return(Convert.ToInt32(dictionaryOfObject[VALUE])); case INT64: return(Convert.ToInt64(dictionaryOfObject[VALUE])); case DOUBLE: var value = dictionaryOfObject[VALUE]; return(Convert.ToDouble(dictionaryOfObject[VALUE])); case STRING: return((String)dictionaryOfObject[VALUE]); case CHAR: return(Char.Parse((String)dictionaryOfObject[VALUE])); case BYTE: return(Convert.ToByte(dictionaryOfObject[VALUE])); case DATE_TIME: long valueDateTime = Convert.ToInt64(dictionaryOfObject[VALUE]); return(new DateTime(valueDateTime)); case TIME_SPAN: long valueTimeSpan = Convert.ToInt64(dictionaryOfObject[VALUE]); return(new TimeSpan(valueTimeSpan)); case GUID_TYPE: Guid guid = new Guid((String)dictionaryOfObject[VALUE]); return(guid); case null: case EMPTY_STRING: return(null); default: Type en = TypeSingleton.GetInstace().GetEnum(typeName); if (en == null) { throw new Exception("Unsupported type!"); } object enumValue = null; if (en.IsEnum) { enumValue = Enum.ToObject(en, Convert.ToInt64(dictionaryOfObject[VALUE])); return(enumValue); } throw new Exception("Unsupported type!"); } } else { throw new Exception("Object is not well formated!"); } } else { throw new Exception("Object is not well formated!"); } } else { throw new Exception("Object is not well formated!"); } } catch (Exception e) { e.ToString(); throw e; } }