} // end Get(Type) /// <summary> /// Get the caption associated with a field. /// </summary> /// <param name="oFldInfo"></param> /// <returns></returns> public static String Get(FieldInfo oFldInfo) { if (null == oFldInfo) { return(""); } // 2013124 CDN - check for 'overrides' (set by the Set() method on a FieldInfo): string fname = oFldInfo.Name; Type deftype = oFldInfo.DeclaringType; string fullname = deftype.FullName + "." + fname; if (m_oFieldCaptionLookup.ContainsKey(fullname)) { return(m_oFieldCaptionLookup[fullname]); } object[] atts = oFldInfo.GetCustomAttributes(typeof(CaptionAttribute), false); if ((null == atts) || (0 == atts.Length)) { return(oFldInfo.Name); } CaptionAttribute catt = atts[0] as CaptionAttribute; if (null == catt) { return(oFldInfo.Name); } return(catt.Caption); }
public static String Get(Type oType) { if (null == oType) { return(""); } object[] atts = oType.GetCustomAttributes(typeof(CaptionAttribute), false); if ((null == atts) || (0 == atts.Length)) { return(oType.Name); } CaptionAttribute attrib_instance = atts[0] as CaptionAttribute; if (null == attrib_instance) { return(oType.Name); } return(attrib_instance.Caption); }
} // end Get(Type) /// <summary> /// Get the caption attribute for a type. /// </summary> /// <param name="oInfo"></param> /// <returns></returns> public static String Get(ParameterInfo oInfo) { if (null == oInfo) { return(""); } object[] atts = oInfo.GetCustomAttributes(typeof(CaptionAttribute), false); if ((null == atts) || (0 == atts.Length)) { return(oInfo.Name); } CaptionAttribute catt = atts[0] as CaptionAttribute; if (null == catt) { return(oInfo.Name); } return(catt.Caption); } // end Get(Type)
/// <summary> /// Allows saving stuff to CSV with the option to append. /// </summary> /// <param name="sCsvFile"></param> /// <param name="oStuffToSave"></param> /// <param name="bAppend"></param> /// <returns></returns> public static bool SaveAsCsv(String sCsvFile, IEnumerable oStuffToSave, bool bAppend) { String fn = MethodBase.GetCurrentMethod().Name; StreamWriter sw = null; try { // Get the 0'th element and errorcheck: Object obj0 = null; foreach (object obj in oStuffToSave) { obj0 = obj; break; } if (null == obj0) { return(Util.HandleAppErr(typeof(PersisterAscii), fn, "Nothing to save")); } // Set up field list and header record: Type type = obj0.GetType(); List <FieldInfo> fields = new List <FieldInfo>(); StringBuilder sb = new StringBuilder(); int nfields = 0; foreach (FieldInfo fi in type.GetFields()) { if (!fi.IsPublic) { continue; } if (fi.IsStatic) { continue; } if (!Util.IsScalarType(fi.FieldType)) { continue; } fields.Add(fi); if (nfields > 0) { sb.Append(","); } sb.Append(Util.Csvify(CaptionAttribute.Get(fi))); nfields++; } // end foreach(field) // Open file and write header record (if file didn't previously exist): bool previously_existed = File.Exists(sCsvFile); sw = new StreamWriter(sCsvFile, bAppend); if (!previously_existed) { sw.WriteLine(sb.ToString()); } // Write data records: foreach (Object obj in oStuffToSave) { sb.Clear(); nfields = 0; foreach (FieldInfo fi in fields) { object val = fi.GetValue(obj); if (nfields > 0) { sb.Append(","); } sb.Append(Util.Csvify(val)); nfields++; } // end foreach(field) sw.WriteLine(sb.ToString()); } // end foreach(object) // We're done (close the file in the finally block): return(true); } // end main try catch (Exception exc) { Util.HandleExc(typeof(PersisterAscii), fn, exc); return(false); } finally { if (null != sw) { sw.Close(); } } }