Ejemplo n.º 1
0
        private static void LoadObjectParser(ChoStringMsgBuilder parseMethodsMsg, Type type, ChoTypeObjectParseInfo typeObjectParseInfo)
        {
            try
            {
                MethodInfo isParseMethodInfo = ChoType.GetMethod(type, typeof(ChoIsStringToObjectConvertable), true);
                if (isParseMethodInfo != null && isParseMethodInfo.IsStatic)
                {
                    if (isParseMethodInfo.ReturnParameter == null ||
                        isParseMethodInfo.ReturnParameter.ParameterType != typeof(bool))
                    {
                        throw new ChoApplicationException(String.Format("{0}: Incorrect Check Parse routine signature found. It should have bool return parameter.", type.Name));
                    }

                    ParameterInfo[] parameters = isParseMethodInfo.GetParameters();
                    if (parameters == null ||
                        parameters.Length != 1 ||
                        parameters[0].ParameterType != typeof(string))
                    {
                        throw new ChoApplicationException(String.Format("{0}: Incorrect Check Parse routine signature found. It should have one and only input string parameter.", type.Name));
                    }

                    MethodInfo parseMethodInfo = ChoType.GetMethod(type, typeof(ChoStringToObjectConverterAttribute), true);

                    if (parseMethodInfo != null)
                    {
                        parameters = parseMethodInfo.GetParameters();
                        if (parameters == null ||
                            parameters.Length != 1 ||
                            parameters[0].ParameterType != typeof(string))
                        {
                            throw new ChoApplicationException(String.Format("{0}: Incorrect Parse routine signature found. It should have one and only input string parameter.", type.Name));
                        }

                        typeObjectParseInfo.CheckParse = isParseMethodInfo.CreateDelegate <Func <string, bool> >();
                        typeObjectParseInfo.Parse      = parseMethodInfo.CreateDelegate <Func <string, object> >();

                        parseMethodsMsg.AppendFormatLine(type.FullName);
                    }
                }
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                parseMethodsMsg.AppendFormatLine("{0}: [{1}]", type.FullName, ex.Message);
            }

            if (typeObjectParseInfo.IsValid())
            {
                _typeObjectsParseInfo.Add(typeObjectParseInfo);
            }
        }
Ejemplo n.º 2
0
        private static void LoadObjectParser(ChoStringMsgBuilder parseMethodsMsg, Type type, ChoTypeObjectParseInfo typeObjectParseInfo)
        {
            ChoStringObjectFormattableAttribute attr = ChoType.GetAttribute <ChoStringObjectFormattableAttribute>(type);

            try
            {
                MethodInfo isParseMethodInfo = ChoType.GetMethod(type, typeof(ChoIsStringToObjectConvertable), true);
                if (isParseMethodInfo != null && isParseMethodInfo.IsStatic)
                {
                    if (isParseMethodInfo.ReturnParameter == null ||
                        isParseMethodInfo.ReturnParameter.ParameterType != typeof(bool))
                    {
                        throw new ChoApplicationException(String.Format("{0}: Incorrect Check Parse routine signature found. It should have bool return parameter.", type.Name));
                    }

                    ParameterInfo[] parameters = isParseMethodInfo.GetParameters();
                    if (parameters == null ||
                        parameters.Length != 1 ||
                        parameters[0].ParameterType != typeof(string))
                    {
                        throw new ChoApplicationException(String.Format("{0}: Incorrect Check Parse routine signature found. It should have one and only input string parameter.", type.Name));
                    }

                    MethodInfo parseMethodInfo = ChoType.GetMethod(type, typeof(ChoStringToObjectConverterAttribute), true);

                    if (parseMethodInfo != null)
                    {
                        parameters = parseMethodInfo.GetParameters();
                        if (parameters == null ||
                            parameters.Length != 1 ||
                            parameters[0].ParameterType != typeof(string))
                        {
                            throw new ChoApplicationException(String.Format("{0}: Incorrect Parse routine signature found. It should have one and only input string parameter.", type.Name));
                        }

                        typeObjectParseInfo.CheckParse = isParseMethodInfo.CreateDelegate <Func <string, bool> >();
                        typeObjectParseInfo.Parse      = parseMethodInfo.CreateDelegate <Func <string, object> >();

                        parseMethodsMsg.AppendFormatLine("{0} [EX: {1}]", type.FullName, ((IChoStringObjectFormatter)Activator.CreateInstance(type)).GetHelpText());
                    }
                }
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                parseMethodsMsg.AppendFormatLine("{0}: [{1}]", type.FullName, ex.Message);
            }

            Type type1 = attr.SupportedType == null?type.GetType() : attr.SupportedType;

            if (typeObjectParseInfo.IsValid())
            {
                if (TypeObjectsParseInfo.ContainsKey(type1))
                {
                    TypeObjectsParseInfo[type1] = typeObjectParseInfo;
                }
                else
                {
                    TypeObjectsParseInfo.Add(type1, typeObjectParseInfo);
                }
            }
        }