“well the answer to your immediate question is X but that’s a pretty strange thing to ask for because of reason Y. Can you tell me more about the wider problem you’re trying to solve?”
This is the right way to answer these questions. SO was famous for the infuriating "You shouldn't do X, so as a favor to you I'm not going to tell you how." or "Before I answer thee, thou must first answer me these questions three!" kinds of answers.
Edit: Don't want to take credit for this. It's a quote from the article.
Your way is good because it answers my question first. I've been wanting to do a weird thing X and had to first spend half an hour convincing people that yes, I really do know what I'm doing, so they'd give me the one-liner answer I came for.
Ultimately, it should be on me to choose whether I want to risk doing X or whether I want to take up your offer to explain Y, not the "as a favor to you, I'm not going to tell you how" way you described.