// Sort fields by field offsets. public int CompareTo(object other) { FieldInfoEx otherF = other as FieldInfoEx; if (otherF == null) return -1; return Offset.CompareTo(otherF.Offset); }
internal BinaryOrderedUdtNormalizer(Type t, bool isTopLevelUdt) { _skipNormalize = false; if (_skipNormalize) { // if skipping normalization, dont write the null // byte header for IsNull _isTopLevelUdt = true; } _isTopLevelUdt = true; // get all the fields FieldInfo[] fields = GetFields(t); FieldsToNormalize = new FieldInfoEx[fields.Length]; int i = 0; foreach (FieldInfo fi in fields) { int offset = Marshal.OffsetOf(fi.DeclaringType, fi.Name).ToInt32(); FieldsToNormalize[i++] = new FieldInfoEx(fi, offset, GetNormalizer(fi.FieldType)); } //sort by offset Array.Sort(FieldsToNormalize); //if this is not a top-level udt, do setup for null values. //null values need to compare less than all other values, //so prefix a null byte indicator. if (!_isTopLevelUdt) { //get the null value for this type, special case for sql types, which //have a null field if (typeof(INullable).IsAssignableFrom(t)) { PropertyInfo pi = t.GetProperty("Null", BindingFlags.Public | BindingFlags.Static); if (pi == null || pi.PropertyType != t) { FieldInfo fi = t.GetField("Null", BindingFlags.Public | BindingFlags.Static); if (fi == null || fi.FieldType != t) { throw new Exception("could not find Null field/property in nullable type " + t); } else { NullInstance = fi.GetValue(null); } } else { NullInstance = pi.GetValue(null, null); } //create the padding buffer _padBuffer = new byte[Size - 1]; } } }
// Sort fields by field offsets. public int CompareTo(object other) { FieldInfoEx otherF = other as FieldInfoEx; if (otherF == null) { return(-1); } return(this.offset.CompareTo(otherF.offset)); }