I always wondered why someone should provide a length attribute for string properties in mapping files. Is it used by nhibernate anyplace?
Anyway I wrote this little function someone can find usefull. If you have any better ways to do that please let me know (I checked the GetClassMetadata function but I don't think it holds that info.)
Code:
RETRIEVING NOT-NULL AND LENGTH ATTRIBUTES
Assembly assembly = this.GetType().Assembly;
string[] resourceNames = assembly.GetManifestResourceNames();
foreach (string resourceName in resourceNames)
{
if (resourceName.Contains(this.GetType().Name + ".hbm.xml"))
{
xmlMapping = new XmlDocument();
xmlMapping.Load(assembly.GetManifestResourceStream(resourceName));
}
}
string xpath = "/urn:hibernate-mapping/urn:class/urn:property[@name='{0}']/urn:column";
XmlNamespaceManager nsmgr = new XmlNamespaceManager(this.xmlMapping.NameTable);
nsmgr.AddNamespace("urn", "urn:nhibernate-mapping-2.0");
XmlNode node = this.xmlMapping.SelectSingleNode(String.Format(xpath, property.Name), nsmgr);
int itemp; bool btemp;
if (Int32.TryParse(node.Attributes["length"].Value, out itemp))
{
length = itemp;
}
if (Boolean.TryParse(node.Attributes["not-null"].Value, out btemp))
{
notNull = btemp;
}