- nettiergeneratorsample.zip
- database
- sample
- BusinessLogic
- Common
- Config
- DalFactory
- GUI
- WinGUI
- IDAL
- Model
- MsSqlDal
- OP_CACHE.ATR
- OP_CACHE.IDX
- Sample.sln
- Security
- TierModel
- Tools
- EnterpriseLibrary
- Microsoft.Practices.EnterpriseLibrary.Caching.dll
- Microsoft.Practices.EnterpriseLibrary.Caching.XML
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.EnterpriseLibrary.Common.XML
- Microsoft.Practices.ObjectBuilder.dll
- Microsoft.Practices.ObjectBuilder.XML
- TierGenerator
- nettiergenerator.zip
- netTierGenerator
- BusinessLogic
- Common
- Common.csproj
- Data
- Exception
- OP_CACHE.ATR
- OP_CACHE.IDX
- Properties
- Util
- Config
- Console
- DalFactory
- IDal
- Model
- MsSqlDal
- netTierGenerator.sln
- OP_CACHE.ATR
- OP_CACHE.IDX
- TierGenerator
- TierModel
|
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using Sample.Common.Util;
namespace Sample.Common.History
{
public static class HistorySerializer
{
#region const
private const string SERIALIZE_DELETE = "<item type=\"{0}\"><prop name=\"Id\" type=\"{1}\">{2}</prop><prop name=\"WasDeleted\" type=\"System.Boolean\">True</prop></item>";
#endregion const
public static string Serialize(object obj)
{
if (obj == null)
{
return String.Empty;
}
Type objType = obj.GetType();
if (objType.IsDefined(typeof(NonHistoryAttributeAttribute), true))
{
return String.Empty;
}
if (objType.IsPrimitive || objType.IsValueType || objType == typeof(String))
{
return String.Format(
"<item type=\"{0}\"><prop name=\"Value\" type=\"{0}\">{1}</prop></item>",
objType.FullName,
HistorySerializer.ConvertToString(objType, obj)
);
}
string entityType = String.Empty;
StringBuilder result = new StringBuilder();
PropertyInfo[] properties = objType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
result.AppendFormat("<item type=\"{0}\">", objType.FullName);
for (int i = 0; i < properties.Length; i++)
{
PropertyInfo property = properties[i];
if (property.Name == "IsDirty")
{
continue;
}
if (property.IsDefined(typeof(NonHistoryAttributeAttribute), true))
{
continue;
}
entityType = String.Empty;
if (property.IsDefined(typeof(HistoryAttributeAttribute), true))
{
HistoryAttributeAttribute[] hattrs = property.GetCustomAttributes(typeof(HistoryAttributeAttribute), true) as HistoryAttributeAttribute[];
if (hattrs[0].EntityType != null)
{
entityType = String.Format(" entityType=\"{0}\"", hattrs[0].EntityType.FullName);
}
}
if (property.PropertyType.IsPrimitive || property.PropertyType.IsValueType || property.PropertyType == typeof(String))
{
object value = property.GetValue(obj, null);
result.AppendFormat(
"<prop name=\"{0}\" type=\"{1}\"{2}>{3}</prop>",
property.Name,
property.PropertyType,
entityType,
HistorySerializer.ConvertToString(property.PropertyType, value)
);
}
}
result.Append("</item>");
return result.ToString();
}
public static string SerializeForDelete(Type objType, Guid id)
{
return String.Format(
HistorySerializer.SERIALIZE_DELETE,
objType.FullName,
typeof(Guid).FullName,
id
);
}
public static HistoryDeserializedInfo Deserialize(string xml)
{
HistoryDeserializedInfo result = new HistoryDeserializedInfo();
if (String.IsNullOrEmpty(xml))
{
return result;
}
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
if (reader.Read())
{
reader.MoveToAttribute("type");
result.Type = reader.ReadContentAsString();
reader.ReadStartElement();
while (reader.IsStartElement())
{
reader.MoveToElement();
string element = reader.Name;
reader.MoveToAttribute("name");
string name = reader.ReadContentAsString();
reader.MoveToAttribute("type");
string type = reader.ReadContentAsString();
reader.MoveToAttribute("entityType");
string entityType = (String.Compare(reader.Name, "entityType", StringComparison.InvariantCultureIgnoreCase) == 0) ? reader.ReadContentAsString() : String.Empty;
reader.MoveToContent();
string value = reader.ReadElementContentAsString();
switch (element)
{
case "prop": // property
result[name] = new HistoryDeserializedInfoMember(type, entityType, HistorySerializer.XmlDecode(value));
break;
}
}
}
}
return result;
}
private static string XmlDecode(string xml)
{
if (String.IsNullOrEmpty(xml))
{
return String.Empty;
}
return StringUtil.XmlDecode(xml);
}
private static string XmlEncode(string text)
{
if (String.IsNullOrEmpty(text))
{
return String.Empty;
}
return StringUtil.XmlEncode(text);
}
private static string ConvertToString(Type type, object obj)
{
if (type == typeof(String))
{
return HistorySerializer.XmlEncode(obj as string);
}
else if (type == typeof(DateTime))
{
return XmlConvert.ToString((DateTime)obj, XmlDateTimeSerializationMode.Local);
}
else
{
return Convert.ToString(obj, CultureInfo.InvariantCulture);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.