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)); } } }
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)); }