static public void RestoreList(DynamicTypeArray array, IList list, Type type, bool allProperties = false) { foreach (DynamicType t in array) { list.Add(t.GetObject(type, allProperties)); } }
static public void StoreEnumerable(DynamicTypeArray array, IEnumerable enumer, bool allProperties = false, Type type = null) { foreach (object o in enumer) { array.Add(DynamicType.CreateDynamicType(o, allProperties, type != null && o != null ? type != o.GetType() : false)); } }
// --- Reflection mechanisms -------------------------------- static public void RestoreArray(DynamicTypeArray array, Array list, bool allProperties = false) { for (int i = 0; i < list.GetLength(0); i++) { list.SetValue(array[i].GetObject(list.GetType().GetElementType(), allProperties), i); } }
public object GetObject(System.Type t, bool allProperties = false) { if (Is(DynamicType.Type.VOID)) { return(null); } if (t == typeof(DynamicType)) { return(this); } t = Nullable.GetUnderlyingType(t) ?? t; if (t.IsSubclassOf(typeof(DynamicType))) { return(this); } if (t == typeof(DynamicTypeArray) && Is(DynamicType.Type.ARRAY)) { return((DynamicTypeArray)this); } if (t.IsSubclassOf(typeof(DynamicTypeArray)) && Is(DynamicType.Type.ARRAY)) { object o = Activator.CreateInstance(t); ((DynamicTypeArray)o).Set(this); return(o); } if (t == typeof(DynamicTypeContainer) && Is(DynamicType.Type.CONTAINER)) { return((DynamicTypeContainer)this); } if (t.IsSubclassOf(typeof(DynamicTypeContainer)) && Is(DynamicType.Type.CONTAINER)) { object o = Activator.CreateInstance(t); ((DynamicTypeContainer)o).Set(this); return(o); } // ------- Converts to builtins -------------------- if (t == typeof(string)) { return(GetString()); } if (t == typeof(UInt64)) { return((UInt64)(DynamicTypeInt64)this); } if (t == typeof(Int64)) { return((Int64)(DynamicTypeInt64)this); } if (t == typeof(Vec2)) { return(GetVec2()); } if (t == typeof(Vec3)) { return(GetVec3()); } if (t == typeof(Vec4)) { return(GetVec4()); } if (t == typeof(Guid)) { return(GetGuid()); } if (t == typeof(System.Type)) { if (TypeResolver != null) { return(System.Type.GetType(GetString(), AssemblyResolver, TypeResolver)); } else { return(System.Type.GetType(GetString())); } } if (t == typeof(Reference) && Is(DynamicType.Type.REFERENCE)) { return(this); } if (t.IsSubclassOf(typeof(Reference)) && Is(DynamicType.Type.REFERENCE)) { return(this); } if (t == typeof(object) && !Is(Type.CONTAINER)) { // ------- Converts to builtins -------------------- if (Is(Type.STRING)) { return(GetString()); } if (Is(Type.INT64)) { return((Int64)this); } if (Is(Type.VEC2)) { return(GetVec2()); } if (Is(Type.VEC3)) { return(GetVec3()); } if (Is(Type.VEC4)) { return(GetVec3()); } if (Is(Type.GUID)) { return(GetGuid()); } if (Is(Type.REFERENCE)) { return((Reference)this); } } if (t.IsEnum) { if (Marshal.SizeOf(Enum.GetUnderlyingType(t)) <= sizeof(UInt32)) { return(Enum.ToObject(t, (UInt32)this)); } else { return(Enum.ToObject(t, (UInt64)this)); } } if (Is(DynamicType.Type.ARRAY)) { if (typeof(Array).IsAssignableFrom(t)) // Array { DynamicTypeArray array = (DynamicTypeArray)this; Array obj = Array.CreateInstance(t.GetElementType(), array.Count); DynamicTypeArray.RestoreArray(array, obj, allProperties); return(obj); } if (typeof(System.Collections.IList).IsAssignableFrom(t)) { object obj = Activator.CreateInstance(t); System.Collections.IList list = (System.Collections.IList)obj; DynamicTypeArray array = (DynamicTypeArray)this; if (t.IsGenericType) { DynamicTypeArray.RestoreList(array, list, t.GenericTypeArguments[0], allProperties); } return(obj); } // We can create it but not populate it return(Activator.CreateInstance(t)); } if (t.IsValueType && !t.IsPrimitive) // struct { object obj = Activator.CreateInstance(t); DynamicTypeContainer.RestorePropertiesAndFields(this, obj, allProperties); return(obj); } if (typeof(System.Collections.IDictionary).IsAssignableFrom(t) && Is(DynamicType.Type.CONTAINER)) { if (t.GenericTypeArguments[0] != typeof(string)) { throw new NotSupportedException("only dictionaries with string key types are supported"); } var valueType = t.GenericTypeArguments[1]; DynamicTypeContainer container = this; var obj = Activator.CreateInstance(t) as System.Collections.IDictionary; foreach (var kvp in container) { obj.Add(kvp.Key, kvp.Value.GetObject(valueType, allProperties)); } return(obj); } if (t.IsClass && Is(DynamicType.Type.CONTAINER)) { DynamicTypeContainer container = this; object obj; string _type_ = container.GetAttribute(TYPE_REFLECT); if (_type_ != null) { if (TypeResolver != null) { obj = Activator.CreateInstance(System.Type.GetType(_type_, AssemblyResolver, TypeResolver)); } else { obj = Activator.CreateInstance(System.Type.GetType(_type_)); } if (obj == null) { obj = Activator.CreateInstance(t); } } else { obj = Activator.CreateInstance(t); } DynamicTypeContainer.RestorePropertiesAndFields(container, obj, allProperties); return(obj); } if (!IsValid()) { return(null); } // Default to integer number return(Convert.ChangeType(GetNumber(), t)); }
public static DynamicType CreateDynamicType(object obj, bool allProperties = false, bool addReflectedType = false) { if (obj == null) { return(new DynamicType()); } System.Type t = obj.GetType(); if (obj is DynamicType) { return(obj as DynamicType); } if (obj is DynamicTypeContainer) { return((DynamicType)(obj as DynamicTypeContainer)); } if (obj is DynamicTypeArray) { return((DynamicType)(obj as DynamicTypeArray)); } if (t == typeof(string)) { return(new DynamicType((string)obj)); } if (t == typeof(Int64)) { return(new DynamicTypeInt64((Int64)obj)); } if (t == typeof(UInt64)) { return(new DynamicTypeInt64((UInt64)obj)); } if (t == typeof(Vec2)) { return(new DynamicType((Vec2)obj)); } if (t == typeof(Vec3)) { return(new DynamicType((Vec3)obj)); } if (t == typeof(Vec4)) { return(new DynamicType((Vec4)obj)); } if (t == typeof(Guid)) { return(new DynamicType((Guid)obj)); } if (t == typeof(Reference)) { return(new DynamicType((Reference)obj)); } if (t == typeof(System.Type)) { return(new DynamicType(t.AssemblyQualifiedName)); } if (t.IsEnum) { if (Marshal.SizeOf(Enum.GetUnderlyingType(obj.GetType())) <= sizeof(UInt32)) { return(new DynamicType((UInt32)Convert.ChangeType(obj, typeof(UInt32)))); } else { return(new DynamicType((UInt64)Convert.ChangeType(obj, typeof(UInt64)))); } } if (t.IsValueType && !t.IsPrimitive) // Struct { DynamicTypeContainer cont = new DynamicTypeContainer(); DynamicTypeContainer.StorePropertiesAndFields(cont, obj, allProperties); if (addReflectedType) { cont.SetAttribute(TYPE_REFLECT, t.AssemblyQualifiedName); } return(cont); } if (obj is System.Collections.IDictionary) { var dic = obj as System.Collections.IDictionary; if (t.GenericTypeArguments[0] == typeof(string)) { DynamicTypeContainer cont = new DynamicTypeContainer(); var keys = dic.Keys; var vals = dic.Values; foreach (string key in keys) { cont.SetAttribute(key, CreateDynamicType(dic[key], allProperties, addReflectedType)); } return(cont); } } if (obj is System.Collections.IEnumerable) { System.Type elemType = obj.GetType().GetElementType(); if (elemType == null && obj.GetType().IsGenericType) { elemType = obj.GetType().GenericTypeArguments[0]; } DynamicTypeArray array = new DynamicTypeArray(); DynamicTypeArray.StoreEnumerable(array, (System.Collections.IEnumerable)obj, allProperties, elemType); return(array); } if (t.IsClass) { DynamicTypeContainer cont = new DynamicTypeContainer(); DynamicTypeContainer.StorePropertiesAndFields(cont, obj, allProperties); if (addReflectedType) { cont.SetAttribute(TYPE_REFLECT, t.AssemblyQualifiedName); } return(cont); } return(new DynamicType((double)Convert.ChangeType(obj, typeof(double)))); // default to double }
public void Dispose() { m_array = null; }
public DynamicTypeArrayIterator(DynamicTypeArray array) { m_index = -1; m_array = array; }