internal static bool ParseTypeName(string inputTypeName, ParseTypeNameLanguage parseTypeNameLanguage, out string typeName, out string[] parameters, out string elemantDecorator)
        {
            typeName = string.Empty;
            parameters = null;
            elemantDecorator = string.Empty;

            // replace all language specific array\generic chars with the net-framework's representation
            if (parseTypeNameLanguage == ParseTypeNameLanguage.VB)
                inputTypeName = inputTypeName.Replace('(', '[').Replace(')', ']');
            else if (parseTypeNameLanguage == ParseTypeNameLanguage.CSharp)
                inputTypeName = inputTypeName.Replace('<', '[').Replace('>', ']');

            int endIndex = inputTypeName.LastIndexOfAny(new char[] { ']', '&', '*' });
            if (endIndex == -1)
            {
                // "simple" type
                typeName = inputTypeName;
            }
            else if (inputTypeName[endIndex] == ']') //array or generic
            {
                int startIndex = endIndex;
                int nestLevel = 1;
                while ((startIndex > 0) && (nestLevel > 0))
                {
                    startIndex--;
                    if (inputTypeName[startIndex] == ']')
                        nestLevel++;
                    else if (inputTypeName[startIndex] == '[')
                        nestLevel--;
                }
                if (nestLevel != 0)
                    return false;

                typeName = inputTypeName.Substring(0, startIndex) + inputTypeName.Substring(endIndex + 1);

                string bracketContent = inputTypeName.Substring(startIndex + 1, endIndex - startIndex - 1).Trim();
                if ((bracketContent == String.Empty) || (bracketContent.TrimStart()[0] == ','))
                {
                    // array
                    elemantDecorator = "[" + bracketContent + "]";
                }
                else
                {
                    // Isolate the parameters (looking for commas alone will not cover cases
                    // when parameters are multi-dim array or generics...
                    int nestingLevel = 0;
                    char[] genericParamChars = bracketContent.ToCharArray();
                    for (int loop = 0; loop < genericParamChars.Length; loop++)
                    {
                        if (genericParamChars[loop] == '[')
                            nestingLevel++;
                        else if (genericParamChars[loop] == ']')
                            nestingLevel--;
                        else if ((genericParamChars[loop] == ',') && (nestingLevel == 0))
                            genericParamChars[loop] = '$';
                    }
                    // split to get the list of generic arguments
                    parameters = new string(genericParamChars).Split(new char[] { '$' });

                    // clean the parameters
                    for (int loop = 0; loop < parameters.Length; loop++)
                    {
                        parameters[loop] = parameters[loop].Trim();

                        // remove extra brackects if exist
                        if (parameters[loop][0] == '[')
                            parameters[loop] = parameters[loop].Substring(1, parameters[loop].Length - 2);

                        // remove the "Of " keyword form VB parameters
                        if ((parseTypeNameLanguage == ParseTypeNameLanguage.VB) && (parameters[loop].StartsWith("Of ", StringComparison.OrdinalIgnoreCase)))
                            parameters[loop] = parameters[loop].Substring(3).TrimStart();
                    }
                }
            }
            else // byref, pointer
            {
                typeName = inputTypeName.Substring(0, endIndex) + inputTypeName.Substring(endIndex + 1);
                elemantDecorator = inputTypeName.Substring(endIndex, 1);
            }

            //Work around: we need to account for these langugue keywords and provide the correct type for them.
            //      A tighter way to achieve this should be found.
            if ((parseTypeNameLanguage == ParseTypeNameLanguage.CSharp) && CSKeywords.Contains(typeName))
                typeName = DotNetKeywords[CSKeywords.IndexOf(typeName)];
            else if ((parseTypeNameLanguage == ParseTypeNameLanguage.VB) && VBKeywords.Contains(typeName))
                typeName = DotNetKeywords[VBKeywords.IndexOf(typeName)];

            return true;
        }
