How to have access to instance members in a static method?

后端 未结 2 1891
星月不相逢
星月不相逢 2020-12-21 19:56

I\'m trying to create classes to encapsulate validation and logic for objects like Email , URL , phone number and so on . in the first try I found that I\'m repeating the sa

相关标签:
2条回答
  • 2020-12-21 20:33

    so , how can I have access to PropOne and PropTwo in this static method ?

    You need to have an instance somehow, otherwise it's a meaningless operation. So the question is - how would you want to identify the instance that you're interested in? Do you really need Validate to be static at all?

    Note that instead of having abstract properties, if you expect the values to always stay the same throughout an instance of the class, you might want to make the values part of the constructor for BaseClass instead, and just keep them in fields.

    If what you're trying to achieve is that each subclass has a single separate validator, I would separate the two concerns - give each subclass a static property of a different type. You wouldn't be able to call this polymorphically, but it sounds like you don't really want to anyway.

    We can't really tell what your classes are meant to represent here - if you can give us more concrete context, we can probably be more helpful.

    0 讨论(0)
  • 2020-12-21 20:51

    you can try making IsValid an extension method.

    0 讨论(0)
提交回复
热议问题