//Read in the value of a simple type from the wrapper object DeserializeIntegral(object inobject, Type elementtype, ScormWrapper wrapper) { if (elementtype == typeof(string)) { inobject = wrapper.Get(GetCurrentIdentifier(), typeof(string)).ToString(); } if (elementtype == typeof(int)) { inobject = wrapper.Get(GetCurrentIdentifier(), typeof(int)).ToInt(); } if (elementtype == typeof(Decimal) || elementtype == typeof(System.Nullable <Decimal>)) { inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Decimal)).ToDecimal(); } if (elementtype == typeof(bool)) { inobject = wrapper.Get(GetCurrentIdentifier(), typeof(bool)).ToBool(); } if (elementtype.IsEnum) { inobject = wrapper.Get(GetCurrentIdentifier(), inobject.GetType()).ToEnum(inobject.GetType()); } if (elementtype == typeof(Scorm2004.DateTime)) { inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Scorm2004.DateTime)).ToDateTime2004(); } if (elementtype == typeof(Scorm2004.TimeSpan)) { inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Scorm2004.TimeSpan)).ToTimeSpan2004(); } if (elementtype == typeof(Scorm1_2.DateTime)) { inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Scorm1_2.DateTime)).ToDateTime1_2(); } if (elementtype == typeof(Scorm1_2.TimeSpan)) { inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Scorm1_2.TimeSpan)).ToTimeSpan1_2(); } return(inobject); }
//If it's a specially formatted string with lang and value portions, treat is as one string and encode the language //in braces private object DeSerializeLangString(object inobject, Type type, ScormWrapper wrapper) { if (inobject == null) { inobject = Activator.CreateInstance(type); } try { // string language = (string)type.GetProperty("lang").GetValue(inobject, null); // string value = (string)type.GetProperty("Value").GetValue(inobject, null); //stack.Push("{" + language + "}" + value); //stack.Pop(); string temp = wrapper.Get(GetCurrentIdentifier(), typeof(string)).ToString(); if (temp.IndexOf("{lang=") != -1) { string[] tokens = temp.Split(new string[] { "{lang=", "}" }, StringSplitOptions.RemoveEmptyEntries); string language = tokens[0]; string value = ""; if (tokens.Length > 1) { value = tokens[1]; } type.GetProperty("lang").SetValue(inobject, language, null); type.GetProperty("Value").SetValue(inobject, value, null); } else { type.GetProperty("lang").SetValue(inobject, "en-US", null); type.GetProperty("Value").SetValue(inobject, temp, null); } } catch (Exception e) { } return(inobject); }
//read in the value of a complex type from the wrapper, recurse if necessary object DeserializeObject(object inobject,Type otype, ScormWrapper wrapper) { //if the object is null and not an array, create a new instance of the //type it should be if (!otype.Name.Contains("System.Collections.Generic.List")) { //little hacky to handle strings if (inobject == null && otype != typeof(string)) inobject = Activator.CreateInstance(otype); if (inobject == null && otype == typeof(string)) inobject = ""; } //if it is an array, call the length and create a new array of that length else { //if (inobject == null) { inobject = System.Activator.CreateInstance(otype); } } //If its not an array iterate over the members if (!otype.FullName.Contains("System.Collections.Generic.List")) { PropertyInfo[] members = otype.GetProperties(); foreach (PropertyInfo mi in members) { if (!mi.Name.Contains("Specified") && DeserializeThis(mi)) { //sanity check. should not be null if (inobject == null) return null; //If its a simple type, call DeserializeIntegral if (mi.PropertyType == typeof(string) || mi.PropertyType == typeof(int) || mi.PropertyType == typeof(float) || mi.PropertyType == typeof(bool) || mi.PropertyType == typeof(Scorm1_2.DateTime) || mi.PropertyType == typeof(Scorm1_2.TimeSpan) || mi.PropertyType == typeof(Scorm2004.DateTime) || mi.PropertyType == typeof(Scorm2004.TimeSpan) || mi.PropertyType == typeof(Decimal) || mi.PropertyType == typeof(System.Nullable<Decimal>) || mi.PropertyType.IsEnum ) { stack.Push(mi.Name); mi.SetValue(inobject, DeserializeIntegral(mi.GetValue(inobject, null), mi.PropertyType, wrapper), null); stack.Pop(); } else if (IsLangStringType(mi.PropertyType)) { stack.Push(mi.Name); mi.SetValue(inobject, DeSerializeLangString(mi.GetValue(inobject, null), mi.PropertyType, wrapper), null); stack.Pop(); } //Its a complex type, push its name on the stack and recurse else { stack.Push(mi.Name); mi.SetValue(inobject, DeserializeObject(mi.GetValue(inobject, null), mi.PropertyType, wrapper), null); stack.Pop(); } } } } //This object is an array - not that the clause above will create a blank array of the proper //length, so this should never be null else { //Loop over every entry in the array stack.Push("_count"); int length = wrapper.Get(GetCurrentIdentifier(), typeof(int)).ToInt(); stack.Pop(); int i = 0; Array arrray = null; MethodInfo[] methods232 = otype.GetMethods(); foreach (MethodInfo m in methods232) { if (m.Name == "ToArray") arrray = (Array)m.Invoke(inobject, null); } otype = arrray.GetType(); for (i = 0; i < length; i++) { //push the array index on the identifier name stack stack.Push(i.ToString()); //If it's a simple element, deserialize and set it on the array if (otype.GetElementType() == typeof(string) || otype.GetElementType() == typeof(int) || otype.GetElementType() == typeof(float) || otype.GetElementType() == typeof(bool) || otype.GetElementType() == typeof(Scorm2004.DateTime) || otype.GetElementType() == typeof(Scorm2004.TimeSpan) || otype.GetElementType() == typeof(Scorm1_2.DateTime) || otype.GetElementType() == typeof(Scorm1_2.TimeSpan) || otype.GetElementType() == typeof(Decimal) || otype.GetElementType().IsEnum ) arrray.SetValue(DeserializeIntegral(arrray.GetValue(i), otype.GetElementType(), wrapper), i); else if (IsLangStringType(otype.GetElementType())) { arrray.SetValue(DeSerializeLangString(arrray.GetValue(i), otype.GetElementType(), wrapper), i); } else //Its a complex object, deserialize it an set it on the array inobject.GetType().GetMethod("Add").Invoke(inobject,new object[]{DeserializeObject(null, otype.GetElementType(), wrapper)}); //pop the index off the identifier name stack stack.Pop(); } } //return the object - important because the inobject could have changed from null to something, and //the parent object must know that the value has been set return inobject; }
//If it's a specially formatted string with lang and value portions, treat is as one string and encode the language //in braces private object DeSerializeLangString(object inobject,Type type, ScormWrapper wrapper) { if(inobject == null) inobject = Activator.CreateInstance(type); try { // string language = (string)type.GetProperty("lang").GetValue(inobject, null); // string value = (string)type.GetProperty("Value").GetValue(inobject, null); //stack.Push("{" + language + "}" + value); //stack.Pop(); string temp = wrapper.Get(GetCurrentIdentifier(),typeof(string)).ToString(); if (temp.IndexOf("{lang=") != -1) { string[] tokens = temp.Split(new string[] { "{lang=", "}" }, StringSplitOptions.RemoveEmptyEntries); string language = tokens[0]; string value = ""; if (tokens.Length > 1) value = tokens[1]; type.GetProperty("lang").SetValue(inobject, language, null); type.GetProperty("Value").SetValue(inobject, value, null); } else { type.GetProperty("lang").SetValue(inobject, "en-US", null); type.GetProperty("Value").SetValue(inobject, temp, null); } } catch (Exception e) { } return inobject; }
//Read in the value of a simple type from the wrapper object DeserializeIntegral(object inobject,Type elementtype, ScormWrapper wrapper) { if (elementtype == typeof(string)) inobject = wrapper.Get(GetCurrentIdentifier(), typeof(string)).ToString(); if (elementtype == typeof(int)) inobject = wrapper.Get(GetCurrentIdentifier(), typeof(int)).ToInt(); if (elementtype == typeof(Decimal) || elementtype == typeof(System.Nullable<Decimal>)) inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Decimal)).ToDecimal(); if (elementtype == typeof(bool)) inobject = wrapper.Get(GetCurrentIdentifier(), typeof(bool)).ToBool(); if (elementtype.IsEnum) inobject = wrapper.Get(GetCurrentIdentifier(), inobject.GetType()).ToEnum(inobject.GetType()); if (elementtype == typeof(Scorm2004.DateTime)) inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Scorm2004.DateTime)).ToDateTime2004(); if (elementtype == typeof(Scorm2004.TimeSpan)) inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Scorm2004.TimeSpan)).ToTimeSpan2004(); if (elementtype == typeof(Scorm1_2.DateTime)) inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Scorm1_2.DateTime)).ToDateTime1_2(); if (elementtype == typeof(Scorm1_2.TimeSpan)) inobject = wrapper.Get(GetCurrentIdentifier(), typeof(Scorm1_2.TimeSpan)).ToTimeSpan1_2(); return inobject; }
//read in the value of a complex type from the wrapper, recurse if necessary object DeserializeObject(object inobject, Type otype, ScormWrapper wrapper) { //if the object is null and not an array, create a new instance of the //type it should be if (!otype.Name.Contains("System.Collections.Generic.List")) { //little hacky to handle strings if (inobject == null && otype != typeof(string)) { inobject = Activator.CreateInstance(otype); } if (inobject == null && otype == typeof(string)) { inobject = ""; } } //if it is an array, call the length and create a new array of that length else { //if (inobject == null) { inobject = System.Activator.CreateInstance(otype); } } //If its not an array iterate over the members if (!otype.FullName.Contains("System.Collections.Generic.List")) { PropertyInfo[] members = otype.GetProperties(); foreach (PropertyInfo mi in members) { if (!mi.Name.Contains("Specified") && DeserializeThis(mi)) { //sanity check. should not be null if (inobject == null) { return(null); } //If its a simple type, call DeserializeIntegral if (mi.PropertyType == typeof(string) || mi.PropertyType == typeof(int) || mi.PropertyType == typeof(float) || mi.PropertyType == typeof(bool) || mi.PropertyType == typeof(Scorm1_2.DateTime) || mi.PropertyType == typeof(Scorm1_2.TimeSpan) || mi.PropertyType == typeof(Scorm2004.DateTime) || mi.PropertyType == typeof(Scorm2004.TimeSpan) || mi.PropertyType == typeof(Decimal) || mi.PropertyType == typeof(System.Nullable <Decimal>) || mi.PropertyType.IsEnum ) { stack.Push(mi.Name); mi.SetValue(inobject, DeserializeIntegral(mi.GetValue(inobject, null), mi.PropertyType, wrapper), null); stack.Pop(); } else if (IsLangStringType(mi.PropertyType)) { stack.Push(mi.Name); mi.SetValue(inobject, DeSerializeLangString(mi.GetValue(inobject, null), mi.PropertyType, wrapper), null); stack.Pop(); } //Its a complex type, push its name on the stack and recurse else { stack.Push(mi.Name); mi.SetValue(inobject, DeserializeObject(mi.GetValue(inobject, null), mi.PropertyType, wrapper), null); stack.Pop(); } } } } //This object is an array - not that the clause above will create a blank array of the proper //length, so this should never be null else { //Loop over every entry in the array stack.Push("_count"); int length = wrapper.Get(GetCurrentIdentifier(), typeof(int)).ToInt(); stack.Pop(); int i = 0; Array arrray = null; MethodInfo[] methods232 = otype.GetMethods(); foreach (MethodInfo m in methods232) { if (m.Name == "ToArray") { arrray = (Array)m.Invoke(inobject, null); } } otype = arrray.GetType(); for (i = 0; i < length; i++) { //push the array index on the identifier name stack stack.Push(i.ToString()); //If it's a simple element, deserialize and set it on the array if (otype.GetElementType() == typeof(string) || otype.GetElementType() == typeof(int) || otype.GetElementType() == typeof(float) || otype.GetElementType() == typeof(bool) || otype.GetElementType() == typeof(Scorm2004.DateTime) || otype.GetElementType() == typeof(Scorm2004.TimeSpan) || otype.GetElementType() == typeof(Scorm1_2.DateTime) || otype.GetElementType() == typeof(Scorm1_2.TimeSpan) || otype.GetElementType() == typeof(Decimal) || otype.GetElementType().IsEnum ) { arrray.SetValue(DeserializeIntegral(arrray.GetValue(i), otype.GetElementType(), wrapper), i); } else if (IsLangStringType(otype.GetElementType())) { arrray.SetValue(DeSerializeLangString(arrray.GetValue(i), otype.GetElementType(), wrapper), i); } else { //Its a complex object, deserialize it an set it on the array inobject.GetType().GetMethod("Add").Invoke(inobject, new object[] { DeserializeObject(null, otype.GetElementType(), wrapper) }); } //pop the index off the identifier name stack stack.Pop(); } } //return the object - important because the inobject could have changed from null to something, and //the parent object must know that the value has been set return(inobject); }