c#中的一个c风格指令,允许使用一个表达式两次#define x(k){#k,k}
本文关键字:一个 #define 两次 表达式 风格 指令 许使用 | 更新日期: 2023-09-27 18:20:35
在C中,可以编写一个宏函数,用字符串形式的输入替换输入。
#define x(k) {#k, k}
"(4)"将生成"{"4",4}"
我在C#中有一个用例,我想把这样的输入传递给单元测试。
private void AssertInt64Expression(Int64 i, string str)
{
Assert.AreEqual(i, MathFactory.make(str));
}
[Test]
public void ParseBasic()
{
AssertInt64Expression(4, "4");
AssertInt64Expression(2+3, "2+3");
AssertInt64Expression(7-11, "7-11");
AssertInt64Expression(7-11 *2, "7-11 *2");
AssertInt64Expression(7 - 11 * 2, "7 - 11 * 2");
}
我本质上是在重复这里的信息(包括空格),我如何使用类似c风格的宏来解决c#中的问题?
编辑:
本质上我很想写:
private void AssertInt64Expression(GeneratorMagic magic)
{
Assert.AreEqual(magic.ToCode(), MathFactory.make(magic.ToString()));
}
[Test]
public void ParseBasic()
{
AssertInt64Expression(<#7 - 11 * 2#>);
}
我知道这不会编译。
编辑:
我添加了一个代码片段作为答案来说明我正在寻找什么。然而,这个代码段运行非常慢,因为我需要它来将我的单元测试重构成更干净、重复更少的代码,所以我需要这个代码段更快地运行。该片段本质上提供了来自先前编辑的magic
作为KeyValuePair
。
您可以使用带有重载运算符的自定义数字类。
static void Main(string[] args)
{
Console.WriteLine((Number)1 + 5);
Console.WriteLine((int)((Number)1 + 5 + 6));
}
public class Number
{
private string _representation = "";
private int _number = 0;
private Number(int n)
{
_number = n;
_representation = n.ToString();
}
public Number Plus(int n)
{
_representation += " + " + n;
_number += n;
return this;
}
public static Number operator +(Number value1, int value2)
{
return value1.Plus(value2);
}
public static explicit operator Number(int val)
{
return new Number(val);
}
public static explicit operator int(Number num)
{
return num._number;
}
public override string ToString()
{
return _representation;
}
}
下面的代码段可以满足我的需要,但运行速度似乎非常慢。
private KeyValuePair<String, Int64> GenerateCodeInt64(String mathKey)
{
string codeNamespace = "MathTestCalculator";
string codeClassName = "MathTestCalculator";
string codeMethodName = "Value";
Int64 I64Value = 0;
StringBuilder codeBuilder = new StringBuilder();
codeBuilder
.Append("using System;'n")
.Append("namespace ").Append(codeNamespace).Append(" {'n")
.Append("class ").Append(codeClassName).Append("{'n")
.Append("public Int64 ").Append(codeMethodName).Append("(){'n")
.Append("return (Int64)(").Append(mathKey).Append(");}}}'n");
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults results = CodeDomProvider
.CreateProvider("CSharp")
.CompileAssemblyFromSource(cp, codeBuilder.ToString());
if (results.Errors.Count > 0)
{
StringBuilder error = new StringBuilder();
error.Append("Unable to evaluate: '").Append(mathKey).Append("''n");
foreach (CompilerError CompErr in results.Errors)
{
error
.Append("Line number ").Append(CompErr.Line)
.Append(", Error Number: ").Append(CompErr.ErrorNumber)
.Append(", '").Append(CompErr.ErrorText).Append(";'n");
}
throw new Exception(error.ToString());
}
else
{
Type calcType = results.CompiledAssembly.GetType(codeNamespace + "." + codeClassName);
object o = Activator.CreateInstance(calcType);
I64Value = (Int64)calcType.GetMethod(codeMethodName).Invoke(o, null);
}
return new KeyValuePair<string, long>(mathKey, I64Value);
}