はい、分かりました!リッポーの助けを借りて!
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;
namespace MCC.Common.DL.BaseObjects
{
public class SerializableUserType<originalType> : IUserType
{
public SqlType[] SqlTypes
{
get
{
SqlType[] types = new SqlType[1];
types[0] = new SqlType(DbType.String);
return types;
}
}
public System.Type ReturnedType
{
get { return typeof(originalType); }
}
public new bool Equals(object x, object y)
{
if (x == null)
{
return false;
}
else
{
return x.Equals(y);
}
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
string txt = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
return StringSerializer<originalType>.DeSerialize(txt);
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null)
{
NHibernateUtil.String.NullSafeSet(cmd, null, index);
return;
}
var wrt = StringSerializer<originalType>.Serialize((originalType)value);
NHibernateUtil.String.NullSafeSet(cmd, wrt, index);
}
public object DeepCopy(object value)
{
if (value == null) return null;
return StringSerializer<originalType>.DeSerialize(StringSerializer<originalType>.Serialize((originalType)value));
}
public bool IsMutable
{
get { return false; }
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return cached;
}
public object Disassemble(object value)
{
return value;
}
}
}
これはFluentで次のように使用します:
Map(x => x.ScriptedTagScript).CustomType(typeof(SerializableUserType<ObservableDictionary<string, string>>));
そして、このヘルパー クラスが必要です。
public class StringSerializer<T>
{
public static string Serialize(T obj)
{
if (obj == null)
return string.Empty;
// XML-Serialisieren in String
XmlSerializer serializer = new XmlSerializer(obj.GetType());
// Serialisieren in MemoryStream
MemoryStream ms = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(ms, settings);
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
serializer.Serialize(writer, obj, namespaces);
// Stream in String umwandeln
StreamReader r = new StreamReader(ms);
r.BaseStream.Seek(0, SeekOrigin.Begin);
return r.ReadToEnd();
}
public static T DeSerialize(string txt)
{
T retVal = default(T);
if (string.IsNullOrEmpty(txt))
{
return retVal;
}
try
{
XmlSerializer ser = new XmlSerializer(typeof (T));
StringReader stringReader = new StringReader(txt);
XmlTextReader xmlReader = new XmlTextReader(stringReader);
retVal = (T) ser.Deserialize(xmlReader);
xmlReader.Close();
stringReader.Close();
}
catch (Exception)
{
}
return retVal;
}
}