如何在C#中实现自定义if语句? [英] How to implement custom if statement in C#?

查看:79
本文介绍了如何在C#中实现自定义if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在制作一种自定义标记语言。例如:

< NAME1> < NAME2> < FAM>。

但只有标签不能帮我工作,所以我得出结论我需要if声明自己做一些检查。



例子

Hello,

I am making one custom markup language.For example:
<NAME1>, <NAME2> or <FAM>.
But only tags won't do me work, so I come to the conclusion I need if statement to make some checkings myself.

Example

<IF NAME1="George"><br />
<DoStuff><br />
<ENDIF>





你能给我一些想法或指导如何制作自定义if语句吗?



编辑



我不认为我得到了这个问题。



为了得到这个想法。让我们说它已经完成了,这是一个例子如何它将执行:

< minding your =business =>



Can you give me some ideas or guidance how to make the custom if statement?

EDIT

I don't think I get the question.

To get the idea.Lets say we have it done, and here is example how it will execute:
<minding your="" business="">

<br />
<IF NAME1="George"><br />
<DoStuff><br />
<DoOtherStuff><br />
<ENDIF><br />
<Minding Your Other Business><br />





如果真的执行将是:



if its true the executed will be:

<br />
<Minding Your Business><br />
<br />
<DoStuff><br />
<DoOtherStuff><br />
<br />
<Minding Your Other Business><br />





if - false:



if - false:

<br />
<Minding Your Business><br />
<Minding Your Other Business><br />

推荐答案

我不确定目标,但我不建议完全提出新的标记。相反,看看如何使用Xml来构建逻辑。对于例如您可以使用属性来指定条件:



I am not really sure about the objective, but I won't recommend to come up with an new markup altogether. Instead, look at how you can use the Xml to structure your logic. For e.g. you can make use of attributes to specify the condition:

<dostuff when="Name1 is George" /> 





你能做什么也可以使用您选择的任何编程语言定义的属性值,这样就不必重写解析器了,它已经可以评估条件了。



What you can also do is have the attribute value defined in any programming language of your choice so that the parser does not have to be rewritten and it can already evaluate the condition.


我设法制作了一个方法做我想做的事。所以我在这里分享它,如果有人想编辑,建议等等。请告诉我。



I managed to make a method for doing what I want. So here I am sharing it if someone wants to edit, advice or etc. Please advise me.

public static string ReplaceIfStatement(string script, CResultData resultData)
{
	string workingScript = script;
	string manipulatedScript = workingScript.Clone() as string; // storing the script in temp variable for further checks

	workingScript = CStringFunctions.ReplaceAll(workingScript, "\r", string.Empty); // ReplaceAll uses the string.Replace() method
	workingScript = CStringFunctions.ReplaceAll(workingScript, "\t", string.Empty);

	string ifStartFieldName = "<if hold=" />	string ifEndFieldName = "><endif>";

	workingScript = CStringFunctions.ReplaceAll(workingScript, "<if\n",>
	bool repeat = true;

	while (repeat)
	{
		int ifStartIndexOpenTag = workingScript.IndexOf(ifStartFieldName);
		int ifEndIndexOpenTag = workingScript.IndexOf(ifEndFieldName);

		if (ifStartIndexOpenTag != -1 
			|| ifEndIndexOpenTag != -1)
		{
			int ifStartIndexCloseTag = CStringFunctions.IndexOfString(
				workingScript,
				">",
				ifStartIndexOpenTag + ifStartFieldName.Length + 1); //IndexOfString() uses the IndexOf()

			int ifEndIndexCloseTag = CStringFunctions.IndexOfString(
				workingScript,
				">",
				ifEndIndexOpenTag + ifStartFieldName.Length + 1);

			if (ifStartIndexCloseTag != -1)
			{
				string ifStatement = CStringFunctions.SubstrString(
					workingScript,
					ifStartIndexOpenTag + ifStartFieldName.Length,
					ifStartIndexCloseTag - (ifStartIndexOpenTag + ifStartFieldName.Length));

				ifStatement = CStringFunctions.ReplaceAll(ifStatement, " ", string.Empty);

				string rightSideIfStatement = CStringFunctions.SubstrString(
					ifStatement,
					1,
					CStringFunctions.IndexOfString(ifStatement, "=") - 1);

				string leftSideIfStatement = CStringFunctions.SubstrString(
					ifStatement,
					CStringFunctions.IndexOfString(ifStatement, "=") + 1,
					CStringFunctions.IndexOfString(ifStatement, ")")
					- CStringFunctions.IndexOfString(ifStatement, "=") - 1);

				CResultData ifResultData = resultData.Clone(); // here is the class containing all props, ex: NAME1, NAME2 or FAM

				foreach (PropertyInfo property in ifResultData.GetType().GetProperties())
				{
					if (rightSideIfStatement == property.Name.ToUpper()
						&& leftSideIfStatement == (string)property.GetValue(ifResultData, null)) // In this statement compare the property name in the <if> and the <if> 
					{
						//if the compare is true i remove the <if> and the <endif> from the script
						manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
							workingScript,
							ifStartIndexOpenTag,
							ifStartIndexCloseTag + 1,
							string.Empty);

						ifEndIndexOpenTag = CStringFunctions.IndexOfString(manipulatedScript, ifEndFieldName);
						ifEndIndexCloseTag = CStringFunctions.IndexOfString(
							manipulatedScript,
							">",
							ifEndIndexOpenTag + ifStartFieldName.Length + 1);

						manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
							manipulatedScript,
							ifEndIndexOpenTag,
							ifEndIndexCloseTag + 1,
							string.Empty);

						workingScript = manipulatedScript;

						return workingScript;
					}
				}

				if (manipulatedScript != null && manipulatedScript.Length == workingScript.Length) // When the IF statement is false
				//here I compare the parameter script with the temp script if there are changes to remove from the script begging from <if> to <endif>
				{
					manipulatedScript = CStringFunctions.ReplaceStringAtPosition(
						workingScript,
						ifStartIndexOpenTag,
						ifEndIndexCloseTag + 1,
						string.Empty);

					workingScript = manipulatedScript;

					return workingScript;
				}
			}
			else
			{
				repeat = false;
			}
		}
		else
		{
			repeat = false;
		}
	}

	workingScript = manipulatedScript;

	return workingScript;
}</endif></if></endif></if></if></if></endif></if>


这篇关于如何在C#中实现自定义if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