Exemplo n.º 1
0
            public void ExpectedException <T>() where T : Exception
            {
                if (_asserts.Count == 0)
                {
                    throw new Exception($"Missing Exception:{typeof(T).Name}");
                }

                TestAssertInfo info = _asserts[0];

                DynamicType dyn = DynamicType.FromXML(info.Message);

                if (dyn.IsValid())
                {
                    if (dyn.IsVoid())
                    {
                        throw new Exception($"Source:{info.Source} Message:{info.Message}");
                    }
                    else if (dyn.Is(DynamicType.Type.CONTAINER))
                    {
                        DynamicTypeContainer cont = dyn;

                        if (cont.GetAttribute("Type") != typeof(T).Name)
                        {
                            throw new Exception($"Source:{info.Source} Message:{info.Message}");
                        }
                    }
                }
                else
                {
                    throw new Exception($"Source:{info.Source} Message:{info.Message}");
                }
            }
            static public void RestorePropertiesAndFields(DynamicTypeContainer container, object obj, bool allProperties = false)
            {
                var bindingFlags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public;

                foreach (System.Reflection.PropertyInfo prop in obj.GetType().GetProperties(bindingFlags))
                {
                    if (allProperties || Attribute.IsDefined(prop, typeof(DynamicTypeProperty)))
                    {
                        prop.SetValue(obj, container.GetAttribute(prop.Name).GetObject(prop.PropertyType, allProperties));
                    }
                }

                foreach (System.Reflection.FieldInfo field in obj.GetType().GetFields(bindingFlags))
                {
                    if (allProperties || Attribute.IsDefined(field, typeof(DynamicTypeProperty)))
                    {
                        field.SetValue(obj, container.GetAttribute(field.Name).GetObject(field.FieldType, allProperties));
                    }
                }
            }
            static public void StorePropertiesAndFields(DynamicTypeContainer container, object obj, bool allProperties = false)
            {
                var bindingFlags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public;

                foreach (System.Reflection.PropertyInfo prop in obj.GetType().GetProperties(bindingFlags))
                {
                    if (allProperties || Attribute.IsDefined(prop, typeof(DynamicTypeProperty)))
                    {
                        var  value       = prop.GetValue(obj);
                        bool reflectType = value == null ? false : prop.PropertyType != value.GetType();
                        container.SetAttribute(prop.Name, DynamicType.CreateDynamicType(value, allProperties, reflectType));
                    }
                }

                foreach (System.Reflection.FieldInfo field in obj.GetType().GetFields(bindingFlags))
                {
                    if (allProperties || Attribute.IsDefined(field, typeof(DynamicTypeProperty)))
                    {
                        var  value       = field.GetValue(obj);
                        bool reflectType = value == null ? false : field.FieldType != value.GetType();
                        container.SetAttribute(field.Name, DynamicType.CreateDynamicType(value, allProperties, reflectType));
                    }
                }
            }
Exemplo n.º 4
0
            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));
            }
Exemplo n.º 5
0
            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 DynamicTypeContainerIterator(DynamicTypeContainer cont) : base(DynamicTypeContainerIterator_create(cont.GetNativeReference()))
 {
 }