Inheritance: System.Attribute
Ejemplo n.º 1
0
        public static ODocument ToDocument <T>(T genericObject)
        {
            ODocument document          = new ODocument();
            Type      genericObjectType = genericObject.GetType();

            // TODO: recursive mapping of nested/embedded objects
            foreach (PropertyInfo propertyInfo in genericObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                string propertyName = propertyInfo.Name;

                // serialize following properties into dedicated fields in ODocument
                if (propertyName.Equals("ORID"))
                {
                    document.ORID = (ORID)propertyInfo.GetValue(genericObject, null);
                    continue;
                }
                else if (propertyName.Equals("OVersion"))
                {
                    document.OVersion = (int)propertyInfo.GetValue(genericObject, null);
                    continue;
                }
                else if (propertyName.Equals("OClassId"))
                {
                    document.OClassId = (short)propertyInfo.GetValue(genericObject, null);
                    continue;
                }
                else if (propertyName.Equals("OClassName"))
                {
                    document.OClassName = (string)propertyInfo.GetValue(genericObject, null);
                    continue;
                }

                bool     isSerializable = true;
                object[] oProperties    = propertyInfo.GetCustomAttributes(typeof(OProperty), true);

                if (oProperties.Any())
                {
                    OProperty oProperty = oProperties.First() as OProperty;
                    if (oProperty != null)
                    {
                        propertyName   = oProperty.Alias;
                        isSerializable = oProperty.Serializable;
                    }
                }

                if (isSerializable)
                {
                    document.SetField(propertyName, propertyInfo.GetValue(genericObject, null));
                }
            }

            return(document);
        }
Ejemplo n.º 2
0
        private T ToObject <T>(T genericObject, string path) where T : class, new()
        {
            Type genericObjectType = genericObject.GetType();

            if (genericObjectType.Name.Equals("ODocument") ||
                genericObjectType.Name.Equals("OVertex") ||
                genericObjectType.Name.Equals("OEdge"))
            {
                foreach (KeyValuePair <string, object> item in this)
                {
                    (genericObject as ODocument).SetField(item.Key, item.Value);
                }
            }
            else
            {
                foreach (PropertyInfo propertyInfo in genericObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    string propertyName = propertyInfo.Name;

                    // serialize orient specific fields into dedicated properties
                    if (propertyName.Equals("ORID"))
                    {
                        propertyInfo.SetValue(genericObject, this.ORID, null);
                        continue;
                    }
                    else if (propertyName.Equals("OVersion"))
                    {
                        propertyInfo.SetValue(genericObject, this.OVersion, null);
                        continue;
                    }
                    else if (propertyName.Equals("OClassId"))
                    {
                        propertyInfo.SetValue(genericObject, this.OClassId, null);
                        continue;
                    }
                    else if (propertyName.Equals("OClassName"))
                    {
                        propertyInfo.SetValue(genericObject, this.OClassName, null);
                        continue;
                    }

                    object[] oProperties = propertyInfo.GetCustomAttributes(typeof(OProperty), true);

                    if (oProperties.Any())
                    {
                        OProperty oProperty = oProperties.First() as OProperty;
                        if (oProperty != null)
                        {
                            propertyName = oProperty.Alias;
                        }
                    }

                    string fieldPath = path + (path != "" ? "." : "") + propertyName;

                    if ((propertyInfo.PropertyType.IsArray || propertyInfo.PropertyType.IsGenericType))
                    {
                        if (this.HasField(fieldPath))
                        {
                            object propertyValue = this.GetField <object>(fieldPath);

                            IList collection = (IList)propertyValue;

                            if (collection.Count > 0)
                            {
                                // create instance of property type
                                object collectionInstance = Activator.CreateInstance(propertyInfo.PropertyType, collection.Count);

                                for (int i = 0; i < collection.Count; i++)
                                {
                                    // collection is simple array
                                    if (propertyInfo.PropertyType.IsArray)
                                    {
                                        ((object[])collectionInstance)[i] = collection[i];
                                    }
                                    // collection is generic
                                    else if (propertyInfo.PropertyType.IsGenericType && (propertyValue is IEnumerable))
                                    {
                                        Type elementType = collection[i].GetType();

                                        // generic collection consists of basic types or ORIDs
                                        if (elementType.IsPrimitive ||
                                            (elementType == typeof(string)) ||
                                            (elementType == typeof(DateTime)) ||
                                            (elementType == typeof(decimal)) ||
                                            (elementType == typeof(ORID)))
                                        {
                                            ((IList)collectionInstance).Add(collection[i]);
                                        }
                                        // generic collection consists of generic type which should be parsed
                                        else
                                        {
                                            // create instance object based on first element of generic collection
                                            object instance = Activator.CreateInstance(propertyInfo.PropertyType.GetGenericArguments().First(), null);

                                            ((IList)collectionInstance).Add(ToObject(instance, fieldPath));
                                        }
                                    }
                                    else
                                    {
                                        object v = Activator.CreateInstance(collection[i].GetType(), collection[i]);

                                        ((IList)collectionInstance).Add(v);
                                    }
                                }

                                propertyInfo.SetValue(genericObject, collectionInstance, null);
                            }
                        }
                    }
                    // property is class except the string or ORID type since string and ORID values are parsed differently
                    else if (propertyInfo.PropertyType.IsClass &&
                             (propertyInfo.PropertyType.Name != "String") &&
                             (propertyInfo.PropertyType.Name != "ORID"))
                    {
                        // create object instance of embedded class
                        object instance = Activator.CreateInstance(propertyInfo.PropertyType);

                        object o = ToObject(instance, fieldPath);
                        propertyInfo.SetValue(genericObject, o, null);
                    }
                    // property is basic type
                    else
                    {
                        if (this.HasField(fieldPath))
                        {
                            object propertyValue = this.GetField <object>(fieldPath);

                            propertyInfo.SetValue(genericObject, propertyValue, null);
                        }
                    }
                }
            }

            return(genericObject);
        }