【发布时间】:2019-01-04 19:10:49
【问题描述】:
我收到以下异常:
System.ArgumentException: '仅支持常量和一维数组表达式'
在尝试使用带有构造函数参数的附加属性来伪造一些抽象对象时。
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));
【问题讨论】:
标签: c# lambda custom-attributes fakeiteasy