是否可以模拟自定义属性 c#?
例如:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class YourAttribute : Attribute
{
}
[Your]
public void MyMethod()
{
//something
}
我想测试MyMethod,但我需要模拟属性Your。
最佳答案
是的,您可以通过 TypeDescriptor 向实例或类型添加属性;以下示例使用 Moq,但它可以应用于其他地方。
var mock = new Mock<IMyInterface>();
var attribute = new YourAttribute();
TypeDescriptor.AddAttributes(mock.Object, attribute);
关于c# - 可以模拟自定义属性 c# 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26528418/