如何在C#中实现自定义if语句?

时间:2022-06-01 17:07:41

I am making one custom script language in textbox.

我在文本框中制作一个自定义脚本语言。

For example: <NAME1>, <NAME2> or <FAM> and when executing it the tags are transform into the data that is coming from file, database or user input from fields.

例如: ,并且在执行时,标签将转换为来自字段的文件,数据库或用户输入的数据。

In the given example the result is:

在给定的示例中,结果是:

Elliot, John or Gleave.

This is good for now. I come to the conclusion that I need if statement.

这对现在来说很好。我得出结论,我需要if声明。

Here is some example:

这是一些例子:

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

The methods that I am using are finding indexes from the string and substringing them. Can you give me some ideas or guidance how to make the custom if statement?

我正在使用的方法是从字符串中查找索引并对它们进行子串。你能给我一些想法或指导如何制作自定义if语句吗?

EDIT: 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 ";
    string ifEndFieldName = "<ENDIF>";

    workingScript = CStringFunctions.ReplaceAll(workingScript, "<IF\n", ifStartFieldName);

    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 (statement)> 
                    {
                        //if the compare is true i remove the <IF (statement)> 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 (statement)> 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;
}

1 个解决方案

#1


3  

If you are going to write a custom script language in the style of the markup you provided above, you might as well make it XML-compliant.

如果您要使用上面提供的标记样式编写自定义脚本语言,那么您也可以使其符合XML标准。

See this example of parsing XML.

请参阅解析XML的此示例。

Assuming the use of XML and the syntax described above,

假设使用XML和上面描述的语法,

var document = new XMLDocument();
document.Load(xmlFilePath);

var documentNodes = document.DocumentElement.SelectNodes("path/from/root/to/stuff/of/interest");

foreach (var node in documentNodes)
{
    var name1NodeValue = node.SelectSingleNode("NAME1").InnerText;
    var ifNode = node.SelectSingleNode("IF");
    if (ifNode.Attributes["NAME1"].Value.Equals(name1NodeValue) {
        //Look for the node of interest. I just hard-coded doStuff for the sake of brevity.
        doStuff();
    }
}

Not a perfect example but should be good enough to get you going.

不是一个完美的例子,但应该足够好,让你去。

#1


3  

If you are going to write a custom script language in the style of the markup you provided above, you might as well make it XML-compliant.

如果您要使用上面提供的标记样式编写自定义脚本语言,那么您也可以使其符合XML标准。

See this example of parsing XML.

请参阅解析XML的此示例。

Assuming the use of XML and the syntax described above,

假设使用XML和上面描述的语法,

var document = new XMLDocument();
document.Load(xmlFilePath);

var documentNodes = document.DocumentElement.SelectNodes("path/from/root/to/stuff/of/interest");

foreach (var node in documentNodes)
{
    var name1NodeValue = node.SelectSingleNode("NAME1").InnerText;
    var ifNode = node.SelectSingleNode("IF");
    if (ifNode.Attributes["NAME1"].Value.Equals(name1NodeValue) {
        //Look for the node of interest. I just hard-coded doStuff for the sake of brevity.
        doStuff();
    }
}

Not a perfect example but should be good enough to get you going.

不是一个完美的例子,但应该足够好,让你去。