How to use method parameter attributes

前端 未结 3 515
故里飘歌
故里飘歌 2021-01-04 05:58

I\'ve been struggling to find examples of how to write a custom attribute to validate method parameters, i.e., turn this form:

public void DoSomething(Client         


        
3条回答
  •  旧时难觅i
    2021-01-04 06:53

    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.

提交回复
热议问题