Exemple #2
0
        internal static bool ParseTypeName(string inputTypeName, ParseTypeNameLanguage parseTypeNameLanguage, out string typeName, out string[] parameters, out string elemantDecorator)
        {
            typeName         = string.Empty;
            parameters       = null;
            elemantDecorator = string.Empty;

            // replace all language specific array\generic chars with the net-framework's representation
            if (parseTypeNameLanguage == ParseTypeNameLanguage.VB)
            {
                inputTypeName = inputTypeName.Replace('(', '[').Replace(')', ']');
            }
            else if (parseTypeNameLanguage == ParseTypeNameLanguage.CSharp)
            {
                inputTypeName = inputTypeName.Replace('<', '[').Replace('>', ']');
            }

            int endIndex = inputTypeName.LastIndexOfAny(new char[] { ']', '&', '*' });

            if (endIndex == -1)
            {
                // "simple" type
                typeName = inputTypeName;
            }
            else if (inputTypeName[endIndex] == ']') //array or generic
            {
                int startIndex = endIndex;
                int nestLevel  = 1;
                while ((startIndex > 0) && (nestLevel > 0))
                {
                    startIndex--;
                    if (inputTypeName[startIndex] == ']')
                    {
                        nestLevel++;
                    }
                    else if (inputTypeName[startIndex] == '[')
                    {
                        nestLevel--;
                    }
                }
                if (nestLevel != 0)
                {
                    return(false);
                }

                typeName = inputTypeName.Substring(0, startIndex) + inputTypeName.Substring(endIndex + 1);

                string bracketContent = inputTypeName.Substring(startIndex + 1, endIndex - startIndex - 1).Trim();
                if ((bracketContent == String.Empty) || (bracketContent.TrimStart()[0] == ','))
                {
                    // array
                    elemantDecorator = "[" + bracketContent + "]";
                }
                else
                {
                    // Isolate the parameters (looking for commas alone will not cover cases
                    // when parameters are multi-dim array or generics...
                    int    nestingLevel      = 0;
                    char[] genericParamChars = bracketContent.ToCharArray();
                    for (int loop = 0; loop < genericParamChars.Length; loop++)
                    {
                        if (genericParamChars[loop] == '[')
                        {
                            nestingLevel++;
                        }
                        else if (genericParamChars[loop] == ']')
                        {
                            nestingLevel--;
                        }
                        else if ((genericParamChars[loop] == ',') && (nestingLevel == 0))
                        {
                            genericParamChars[loop] = '$';
                        }
                    }
                    // split to get the list of generic arguments
                    parameters = new string(genericParamChars).Split(new char[] { '$' });

                    // clean the parameters
                    for (int loop = 0; loop < parameters.Length; loop++)
                    {
                        parameters[loop] = parameters[loop].Trim();

                        // remove extra brackects if exist
                        if (parameters[loop][0] == '[')
                        {
                            parameters[loop] = parameters[loop].Substring(1, parameters[loop].Length - 2);
                        }

                        // remove the "Of " keyword form VB parameters
                        if ((parseTypeNameLanguage == ParseTypeNameLanguage.VB) && (parameters[loop].StartsWith("Of ", StringComparison.OrdinalIgnoreCase)))
                        {
                            parameters[loop] = parameters[loop].Substring(3).TrimStart();
                        }
                    }
                }
            }
            else // byref, pointer
            {
                typeName         = inputTypeName.Substring(0, endIndex) + inputTypeName.Substring(endIndex + 1);
                elemantDecorator = inputTypeName.Substring(endIndex, 1);
            }

            //Work around: we need to account for these langugue keywords and provide the correct type for them.
            //      A tighter way to achieve this should be found.
            if ((parseTypeNameLanguage == ParseTypeNameLanguage.CSharp) && CSKeywords.Contains(typeName))
            {
                typeName = DotNetKeywords[CSKeywords.IndexOf(typeName)];
            }
            else if ((parseTypeNameLanguage == ParseTypeNameLanguage.VB) && VBKeywords.Contains(typeName))
            {
                typeName = DotNetKeywords[VBKeywords.IndexOf(typeName)];
            }

            return(true);
        }
        internal static bool ParseTypeName(string inputTypeName, ParseTypeNameLanguage parseTypeNameLanguage, out string typeName, out string[] parameters, out string elemantDecorator)
        {
            typeName         = string.Empty;
            parameters       = null;
            elemantDecorator = string.Empty;
            if (parseTypeNameLanguage == ParseTypeNameLanguage.VB)
            {
                inputTypeName = inputTypeName.Replace('(', '[').Replace(')', ']');
            }
            else if (parseTypeNameLanguage == ParseTypeNameLanguage.CSharp)
            {
                inputTypeName = inputTypeName.Replace('<', '[').Replace('>', ']');
            }
            int length = inputTypeName.LastIndexOfAny(new char[] { ']', '&', '*' });

            if (length == -1)
            {
                typeName = inputTypeName;
            }
            else if (inputTypeName[length] == ']')
            {
                int num2 = length;
                int num3 = 1;
                while ((num2 > 0) && (num3 > 0))
                {
                    num2--;
                    if (inputTypeName[num2] == ']')
                    {
                        num3++;
                    }
                    else if (inputTypeName[num2] == '[')
                    {
                        num3--;
                    }
                }
                if (num3 != 0)
                {
                    return(false);
                }
                typeName = inputTypeName.Substring(0, num2) + inputTypeName.Substring(length + 1);
                string str = inputTypeName.Substring(num2 + 1, (length - num2) - 1).Trim();
                if ((str == string.Empty) || (str.TrimStart(new char[0])[0] == ','))
                {
                    elemantDecorator = "[" + str + "]";
                }
                else
                {
                    int    num4    = 0;
                    char[] chArray = str.ToCharArray();
                    for (int i = 0; i < chArray.Length; i++)
                    {
                        if (chArray[i] == '[')
                        {
                            num4++;
                        }
                        else if (chArray[i] == ']')
                        {
                            num4--;
                        }
                        else if ((chArray[i] == ',') && (num4 == 0))
                        {
                            chArray[i] = '$';
                        }
                    }
                    parameters = new string(chArray).Split(new char[] { '$' });
                    for (int j = 0; j < parameters.Length; j++)
                    {
                        parameters[j] = parameters[j].Trim();
                        if (parameters[j][0] == '[')
                        {
                            parameters[j] = parameters[j].Substring(1, parameters[j].Length - 2);
                        }
                        if ((parseTypeNameLanguage == ParseTypeNameLanguage.VB) && parameters[j].StartsWith("Of ", StringComparison.OrdinalIgnoreCase))
                        {
                            parameters[j] = parameters[j].Substring(3).TrimStart(new char[0]);
                        }
                    }
                }
            }
            else
            {
                typeName         = inputTypeName.Substring(0, length) + inputTypeName.Substring(length + 1);
                elemantDecorator = inputTypeName.Substring(length, 1);
            }
            if ((parseTypeNameLanguage == ParseTypeNameLanguage.CSharp) && CSKeywords.Contains(typeName))
            {
                typeName = DotNetKeywords[CSKeywords.IndexOf(typeName)];
            }
            else if ((parseTypeNameLanguage == ParseTypeNameLanguage.VB) && VBKeywords.Contains(typeName))
            {
                typeName = DotNetKeywords[VBKeywords.IndexOf(typeName)];
            }
            return(true);
        }
 internal static bool ParseTypeName(string inputTypeName, ParseTypeNameLanguage parseTypeNameLanguage, out string typeName, out string[] parameters, out string elemantDecorator)
 {
     typeName = string.Empty;
     parameters = null;
     elemantDecorator = string.Empty;
     if (parseTypeNameLanguage == ParseTypeNameLanguage.VB)
     {
         inputTypeName = inputTypeName.Replace('(', '[').Replace(')', ']');
     }
     else if (parseTypeNameLanguage == ParseTypeNameLanguage.CSharp)
     {
         inputTypeName = inputTypeName.Replace('<', '[').Replace('>', ']');
     }
     int length = inputTypeName.LastIndexOfAny(new char[] { ']', '&', '*' });
     if (length == -1)
     {
         typeName = inputTypeName;
     }
     else if (inputTypeName[length] == ']')
     {
         int num2 = length;
         int num3 = 1;
         while ((num2 > 0) && (num3 > 0))
         {
             num2--;
             if (inputTypeName[num2] == ']')
             {
                 num3++;
             }
             else if (inputTypeName[num2] == '[')
             {
                 num3--;
             }
         }
         if (num3 != 0)
         {
             return false;
         }
         typeName = inputTypeName.Substring(0, num2) + inputTypeName.Substring(length + 1);
         string str = inputTypeName.Substring(num2 + 1, (length - num2) - 1).Trim();
         if ((str == string.Empty) || (str.TrimStart(new char[0])[0] == ','))
         {
             elemantDecorator = "[" + str + "]";
         }
         else
         {
             int num4 = 0;
             char[] chArray = str.ToCharArray();
             for (int i = 0; i < chArray.Length; i++)
             {
                 if (chArray[i] == '[')
                 {
                     num4++;
                 }
                 else if (chArray[i] == ']')
                 {
                     num4--;
                 }
                 else if ((chArray[i] == ',') && (num4 == 0))
                 {
                     chArray[i] = '$';
                 }
             }
             parameters = new string(chArray).Split(new char[] { '$' });
             for (int j = 0; j < parameters.Length; j++)
             {
                 parameters[j] = parameters[j].Trim();
                 if (parameters[j][0] == '[')
                 {
                     parameters[j] = parameters[j].Substring(1, parameters[j].Length - 2);
                 }
                 if ((parseTypeNameLanguage == ParseTypeNameLanguage.VB) && parameters[j].StartsWith("Of ", StringComparison.OrdinalIgnoreCase))
                 {
                     parameters[j] = parameters[j].Substring(3).TrimStart(new char[0]);
                 }
             }
         }
     }
     else
     {
         typeName = inputTypeName.Substring(0, length) + inputTypeName.Substring(length + 1);
         elemantDecorator = inputTypeName.Substring(length, 1);
     }
     if ((parseTypeNameLanguage == ParseTypeNameLanguage.CSharp) && CSKeywords.Contains(typeName))
     {
         typeName = DotNetKeywords[CSKeywords.IndexOf(typeName)];
     }
     else if ((parseTypeNameLanguage == ParseTypeNameLanguage.VB) && VBKeywords.Contains(typeName))
     {
         typeName = DotNetKeywords[VBKeywords.IndexOf(typeName)];
     }
     return true;
 }