如何在C#中实现自定义if语句?
我正在文本框中制作一种自定义脚本语言 例如:如何在C#中实现自定义if语句?,c#,.net,C#,.net,我正在文本框中制作一种自定义脚本语言 例如: 、或执行时,标签将转换为来自文件、数据库或用户字段输入的数据 在给定示例中,结果为: Elliot, John or Gleave. 这对现在来说很好。我的结论是,我需要if语句 以下是一些例子: <IF NAME1="George"> <DoStuff> <ENDIF> 我使用的方法是从字符串中查找索引并将其子字符串化。 你能给我一些想法或指导如何制作自定义if语句吗 编辑:我设法找到了一种方法来做我想做
、
或
执行时,标签将转换为来自文件、数据库或用户字段输入的数据
在给定示例中,结果为:
Elliot, John or Gleave.
这对现在来说很好。我的结论是,我需要if语句
以下是一些例子:
<IF NAME1="George">
<DoStuff>
<ENDIF>
我使用的方法是从字符串中查找索引并将其子字符串化。
你能给我一些想法或指导如何制作自定义if语句吗
编辑:我设法找到了一种方法来做我想做的事情。所以在这里我分享它,如果有人想编辑,建议等,请告诉我
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;
}
publicstaticstringreplaceifstatement(字符串脚本、CResultData结果数据)
{
字符串工作脚本=脚本;
string manufactedscript=workingScript.Clone()作为字符串;//将脚本存储在temp变量中以供进一步检查
workingScript=CStringFunctions.ReplaceAll(workingScript,“\r”,string.Empty);//ReplaceAll使用string.Replace()方法
workingScript=CStringFunctions.ReplaceAll(workingScript,“\t”,string.Empty);
字符串ifStartFieldName=“”,
ifEndIndexOpenTag+ifStartFieldName.Length+1);
如果(ifStartIndexCloseTag!=-1)
{
字符串ifStatement=CStringFunctions.SubstrString(
工作脚本,
ifStartIndexOpenTag+ifStartFieldName.Length,
ifStartIndexCloseTag-(ifStartIndexOpenTag+ifStartFieldName.Length));
ifStatement=CStringFunctions.ReplaceAll(ifStatement,“,string.Empty);
string rightSideIfStatement=CStringFunctions.SubstrString(
如果说,
1.
IndexOfString(ifStatement,“=”)-1);
string leftSideIfStatement=CStringFunctions.SubstrString(
如果说,
CStringFunctions.IndexOfString(ifStatement,“=”)+1,
CStringFunctions.IndexOfString(ifStatement,“)”)
-IndexOfString(ifStatement,“=”)-1);
CResultData ifResultData=resultData.Clone();//下面是包含所有道具的类,例如:NAME1、NAME2或FAM
foreach(ifResultData.GetType().GetProperties()中的PropertyInfo属性)
{
if(rightSideIfStatement==property.Name.ToUpper()
&&leftSideIfStatement==(string)property.GetValue(ifResultData,null))//在此语句中,比较
{
//如果比较为true,我将从脚本中删除和
操纵脚本=CStringFunctions.ReplaceStringAtPosition(
工作脚本,
如果StartIndexOpenTag,
如果StartIndexCloseTag+1,
字符串(空);
ifEndIndexOpenTag=CStringFunctions.IndexOfString(操纵脚本,ifEndFieldName);
ifEndIndexCloseTag=CStringFunctions.IndexOfString(
操纵脚本,
">",
ifEndIndexOpenTag+ifStartFieldName.Length+1);
操纵脚本=CStringFunctions.ReplaceStringAtPosition(
操纵脚本,
如果是IndexOpentag,
如果EndIndexCloseTag+1,
字符串(空);
工作脚本=操纵脚本;
返回工作脚本;
}
}
if(manufactedscript!=null&&manufactedscript.Length==workingScript.Length)//当if语句为false时
//在这里,我比较了参数脚本和临时脚本,如果脚本中存在要从中删除的更改,请从到
{
操纵脚本=CStringFunctions.ReplaceStringAtPosition(
工作脚本,
如果StartIndexOpenTag,
如果EndIndexCloseTag+1,
字符串(空);
工作脚本=操纵脚本;
返回工作脚本;
}
}
其他的
{
重复=错误;
}
}
其他的
{
重复=错误;
}
}
工作脚本=操纵脚本;
返回工作脚本;
}
如果要以上面提供的标记样式编写自定义脚本语言,那么最好使其与XML兼容
假设使用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();
}
}
这不是一个完美的示例,但应该足以让您继续使用。如果您要以上面提供的标记样式编写自定义脚本语言,您最好使其与XML兼容 假设使用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();
}
}
这不是一个完美的示例,但应该足以让您继续使用。如果您要以上面提供的标记样式编写自定义脚本语言,您最好使其与XML兼容 假设使用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();
}
}
这不是一个完美的示例,但应该足以让您继续使用。如果您要以上面提供的标记样式编写自定义脚本语言,您最好使其与XML兼容 假设使用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();
}
}
这不是一个完美的例子,但应该足够好,可以让您继续。您所说的