Inspired by Is it possible to use creatures from an RPG (like Dungeons and Dragons) in writing a book without infringing on copyrights?, I did some research into whether fanfiction is legal. The answer seems to be no, with a single exception: you are allowed to critique or parody the original work (case #3 of the link).
If I take the characters of a well-known piece of work and cast them in a completely different genre, is that legal? For example, suppose I take Harry Potter & Hermione Granger (two characters from the Harry Potter series, which is in the fantasy genre), put them in Hogwarts (a location from the same series) and write a bunch of sex scenes. Effectively, I've written my Harry Potter fanfiction in the erotica genre, which is very different from the fantasy genre.
Would this qualify as a critique or parody, or be legal in some other way? Or would it not matter, and therefore be illegal?