How to use method parameter attributes

ぐ巨炮叔叔 提交于 2019-11-30 18:39:56

You're applying it correctly - but an attribute basically doesn't know the member it refers to. This definitely makes life harder.

Not only does it not have access to the member that it refers to, but that member would be a ParameterInfo, not a Client - there's no easy way of accessing the value of a parameter externally. Your method would need to call some helper code, passing the value of client in order to handle it appropriately... or you need to hook into the code which is going to call your method to start with, in order to notice the attribute.

It's not clear exactly how you were hoping to use this, but it may well be that you need to change your design significantly.

Attributes are not enough for doing it.

If I understood you correctly you want to add an attribute on a parameter in order to validate it at run time and that is impossible only with attributes.

It is impossible because attributes are only "metadata" and not executed code.

You will need some "real" code to read it and act accordingly. That code can be injected at compile time or you can hook into the function execution.

Attributes probably should be put on the method itself. When I was searching for the solution I found the following link and the way it uses interceptor seems even better http://www.codinginstinct.com/2008/05/argument-validation-using-attributes.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!