internal static void Initialize() { //ChoStreamProfile.Clean(ChoReservedDirectoryName.Others, ChoType.GetLogFileName(typeof(ChoTypesManager))); StringBuilder topMsg = new StringBuilder(); ChoStringMsgBuilder parseMethodsMsg = new ChoStringMsgBuilder("Below are the loaded parse methods"); ChoStringMsgBuilder formatMethodsMsg = new ChoStringMsgBuilder("Below are the loaded format methods"); ChoStringMsgBuilder msg = new ChoStringMsgBuilder("Below are the loaded type objects"); foreach (Type type in ChoType.GetTypes(typeof(ChoStringObjectFormattableAttribute))) { if (ChoTypesManagerSettings.IsExcludedType(type)) { continue; } ChoTypeObjectParseInfo typeObjectParseInfo = new ChoTypeObjectParseInfo(); ChoTypeObjectFormatInfo typeObjectFormatInfo = new ChoTypeObjectFormatInfo(); try { //typeObjectFormatInfo.TypeObject = typeObjectParseInfo.TypeObject = ChoObject.CreateInstance(type); msg.AppendFormatLine(type.FullName); } catch (ChoFatalApplicationException) { throw; } catch (Exception ex) { msg.AppendFormatLine("{0}: [{1}]", type.FullName, ex.Message); } LoadObjectParser(parseMethodsMsg, type, typeObjectParseInfo); LoadObjectFormatter(formatMethodsMsg, type, typeObjectFormatInfo); } _typeObjectsParseInfoArr = TypeObjectsParseInfo.Values.ToArray(); _typeObjectsFormatInfoArr = TypeObjectsFormatInfo.Values.ToArray(); //_typeObjectsParseInfo.Clear(); //_typeObjectsFormatInfo.Clear(); topMsg.Append(parseMethodsMsg.ToString() + Environment.NewLine + Environment.NewLine); topMsg.Append(formatMethodsMsg.ToString() + Environment.NewLine + Environment.NewLine); topMsg.Append(msg.ToString() + Environment.NewLine + Environment.NewLine); _helpText = topMsg.ToString(); }
private static void LoadObjectFormatter(ChoStringMsgBuilder formatMethodsMsg, Type type, ChoTypeObjectFormatInfo typeObjectFormatInfo) { try { MethodInfo isFormatMethodInfo = ChoType.GetMethod(type, typeof(ChoCanObjectFormattableAttribute), true); if (isFormatMethodInfo != null && isFormatMethodInfo.IsStatic) { if (isFormatMethodInfo.ReturnParameter == null || isFormatMethodInfo.ReturnParameter.ParameterType != typeof(bool)) { throw new ChoApplicationException(String.Format("{0}: Incorrect Check Format routine signature found. It should have bool return parameter.", type.Name)); } ParameterInfo[] parameters = isFormatMethodInfo.GetParameters(); if (parameters == null || parameters.Length != 1 || parameters[0].ParameterType != typeof(object)) { throw new ChoApplicationException(String.Format("{0}: Incorrect Check Format routine signature found. It should have one and only input object parameter.", type.Name)); } MethodInfo formatMethodInfo = ChoType.GetMethod(type, typeof(ChoObjectFormatterAttribute), true); if (formatMethodInfo != null) { parameters = formatMethodInfo.GetParameters(); if (formatMethodInfo.ReturnParameter == null || formatMethodInfo.ReturnParameter.ParameterType != typeof(string)) { throw new ChoApplicationException(String.Format("{0}: Incorrect Format routine signature found. It should have string return parameter.", type.Name)); } if (parameters == null || parameters.Length != 2 || parameters[0].ParameterType != typeof(object) || parameters[1].ParameterType != typeof(string)) { throw new ChoApplicationException(String.Format("{0}: Incorrect Format routine signature found. It should have two input parameters of types [object, string].", type.Name)); } typeObjectFormatInfo.CanFormat = isFormatMethodInfo.CreateDelegate <Func <object, bool> >(); typeObjectFormatInfo.Format = formatMethodInfo.CreateDelegate <Func <object, string, string> >(); formatMethodsMsg.AppendFormatLine(type.FullName); } } } catch (ChoFatalApplicationException) { throw; } catch (Exception ex) { formatMethodsMsg.AppendFormatLine("{0}: [{1}]", type.FullName, ex.Message); } if (typeObjectFormatInfo.IsValid()) { _typeObjectsFormatInfo.Add(typeObjectFormatInfo); } }