⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csharp2vbconverter.cs

📁 vc转换成vb
💻 CS
📖 第 1 页 / 共 4 页
字号:

			for(int i =0; i < aText.Length; i++)
			{
				lcStr = aText[i].Trim();

				//Handle the comments
				int nStart = aText[i].IndexOf("//");
				if(nStart < 0)
				{
					sb.Append(aText[i]+ "\n");
					continue;
				}
				
				string cOriginal = "'" + aText[i].Substring(nStart + 2);
				string cToken = this.GetNewToken();
				aText[i] = aText[i].Substring(0, nStart) + cToken ;
				sb.Append(aText[i]+ "\n");

				MappingToken o = new MappingToken();
				o.cOriginal = cOriginal;
				o.cToken = cToken;
				this.Stack.Add(o);
				//i--; do this once for a line only
				continue;

			}
			return sb.ToString();
		}

		/// <summary>
		/// Handles the breakdown of lines so the spacing is maintained as c#
		/// allows to have multiple statements in a single line
		/// </summary>
		/// <param name="tcText"></param>
		/// <returns></returns>
		private string HandleLineBreakDown(string tcText)
		{
			StringBuilder sb = new StringBuilder();
			string[] aText = tcText.Split('\n');
			BaseManager b = new BaseManager();
			string lcStr;

			for(int i =0; i<aText.Length; i++)
			{
				lcStr = aText[i].Trim();

				if(this.IsHandled(lcStr))
				{
					sb.Append(aText[i]+ "\n");
					continue;
				}

				if(lcStr.StartsWith("{") && lcStr.Length == 1)
				{
					sb.Append(aText[i] + "\n");
					continue;
				}

				if(lcStr.StartsWith("}") && lcStr.Length == 1)
				{
					sb.Append(aText[i] + "\n");
					continue;
				}

				if(lcStr.IndexOf("}") >= 0 || lcStr.IndexOf("}") >= 0)
				{
					//if the line contains the tokens while or until then ignore the line
					if((lcStr.IndexOf("while") > 0) || lcStr.IndexOf("until") >0)
					{
						sb.Append(aText[i] + "\n");
						continue;
					}

					//Handle it appropriately
					b.GetBlankToken(aText[i]);
					this.HandleBrakets(ref sb, aText[i], b.BlankToken);
					sb.Append("\n");
					continue;
				}
				else if(lcStr.EndsWith("{"))
				{
					//break it down into two lines
					b.GetBlankToken(aText[i]);
					this.HandleBrakets(ref sb, aText[i], b.BlankToken);
					sb.Append("\n");
					continue;
				}
				else
				{
					sb.Append(aText[i] + "\n");
				}

			}
			return sb.ToString();
		}


		/// <summary>
		/// Handles the namespace declaration
		/// </summary>
		/// <param name="tcText"></param>
		/// <returns></returns>
		private string HandleReplacements(string tcText)
		{
			tcText = Regex.Replace(tcText, "using ", "Imports ");
			tcText = Regex.Replace(tcText, "==", "=");

			this.ReplaceTypes(ref tcText, "bool", "Boolean");
			this.ReplaceTypes(ref tcText, "byte", "Byte");
			this.ReplaceTypes(ref tcText, "char", "Char");
			this.ReplaceTypes(ref tcText, "decimal", "Decimal");
			this.ReplaceTypes(ref tcText, "double", "Double");
			this.ReplaceTypes(ref tcText, "float", "single");
			this.ReplaceTypes(ref tcText, "int", "Integer");
			this.ReplaceTypes(ref tcText, "long", "Long");
			this.ReplaceTypes(ref tcText, "object", "Object");
			this.ReplaceTypes(ref tcText, "short", "Short");
			this.ReplaceTypes(ref tcText, "string", "String");
			this.ReplaceTypes(ref tcText, "sbyte", "System.SByte");
			this.ReplaceTypes(ref tcText, "uint", "System.UInt32");
			this.ReplaceTypes(ref tcText, "ulong", "System.UInt64");
			this.ReplaceTypes(ref tcText, "ushort", "System.UInt16");
			this.ReplaceTypes(ref tcText, "this", "Me");
			this.ReplaceTypes(ref tcText, "base", "MyBase");


			tcText = Regex.Replace(tcText, "true", "True");
			tcText = Regex.Replace(tcText, "false", "False");
			tcText = Regex.Replace(tcText, "typeof", "Type.GetType");
			tcText = Regex.Replace(tcText, "public ", "Public ");
			tcText = Regex.Replace(tcText, "///", "'--");
			tcText = Regex.Replace(tcText, "//", "'");
			tcText = Regex.Replace(tcText, "new", "New");
			tcText = Regex.Replace(tcText, "try", "Try");
			tcText = Regex.Replace(tcText, "catch", "Catch");
			tcText = Regex.Replace(tcText, "finally", "Finally");
			tcText = Regex.Replace(tcText, "return", "Return");
			tcText = Regex.Replace(tcText, "throw ", "Throw ");
			tcText = Regex.Replace(tcText, "!=", "<>");
			tcText = Regex.Replace(tcText, "! =", "<>");
			tcText = Regex.Replace(tcText, ";", "");
			tcText = Regex.Replace(tcText, "goto", "GoTo");
			tcText = tcText.Replace("[", "(");
			tcText = tcText.Replace("]", ")");
			tcText = tcText.Replace(">>", ">");
			tcText = tcText.Replace("<<", "<");
			tcText = tcText.Replace(" || ", " Or ");
			tcText = tcText.Replace(" && ", " And ");


			// tcText = tcText.Replace("ref ", "");		//remove any calls where we pass by reference because vb takes care of it
			// tcText = tcText.Replace("out ", "");		//remove any calls where we pass by reference because vb takes care of it
			this.ReplaceTypes(ref tcText, "ref", "");
			this.ReplaceTypes(ref tcText, "out", "");

			//tcText = Regex.Replace(tcText, "#region Windows Form Designer generated code", "#Region \"Windows Form Designer generated code\"");
			//tcText = Regex.Replace(tcText, "#region Web Form Designer generated code",  "#Region \"Web Form Designer generated code\"");
			//tcText = Regex.Replace(tcText, "#region ([a-Z]*)", "#Region \"$1");
			tcText = Regex.Replace(tcText, "#endregion", "#End Region");
			tcText = Regex.Replace(tcText, "null", "Nothing");
			tcText = Regex.Replace(tcText, "!", "Not ");
			tcText = this.FixLines(this.FixArrays(tcText));
			tcText = this.FixLines(this.HandleExits(tcText));

			return tcText;

		}

		// Handle the post replacements after all the strings have been popped out of the stack
		private string HandlePostReplacements(string tcText)
		{
			// Handle removing the @ symbol from strings
			// (@\"([^\"])) : Look for any combination of the @ symbol followed by a double quote. Make sure that it is not followed by another bouble quote
			// \"$2 : As we have two groups in the above condition. Add a double quote and leave the second group as is
			tcText = Regex.Replace(tcText, "(@\"([^\"]))", "\"$2");

			// Handle the #region blocks
			// ^#region ((\\w*\\s*)*) : Look for a pattern that begins with #region followed by any number of words or spaces
			// #Region \"$1\" : Replace it with quotes around the string and add the #Region in the beginning
			tcText = Regex.Replace(tcText, "^#region ((\\w*\\s*)*)", "#Region \"$1\"");

			// Handle Decimal.Remainder
			// =([a-zA-Z0-9 \\.\\(\\)]*)[%]([a-zA-Z0-9 \\.\\(\\)]*): Look for a pattern that begins with = followed by another number of characters including spaces, digits, characters, (, ) or dots. Then followed by a percent and then again followed by characters, spaces, dots or parenthesis.
			// = Decimal.Remainder($1, $2) : Replace with Decimal.Remainder()
			tcText = Regex.Replace(tcText, "=([a-zA-Z0-9 \\.\\(\\)]*)[%]([a-zA-Z0-9 \\.\\(\\)]*)", "= Decimal.Remainder($1, $2)");

			return tcText;
		}


		private string FixArrays(string tcText)
		{
			StringBuilder sb = new StringBuilder();
			string[] aText = tcText.Split('\r');
			for(int i = 0; i < aText.Length; i++)
			{
				if(aText[i].EndsWith(this.EndOfAssignedArray))
				{
					StringBuilder s = new StringBuilder();
					aText[i] = aText[i].Replace(this.EndOfAssignedArray, "");
					s.Append(aText[i] + " ");
					i++;

					do
					{
						s.Append(aText[i].Trim());
						if(aText[i].Trim().IndexOf("}") >= 0)
						{
							break;
						}
						i++;
					}while(true);

					sb.Append(s.ToString() + "\r");
				}
				else
				{
					sb.Append(aText[i] + "\r");
				}
			}
			return sb.ToString();

		}


		/// <summary>
		/// Looks for specific patters for the exit loop and fixes the ONE that we passed
		/// </summary>
		/// <param name="aText"></param>
		/// <param name="nStart"></param>
		private void FixExit(ref string[] aText, int nStart)
		{
			string lcCurrent = "";
			for(int i = nStart; i > 0; i--)
			{

				lcCurrent = aText[i].TrimStart();
				//Sub,Function,Property,Do,For,While,Select,Try
				if(lcCurrent.IndexOf("Sub ") >= 0)
				{
					aText[nStart] = aText[nStart].Replace("break", "Exit Sub");
					break;
				}
				else if(lcCurrent.IndexOf("Function ") >=0)
				{
					aText[nStart] = aText[nStart].Replace("break", "Exit Function");
					break;
				}
				else if(lcCurrent.IndexOf("Property ") >= 0)
				{
					aText[nStart] = aText[nStart].Replace("break", "Exit Property");
					break;
				}
				else if(lcCurrent.StartsWith("Do ") == true)
				{
					aText[nStart] = aText[nStart].Replace("break", "Exit Do");
					break;
				}
				else if(lcCurrent.StartsWith("For ") == true)
				{
					aText[nStart] = aText[nStart].Replace("break", "Exit For");
					break;
				}
				else if(lcCurrent.StartsWith("While ") == true)
				{
					aText[nStart] = aText[nStart].Replace("break", "Exit While");
					break;
				}
				else
				{
					//nothing
				}
			}

		}

		/// <summary>
		/// Handles all the exits by looking for specific keywords and then handing the exit appropriately
		/// </summary>
		/// <param name="tcText"></param>
		/// <returns></returns>
		private string HandleExits(string tcText)
		{
			StringBuilder sb = new StringBuilder();
			string[] aText = tcText.Split('\r');

			for(int i = 0; i < aText.Length; i++)
			{
				if(aText[i].Trim() == "break")
				{
					// we found a customer. Now we pass this to the appropriate handle to fix it
					this.FixExit(ref aText, i);
				}
				sb.Append(aText[i] + "\r");
			}
			return sb.ToString();
		}


		/// <summary>
		/// Returns the footer that is added to the end of the generated VB.Net code
		/// </summary>
		/// <returns></returns>
		private string GetFooter()
		{
			string lcFooter = "";
			lcFooter += "'----------------------------------------------------------------\r\n"; 
			lcFooter += "' Converted from C# to VB .NET using CSharpToVBConverter(1.2).\r\n";
			lcFooter += "' Developed by: Kamal Patel (http://www.KamalPatel.net) \r\n" ;
			lcFooter += "'----------------------------------------------------------------\r\n"; 
			return lcFooter;
		}

		
		/// <summary>
		/// Handles multi line comments by commenting each line
		/// </summary>
		/// <param name="tcText"></param>
		/// <returns></returns>
		protected string HandleMultiLineComments(string tcText)
		{
			string[] aText = tcText.Split('\r');
	
			//Loop through each line of the string 
			int i;
			bool lStart = false;
			for(i = 0; i < aText.Length; i++)
			{
				//start making the changes when we encounter the symbol "/*"
				if(lStart)
				{
					//Make the change and continue
					aText[i] = "// " + aText[i].Replace("\n", "") + "\n";


					//Maybe we are at the end point so check for terminator
					if(aText[i].IndexOf("*/") >= 0)
					{
						//locate the actual end point
						int nEndPt = aText[i].IndexOf("*/");
						aText[i] = aText[i].Substring(0, nEndPt + 2) + "\n" + aText[i].Substring(nEndPt +2);

						lStart = false;
					}
				}
				else
				{
					//Make the check here
					if(aText[i].Trim().StartsWith("//") == false && aText[i].IndexOf("/*") >=0)
					{
						lStart = true;
						aText[i] = aText[i].Replace("/*", "");

						//decrement the value of i so it gets picked up
						i--;
					}
				}

			}

			return this.Build(ref aText);
		}

		public void ReplaceTypes(ref string tcText, string tcOld, string tcNew)
		{

			tcText = tcText.Replace(" " + tcOld + " ", " " + tcNew + " ");
			tcText = tcText.Replace("	" + tcOld + " ", "	" + tcNew + " ");
			tcText = tcText.Replace("(" + tcOld + ")", "(" + tcNew + ")");
			tcText = tcText.Replace("(" + tcOld + " ", "(" + tcNew + " ");
			tcText = tcText.Replace("\n" + tcOld + " ", "\n" + tcNew + " ");
			tcText = tcText.Replace(tcOld + ".", tcNew + ".");
			tcText = tcText.Replace(" " + tcOld + "(", " " + tcNew + "(");
			tcText = tcText.Replace("	" + tcOld + "(", "	" + tcNew + "(");
			tcText = tcText.Replace(" " + tcOld + "\r", " " + tcNew + "\r");
			tcText = tcText.Replace("	" + tcOld + "\r", "	" + tcNew + "\r");
			tcText = tcText.Replace(" " + tcOld + "\n", " " + tcNew + "\n");
			tcText = tcText.Replace("	" + tcOld + "\n", "	" + tcNew + "\n");
			tcText = tcText.Replace(" " + tcOld + ";", " " + tcNew + ";");
			tcText = tcText.Replace("	" + tcOld + ";", "	" + tcNew + ";");
			tcText = tcText.Replace("=" + tcOld + "(", "=" + tcNew + "(");
			tcText = tcText.Replace(" " + tcOld + "(", " " + tcNew + "(");
			tcText = tcText.Replace("=" + tcOld + "[", "=" + tcNew + "[");
			tcText = tcText.Replace(" " + tcOld + "[", " " + tcNew + "[");
			tcText = tcText.Replace(" " + tcOld + ")", " " + tcNew + ")");
			tcText = tcText.Replace("," + tcOld + ")", "," + tcNew + ")");
		}




		/// <summary>
		/// Handles all the attributes
		/// </summary>
		/// <param name="tcText"></param>
		/// <returns></returns>
		protected string HandleAttributes(string tcText)
		{
			//Search for a pattern where the line begins with [ and ends with ]
			string[] aText = tcText.Split('\r');
	
			//Loop through each line of the string 
			int i;
			for(i = 0; i < aText.Length; i++)
			{
				//this.HandleCasting(ref aText[i]);

				//When we find the line starting with [ symbol we begin
				if(aText[i].Trim().StartsWith("["))
				{
					//Locate the ending position
					for(int j=i; j<aText.Length; j++)
					{
						if(aText[j].Trim().EndsWith("]"))
						{
							//we have a match for an attribute
							//Change the [ to < and ] to >
							int npos = aText[i].IndexOf("[");
							aText[i] = aText[i].Substring(0, npos) + "<<" + aText[i].Substring(npos+1);
							
							npos = aText[j].IndexOf("]");
							aText[j] = aText[j].Substring(0, npos) + ">> _ " + aText[j].Substring(npos+1);
							break;
						}

					}
				}
			}

			return this.Build(ref aText);
		}

		/// <summary>
		/// Handles all the math operations such as ++ and replaces them with VB version
		/// </summary>
		/// <param name="tcText"></param>
		/// <returns></returns>
		protected string HandleMathOperations(string tcText)
		{
			string[] aText = tcText.Split('\r');
			MathManager mm = new MathManager();

			//Loop through each line of the string 
			int i;
			for(i = 0; i < aText.Length; i++)
			{
				if(this.IsHandled(aText[i]))
					continue;

				//Check if we can find the pattern
				if(aText[i].IndexOf("++") >= 0 || aText[i].IndexOf("--") >=0)
				{
					if(aText[i].Trim().StartsWith("for"))
						continue;

					aText[i] = mm.GetBlock(aText[i] + "\r\n");
				}
			}

			return this.Build(ref aText);
		}

		/// <summary>
		/// Handles all the conditional operators that contain the pattern ?:
		/// </summary>
		/// <param name="tcText"></param>
		/// <returns></returns>
		protected string HandleConditionalOperator(string tcText)
		{
			string[] aText = tcText.Split('\r');

			//Loop through each line of the string 
			int i;
			for(i = 0; i < aText.Length; i++)
			{
				if(this.IsHandled(aText[i]))
					continue;

				//Check if we can find the pattern
				if(aText[i].IndexOf("?") >= 0 || aText[i].IndexOf(":") >=0)
				{
					string lcSearchPattern;
					string lcReplacePattern;

					// Check if the pattern begins with the word return
					if(aText[i].Trim().StartsWith("return"))
					{
						lcSearchPattern = "([ ]*return[ ]*)([a-zA-Z0-9 \\.\\(\\)]*)[?]([a-zA-Z0-9 \\.\\(\\)]*)[:]([a-zA-Z0-9 \\.\\(\\)]*)";
						lcReplacePattern = "$1 IIf($2, $3, $4)";
					}
					else
					{
						lcSearchPattern = "([a-zA-Z0-9 \\.\\(\\)]*)[=]([a-zA-Z0-9 \\.\\(\\)]*)[?]([a-zA-Z0-9 \\.\\(\\)]*)[:]([a-zA-Z0-9 \\.\\(\\)]*)";
						lcReplacePattern = "$1= IIf($2, $3, $4)";
					}

					// Apply the pattern update
					string lcResult = Regex.Replace(aText[i], lcSearchPattern, lcReplacePattern);
					aText[i] = lcResult + "\r\n";
				}
			}

			return this.Build(ref aText);
		}

		protected bool IsNextCharValidVariable(string tcLine, int tnStart)
		{
			//quick and dirty for now
			if((tcLine.Trim().EndsWith(";") || (tcLine.Trim().EndsWith(")"))))
			{
				tcLine = tcLine.Replace(") ", ")");
			}

			//Check if this is the last character in the string
			if(tnStart == tcLine.Length)
				return false;

			//if the next character is a space then simply ignore it
			char lcNextChar = tcLine[tnStart + 1];

			int nAscii = (int)lcNextChar;
			if((nAscii == 95) || (nAscii == 40) || (nAscii >= 65 && nAscii <= 90) || (nAscii >= 97 && nAscii <= 122))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public string HandleCasting(string tcText)
		{
			StringBuilder sb = new StringBuilder();
			string[] aText = tcText.Split('\n');

			for(int i =0; i<aText.Length; i++)
			{
				if(aText[i].IndexOf("(") > 0)
					this.GetFixedCasting(ref aText[i]);

				sb.Append(aText[i] + "\n");
			}
			return sb.ToString();
		}

		protected void GetFixedCasting(ref string tcLine)
		{
			int nStart = -1;
			do
			{
				int npos = tcLine.IndexOf("(", nStart + 1);

				nStart = npos;
				if(npos < 0)
					break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -