public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (value as EnumFlagsCTD != null && destinationType.Equals(typeof(string))) { try { EnumFlagsCTD ctd = (EnumFlagsCTD)value; string name = ctd.field.Split(' ').Length == 1 ? ctd.field : ctd.field.Split(new char[] { ' ' }, 2)[1].Trim(); TypedValue tv = ctd.owner[name]; return("0x" + Enum.Format(tv.Type, tv.Value, "X")); } //catch { } catch (Exception ex) { throw new NotSupportedException("Invalid data", ex); } } return(base.ConvertTo(context, culture, value, destinationType)); }
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { EnumFlagsCTD ctd = (EnumFlagsCTD)value; string name = ctd.field.Split(' ').Length == 1 ? ctd.field : ctd.field.Split(new char[] { ' ' }, 2)[1].Trim(); Type enumType = AApiVersionedFields.GetContentFieldTypes(0, ctd.owner.GetType())[name]; int bits = underlyingTypeToBits(Enum.GetUnderlyingType(enumType)); EnumFlagPropertyDescriptor[] enumFlags = new EnumFlagPropertyDescriptor[bits]; string fmt = "[{0:X" + bits.ToString("X").Length + "}] "; for (int i = 0; i < bits; i++) { ulong u = (ulong)1 << i; string s = (Enum)Enum.ToObject(enumType, u) + ""; if (s == u.ToString()) { s = "-"; } s = String.Format(fmt, i) + s; enumFlags[i] = new EnumFlagPropertyDescriptor(ctd.owner, ctd.field, u, s); } return(new PropertyDescriptorCollection(enumFlags)); }