carpics carpics - 28 days ago 9
C# Question

Nhibernate Interceptor - Get Property length OnSave

I have simple NHibernate interceptor and override method OnSave().

Now what I am trying to do here is to get SQL length for string properties.
Is that possible.

I can see that property

IType[]
types contains
SqlType
where
Length
is available, but just cannot find how to read it. Example from debug:

enter image description here

This is example of the code which I have, and where I am trying to get Sql length of property.

public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
for (int i = 0; i < propertyNames.Length; i++)
{
//If type is string
if (types[i].GetType() == typeof(NHibernate.Type.StringType))
{
//Get SQL length of string property
}
}

return false;
}


Any help how I can get this?

Answer

Let's just try to cast the IType into intended one:

//If type is string
var stringType = types[i] as NHibernate.Type.StringType;

//if (types[i].GetType() == typeof(NHibernate.Type.StringType))
if(stringType != null)
{
     //Get SQL length of string property   
     var length = stringType.SqlType.Length;
}