static void DeserializeImpl <T>(out T obj, TextReader tr, ConstructFromXmlParams parameters) { XmlSerializer s = new XmlSerializer(typeof(T)); obj = (T)s.Deserialize(tr); CallConstructFromXml(obj, parameters); }
public void ConstructFromXml(ConstructFromXmlParams parameters) { // This allows to use path to xml file in the values of properties. string inheritedProp; inheritedProp = "loc.xml.File"; string file = parameters.Local.GetRaw(inheritedProp); Set(inheritedProp, file); Set("loc.xml.FileName", Path.GetFileNameWithoutExtension(file)); Set("loc.xml.FileExt", Path.GetExtension(file)); inheritedProp = "loc.xml.Dir"; Set(inheritedProp, parameters.Local.GetRaw(inheritedProp)); }
public static void Deserialize <T>(out T obj, string fileName) { string absolutePath = fileName; if (!Path.IsPathRooted(absolutePath)) { absolutePath = Path.Combine(Directory.GetCurrentDirectory(), fileName); } using (TextReader tr = new StreamReader(absolutePath)) { ConstructFromXmlParams p = new ConstructFromXmlParams { XmlFile = absolutePath }; p.Local.UpdateFrom(Props.Global); p.Local.Set("loc.xml.File", p.XmlFile); p.Local.Set("loc.xml.Dir", Path.GetDirectoryName(absolutePath) + Path.DirectorySeparatorChar); DeserializeImpl(out obj, tr, p); tr.Close(); } }
static void CallConstructFromXml <T>(T obj, ConstructFromXmlParams parameters) { try { MethodInfo methodInfo = typeof(T).GetMethod("ConstructFromXml"); if (methodInfo != null) { methodInfo.Invoke(obj, new object[] { parameters }); } } catch (Exception e) { string xmlFile = parameters.XmlFile ?? "null"; // Create an exception wrapper containing information about the type name // because this is a very important information to analyze the problem and // this information is usually missing in the original exception. ApplicationException excWrapper = new ApplicationException( String.Format("Type: {0}, file: {1}: CallConstructFromXml() failed, see inner exception for details.", typeof(T).Name, xmlFile), e); throw excWrapper; } }