public static T Load(string filePath_, bool createDefault = false) #endif { if (createDefault) { #if NETFX_CORE if (!(await FileTools.FileExists(filePath_))) #else if (!File.Exists(filePath_)) #endif { string dir = Path.GetDirectoryName(filePath_); #if NETFX_CORE await FileTools.CreateDirectory(dir); #else FileTools.CreateDirectory(dir); #endif // We create a new one, and then we can set the default properties. T instance = Activator.CreateInstance <T>(); IEnumerable <AttributeAndProp <DefaultValueAttribute> > attrs = ReflectionTools.GetAttributesOnProperties <DefaultValueAttribute, T>(); foreach (var item in attrs) { object val = null; if (item.Attribute.UseNewInstace) { val = Activator.CreateInstance(item.Property.PropertyType); } else { val = item.Attribute.Value; } item.Property.SetValue(instance, val, null); } #if NETFX_CORE await instance.Save(filePath_, false); #else instance.Save(filePath_, false); #endif return(instance); } } #if NETFX_CORE var res = await InternalLoad(filePath_); res.PostLoad(); #else var res = InternalLoad(filePath_); res.PostLoad(); #endif return(res); }
public void CanGetPropertyAttributes() { var propsWithAttributes = ReflectionTools.GetAttributesOnProperties <DefaultValueAttribute, TypeWithPropsWithAttributes>().ToList(); Assert.AreEqual(2, propsWithAttributes.Count, "There should be two items listed!"); Assert.AreEqual("x", propsWithAttributes[0].Attribute.Value, "Bad value for item #1!"); Assert.AreEqual("SomeString", propsWithAttributes[0].Property.Name); Assert.AreEqual(100, propsWithAttributes[1].Attribute.Value, "Bad value for item #2!"); Assert.AreEqual("SomeInt", propsWithAttributes[1].Property.Name); }