在C中,可以编写宏函数,将输入替换为字符串。
#define x(k) {#k, k}
'(4)‘将生成{“4”,4}’
我在C#中有一个usecase,其中我想将这样的输入传递给一个单元测试。
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
。
发布于 2014-09-08 11:36:12
下面的片段完成了我所需要的功能,但是它运行得非常慢。
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);
}
发布于 2014-09-04 14:15:23
您可以在重载运算符中使用自定义数字类。
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;
}
}
https://stackoverflow.com/questions/25667310
复制相似问题