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

主宰稳场 提交于 2019-11-29 16:57:26

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.

you can try making IsValid an extension method.

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