我遇到以下异常:
System.ArgumentException: 'Only constant and single-dimensional array expressions are supported'
在尝试使用在构造函数中接受参数的附加属性来伪造一些抽象对象时。
var foo = A.Fake<SelfComplementaryCustomizableTupleConsumer>(
opt => opt.WithAttributes(
() => new RequiredVariableNameAttribute(requiredVariableName,requiredVariableType)
)
);
值得一提的是,如果我不带参数调用构造函数,一切都很好。更令我困惑的是,如果我用常量替换变量,问题也不会出现。
完整代码:
string requiredVariableName = "abc";
Type requiredVariableType = typeof(string);
var foo = A.Fake<SelfComplementaryCustomizableTupleConsumer>(
opt => opt.WithAttributes(
() => new RequiredVariableNameAttribute(requiredVariableName,requiredVariableType)
)
);
var requiredVariables = foo.GetRequiredVariables();
Assert.IsTrue(requiredVariables.TryGetValue(requiredVariableName, out Type tmp));
最佳答案
这是由于 how the attribute creation expression is analyzed .它不支持所有可能的表达式,因为它会非常复杂。我想可以处理局部变量的情况,但代码在 CaSTLe.Core(FakeItEasy 使用)中,所以不要指望很快修复。同时,如果可以的话,在表达式中使用常量;如果没有,另一种方法是手动构建表达式(使用 Expression.Lambda<Func<Attribute>>(...)
)。
编辑:我打开了一个 issue在 CaSTLe.Core 仓库中。
关于c# - FakeItEasy 伪造带有构造函数中参数的自定义属性的抽象类抛出 ArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54044725/