//static public int GetEnumValueIndex(System.Type enumType, string valueName) //{ // string[] names = System.Enum.GetNames(enumType); // for (int i = 0; i < names.Length; i++) // { // if (names[i] == valueName) // { // return i; // } // } // throw new Exception("Invalid enum value name passed in."); //} /// <summary> /// Loads all the assemblies in the directory of the executing (entry) assembly and searches /// them for inheritors of the given type. SLOW! /// </summary> /// <returns></returns> //static public List<Type> GetCollectTypeChildrenTypesFromRelatedAssemblies(Type typeSearched) //{ // List<Type> result = new List<Type>(); // // Load all the assemblies in the directory of the current application and try to find // // inheritors of AIndividual in them, then gather those in the list. // string path = Assembly.GetEntryAssembly().Location; // path = path.Remove(path.LastIndexOf('\\')); // string[] dllFiles = System.IO.Directory.GetFiles(path, "*.dll"); // foreach (string file in dllFiles) // { // Assembly assembly; // try // { // assembly = Assembly.LoadFile(file); // } // catch (Exception) // {// This DLL was not a proper assembly, disregard. // continue; // } // // Try to find typeSearched inheritors in this assembly. // result.AddRange(ReflectionSupport.GetTypeChildrenTypes(typeSearched, assembly)); // } // return result; //} /// <summary> /// Helper method allows to retrieve application entry assembly referenced (static and runtime) assemblies. /// </summary> static public List <Assembly> GetAssemblies(bool entryAssembly, bool entryReferencedAssemblies) { Assembly startAssembly = Assembly.GetEntryAssembly(); if (startAssembly == null) { startAssembly = Assembly.GetCallingAssembly(); CoreSystemMonitor.OperationWarning("Failed to find application entry assembly, operating with reduced set."); } return(GetReferencedAndInitialAssembly(startAssembly)); }
/// <summary> /// Helper. /// </summary> /// <param name="dateTime"></param> /// <param name="timeZoneId"></param> /// <returns></returns> private static DateTime ConvertToLocalDateTime(string dateTime, string timeZoneId) { // Strip the time zone ID from the end of the dateTime string. dateTime = dateTime.Replace(timeZoneId, "").Trim(); // Convert the timeZoneId to a TimeSpan. // (Leading + signs aren't allowed in the TimeSpan.Parse // parameter, although leading - signs are. // The purpose of the [+]*? at the beginning of the // regex is to account for, and ignore, any leading + sign). string ts = Regex.Replace(GetTimeZoneOffset(timeZoneId), @"^[+]*?(?<hours>[-]?\d\d)(?<minutes>\d\d)$", "${hours}:${minutes}:00"); TimeSpan timeZoneOffset = TimeSpan.Parse(ts); TimeSpan localUtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now); // Get the absolute time difference between the given // datetime's time zone and the local datetime's time zone. TimeSpan absoluteOffset = timeZoneOffset - localUtcOffset; absoluteOffset = absoluteOffset.Duration(); // Now that the absolute time difference is known, // determine whether to add or subtract it from the // given dateTime, and then return the result. try { if (timeZoneOffset < localUtcOffset) { return(DateTime.Parse(dateTime) + absoluteOffset); } else { return(DateTime.Parse(dateTime) - absoluteOffset); } } catch { CoreSystemMonitor.OperationWarning("Parsing of date time [" + dateTime + "] failed."); return(DateTime.MinValue); } }
/// <summary> /// Collect them from a given assembly. Also works for interfaces. /// </summary> static public List <Type> GetTypeChildrenTypes(Type typeSearched, System.Reflection.Assembly assembly) { List <Type> result = new List <Type>(); Type[] types; try { types = assembly.GetTypes(); } catch (ReflectionTypeLoadException ex) { string message = string.Empty; foreach (Exception subEx in ex.LoaderExceptions) { message += "{" + CommonHelper.GetExceptionMessage(subEx) + "}"; } CoreSystemMonitor.OperationError("Failed to load assembly types for [" + typeSearched.Name + ", " + assembly.GetName().Name + "] [" + CommonHelper.GetExceptionMessage(ex) + ", " + message + "]."); return(result); } catch (Exception ex) { CoreSystemMonitor.OperationError("Failed to load assembly types for [" + typeSearched.Name + ", " + assembly.GetName().Name + "] [" + CommonHelper.GetExceptionMessage(ex) + "]."); return(result); } foreach (Type type in types) { if (typeSearched.IsInterface) { List <Type> interfaces = new List <Type>(type.GetInterfaces()); if (interfaces.Contains(typeSearched)) { result.Add(type); } } else if (type.IsSubclassOf(typeSearched)) { result.Add(type); } } return(result); }
/// <summary> /// This allows parsing with taking TimeZones into the string in consideration. /// </summary> public static DateTime ParseDateTimeWithZone(string dateTime) { DateTime result = DateTime.MinValue; try { dateTime = dateTime.Trim(); if (string.IsNullOrEmpty(dateTime)) { return(result); } if (dateTime.Contains("/")) {// Some date time formats contain 2 zones like "Etc/GMT" - fix that here. dateTime = dateTime.Substring(0, dateTime.IndexOf("/")); } if (dateTime.Contains(".")) {// Some date time formats contain a dot, clear it. dateTime = dateTime.Replace(".", ""); } if (dateTime.Contains("Thur")) {// Thur occurs in some feeds and is not recognized. dateTime = dateTime.Replace("Thur", ""); } // The dateTime parameter is either local (no time // zone specified), or it has a time zone. // Use the regex to examine the last part of the // dateTime string and determine which category it falls into. Match m = Regex.Match(dateTime.Trim(), @"(\b\w{3,4}|[+-]?\d{4})$"); //Match m = Regex.Match(dateTime.Trim(), @"(\b\w{3,4})$"); if (m.Value == DateTime.Now.Year.ToString() || m.Value == (DateTime.Now.Year - 1).ToString() || m.Value == (DateTime.Now.Year - 2).ToString() || m.Value == (DateTime.Now.Year - 3).ToString()) {// Sometimes the year is passed this way and the algo confuses it with a timizing zone. m = null; } if (m == null || m.Length == 0) { //result = DateTime.Parse(dateTime); if (DateTimeHelper.TryParse(dateTime, DateTimeHelper.DateTimeFormat.USA_DATE, out result) == false) { result = DateTime.MinValue; } } else { // Date w/ time zone. m.Value holds the time zone info // (either a time zone name (e.g. PST), or the // numeric offset (e.g. -0800). result = ConvertToLocalDateTime(dateTime, m.Value); } } catch (Exception ex) { CoreSystemMonitor.OperationError("Failed to parse time.", ex); } return(result); }