For decades — at least since Orson Welles scared the daylights out of radio listeners with “War of the Worlds” back in 1938 — the public has embraced the terrifying prospect of alien invasion. But what if, notwithstanding the occasional humanist fable like “E.T.,” all those movies and television programs have been inculcating a potentially toxic form of interplanetary prejudice?

“District 9,” a smart, swift new film from the South African director Neill Blomkamp (who now lives in Canada and who wrote the screenplay with Terri Tatchell), raises such a possibility in part by inverting an axiomatic question of the U.F.O. genre. In place of the usual mystery — what are they going to do to us? — this movie poses a different kind of hypothetical puzzle. What would we do to them? The answer, derived from intimate knowledge of how we have treated one another for centuries, is not pretty.

A busy opening flurry of mock-news images and talking-head documentary chin scratching fills in a grim, disturbingly plausible scenario. Back in the 1980s a giant spacecraft stalled in the skies over Johannesburg. On board were a large number of starving and disoriented creatures, who were rescued and placed in a temporary refugee camp in the part of the city that gives the film its title. Over the next 20 years the settlement became a teeming shantytown like so many others in the developing world, with the relatively minor distinction of being home to tall, skinny bipeds with insectlike faces and bodies that seem to combine biological and mechanical features. Though there is evidence that those extraterrestrials — known in derogatory slang as prawns because of their vaguely crustacean appearance — represent an advanced civilization, their lives on Earth are marked by squalor and dysfunction. And they are viewed by South Africans of all races with suspicion, occasional pity and xenophobic hostility.

The South African setting hones the allegory of “District 9” to a sharp topical point. That country’s history of apartheid and its continuing social problems are never mentioned, but they hardly need to be. And the film’s implications extend far beyond the boundaries of a particular nation, which is taken as more or less representative of the planet as a whole.

No group, from the mostly white soldiers and bureaucrats who corral and abuse the prawns to the Nigerian gangsters who prey upon the aliens and exploit their addiction to cat food, is innocent. And casual bigotry turns out to be the least of the problems facing the exiles. As it progresses, “District 9” uncovers a horrific program of medical experimentation yoked to a near-genocidal agenda of corporate greed. A company called M.N.U. (it stands, none too subtly, for Multi-National United) has taken over administration of the prawn population, which means resettling the aliens in a remote enclosure reminiscent of the Bantustans of the apartheid era.

The M.N.U. executive charged with carrying out this program is Wikus van der Merwe (Sharlto Copley), a nervous nebbish whose father-in-law (Louis Minnaar) is the head of the company. Cowardly, preening and hopeless at projecting authority, Wikus is the kind of guy who gives nepotism a bad name. It says a lot about Mr. Blomkamp’s sense of humor, and about his view of his own species, that this pathetic little paper pusher is his chosen agent of mankind’s potential moral redemption.

But I’m getting ahead of the story, and perhaps overselling the allegory. Not that the metaphorical resonances of “District 9” aren’t rich and thought provoking. But the filmmakers don’t draw them out with a heavy, didactic hand. Instead, in the best B-movie tradition, they embed their ideas in an ingenious, propulsive and suspenseful genre entertainment, one that respects your intelligence even as it makes your eyes pop (and, once in a while, your stomach turn).

The early pseudo-documentary conceit, which uses footage that pretends to have been harvested from news choppers and security cameras as well as some by the unseen crew accompanying Wikus on his tour of the prawn camp, fades away after a while. The academic authorities do too, having served the dual functions of providing narrative exposition and demonstrating the high-minded uselessness of official liberal discourse.

An error has occurred. Please try again later.

You are already subscribed to this email.

Once a terrible accident befalls Wikus, we are at his side and under his skin, and “District 9” subtly shifts from speculative science fiction to zombie bio-horror and then, less subtly, turns into an escape-action-chase movie full of explosions, gunplay and vehicular mayhem.

In the midst of it all you almost take for granted the carefully rendered details of the setting, the tightness of the editing and the inventiveness of the special effects. Not the least of these are the aliens themselves, who are made expressive and soulful without quite being anthropomorphized. (Their whirring, clicking speech, partly understood by Wikus and others who work with the creatures, is translated for the rest of us via subtitles.)

One in particular, named Christopher Johnson (Jason Cope), becomes Wikus’s protector and ward, and their relationship turns “District 9,” in its final act, into an intergalactic buddy picture, with some intriguing (and also possibly disappointing) sequel opportunities left open.

At its core the film tells the story — hardly an unfamiliar one in the literature of modern South Africa — of how a member of the socially dominant group becomes aware of the injustice that keeps him in his place and the others, his designated inferiors, in theirs. The cost he pays for this knowledge is severe, as it must be, given the dreadful contours of the system. But if the film’s view of the world is bleak, it is not quite nihilistic. It suggests that sometimes the only way to become fully human is to be completely alienated.

“District 9” is rated R (Under 17 requires accompanying parent or adult guardian). It has intense violence and violent swearing in the languages of two planets.

DISTRICT 9

Opens on Friday nationwide.

Directed by Neill Blomkamp; written by Mr. Blomkamp and Terri Tatchell; director of photography, Trent Opaloch; edited by Julian Clarke; production designer, Philip Ivey; music by Clinton Shorter; produced by Peter Jackson and Carolynne Cunningham; released by TriStar Pictures. Running time: 1 hour 51 minutes.