起订量和参数属性继承

发布于 2024-08-24 04:41:51 字数 2172 浏览 2 评论 0原文

当我尝试使用 Moq 模拟一个类时,方法属性会继承到模拟类,但不会继承参数属性。

基本上,“let a = (ArgumentsAttribute) p.GetCustomAttributes(typeof (ArgumentsAttribute), true).SingleOrDefault()”行不会返回属性。运行代码以查看失败的位置。

我怎样才能通过这个测试?

[TestFixture]
public class MyTests {
    [Test]
    public void shouldFindAndCallMethodWithAttributes() {
        var myInterface = new Mock<MyClass>();
        myInterface.Setup(x => x.MyMarkedMethod(1));
        myInterface.Setup(x => x.MyMarkedMethod(5));
        myInterface.Setup(x => x.MyMarkedMethod(9));

        var executor = new MarkedMethodExecutor();
        executor.FindAndCallMethodWithAttributes(myInterface.Object);

        myInterface.VerifyAll();
    }
}

public class MarkedMethodExecutor {
    public void FindAndCallMethodWithAttributes(object anObject) {
        var methods = from m in anObject.GetType().GetMethods()
                      where m.GetCustomAttributes(typeof (ExecuteMeAttribute), true).SingleOrDefault() != null
                      select m;
        foreach (var method in methods) {
            var callInfos = from p in method.GetParameters()
                            let a = (ArgumentsAttribute) p.GetCustomAttributes(typeof (ArgumentsAttribute), true).SingleOrDefault()
                            where a != null
                            select new {Parameter = p, Attribute = a};
            // assume its one argument here for simplicity..
            var attribute = callInfos.Single().Attribute;
            foreach (var argument in attribute.Arguments) {
                method.Invoke(anObject, new[] {argument});
            }
        }
    }
}

public class MyClass {
    [ExecuteMe]
    public virtual void MyMarkedMethod([Arguments(1, 5, 9)] int arg) {}
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ExecuteMeAttribute : Attribute {}

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public class ArgumentsAttribute : Attribute {
    public readonly object[] Arguments;

    public ArgumentsAttribute(params object[] arguments) {
        Arguments = arguments;
    }
}

When I try to mock a class using Moq the method attribute gets inherited to the mock class, but not the parameter attribute.

Basically the row "let a = (ArgumentsAttribute) p.GetCustomAttributes(typeof (ArgumentsAttribute), true).SingleOrDefault()" doesnt return the attribute.. Run the code to see where it fails.

How do I make this test pass?

[TestFixture]
public class MyTests {
    [Test]
    public void shouldFindAndCallMethodWithAttributes() {
        var myInterface = new Mock<MyClass>();
        myInterface.Setup(x => x.MyMarkedMethod(1));
        myInterface.Setup(x => x.MyMarkedMethod(5));
        myInterface.Setup(x => x.MyMarkedMethod(9));

        var executor = new MarkedMethodExecutor();
        executor.FindAndCallMethodWithAttributes(myInterface.Object);

        myInterface.VerifyAll();
    }
}

public class MarkedMethodExecutor {
    public void FindAndCallMethodWithAttributes(object anObject) {
        var methods = from m in anObject.GetType().GetMethods()
                      where m.GetCustomAttributes(typeof (ExecuteMeAttribute), true).SingleOrDefault() != null
                      select m;
        foreach (var method in methods) {
            var callInfos = from p in method.GetParameters()
                            let a = (ArgumentsAttribute) p.GetCustomAttributes(typeof (ArgumentsAttribute), true).SingleOrDefault()
                            where a != null
                            select new {Parameter = p, Attribute = a};
            // assume its one argument here for simplicity..
            var attribute = callInfos.Single().Attribute;
            foreach (var argument in attribute.Arguments) {
                method.Invoke(anObject, new[] {argument});
            }
        }
    }
}

public class MyClass {
    [ExecuteMe]
    public virtual void MyMarkedMethod([Arguments(1, 5, 9)] int arg) {}
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ExecuteMeAttribute : Attribute {}

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public class ArgumentsAttribute : Attribute {
    public readonly object[] Arguments;

    public ArgumentsAttribute(params object[] arguments) {
        Arguments = arguments;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤城病女 2024-08-31 04:41:51

我不明白你的问题:你想设置属性吗?

您可以使用函数 It.is(match)。也许有帮助:
http://api.moq.me/html/5976987C.htm

I do not understand your problem: do you want to set the attributes?

You could use the function It.is(match). Maybe it helps:
http://api.moq.me/html/5976987C.htm

得不到的就毁灭 2024-08-31 04:41:51

使用 TypeDescriptor.AddAttributes 方法可以工作吗?我很想知道:

var myInterface = new Mock<IRepository>();
var attribute = new ExecuteMeAttribute();
someInstanceTypeDescriptor.AddAttributes(myInterface.Object, attribute);

Would futzing with the TypeDescriptor.AddAttributes method work? I'd be curious to know:

var myInterface = new Mock<IRepository>();
var attribute = new ExecuteMeAttribute();
someInstanceTypeDescriptor.AddAttributes(myInterface.Object, attribute);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文