It worked like a charm, though at a huge cost, both to the targeted foreigners and to the country rejecting them. For example, the Danish state spent thousands of kroner (a krone is worth about 15 cents) prosecuting the respected Colombian economist Jimmy Martínez-Correa, a professor at the Copenhagen Business School, on the same charges leveled against me; he was acquitted 18 months later. Rachel Bullen, an Australian oboist prosecuted for the crime of playing guest sets with Danish orchestras, was not so lucky: She was convicted and left Denmark with her career in tatters; with a criminal record, she found it hard to get work even in her native country.

I don’t know what became of the other non-Europeans (about a dozen) who were charged at the same time as I was; some contacted me privately, saying they were so fearful of the xenophobia whipped up in the Danish public sphere that they dare not speak to the media.

As the story of my case spread, Danish government officials tried to do damage control, taking to mass media and Twitter to say that Denmark would never want to discourage immigrants from sharing their skills and knowledge. But it was already too late: Denmark started to lose things it cared about.

Most notable, it may have contributed to the loss of the jobs and prestige that would have come with the relocation to Copenhagen of the European Medicines Agency, a European Union office that tested pharmaceuticals. The Danish government had spent months and millions of kroner wooing the agency, which had decided to move from London to the Continent after the Brexit decision. But the publicity surrounding the charges I faced may have spooked the agency’s multinational staff members.

For me, the nightmare ended after eight months; Denmark changed its laws so that the charges against me were dropped. I was more fortunate than Ms. Bullen in escaping the lifetime stigma of criminal conviction; but like her, I returned to my native country determined to warn of what we stand to lose in continuing down the path set by ultranationalist policymakers.

I saw that the hatred of others is contagious and inevitably spreads to the people a country most needs to survive, whether they be farm laborers or physicians. This ultimately comes into conflict with the country’s economy and its aspirations.

The humanitarian disaster of the Trump administration’s treatment of migrants has already been documented in gut-wrenching detail. Less recognized, however, is the destruction such policies wreak on the society, economy and long-term development of countries that attempt to rid themselves of immigrants.