This article is mostly intended for snipers, but some of the advice will apply to other infiltrators as well.
First of all, do you best to avoid getting caught in the first place. Infiltrator have access to cloaking and motion detection tools, you should be the one ambushing people, not the vice versa. Still, even the best of us sometimes get surprised by an enemy at close range, and then you have several options to choose from:
(1) If at all possible, try to escape. You’re always gonna be at a disadvantage against anyone with an automatic weapon in close quarters. No need to risk and try to outplay if you can run and engage later on your terms.
If running isn’t an option:
(2) Killing with your primary weapon. If you’re using a bolt action rifle, take a page from Elusive’s book and try to get a headshot. Semi auto snipers have workable hip fire at close ranges.
(3) If you don’t want to stake your life on sniper rifle proficiency, engaging with sidearm is your next option. Sidearms have excellent hip fire accuracy, quite more spammable than sniper rifles, and can have surprisingly decent TTK if you can hit headshots. Revolvers are especially good, as they usually can kill an enemy in 1 headshot + 1 bodyshot.
(4) Melee combo with your sniper rifle. It best works with bolt action rifles, as they can reliably melee combo even through nanoweave after one shot. This is a solid option when you can quickly get close to the enemy. Have a habit to queue equipping the sidearm or knife right after melee combo. This will be your next option if melee combo fails.
(5) Melee combo with a sidearm. Those don’t usually work very well. Most sidearms require quite a few shots to enable a quick melee kill, and in general melee combos with semi auto weapons are clunky and hard to execute properly.
If you’re good, and using a 250+ damage sidearm, you could go for one headshot + quick melee combo, but otherwise, purposefully going for a melee combo with a sidearm is usually a bad idea. If you and the enemy happen to close the distance, and you feel like you’ve dealt enough damage to kill with melee, then go for it. Think of it as of something that happens naturally rather than your initial goal.
(6) Wielded melee attacks. Usually, these don’t work well against aware players, especially if they have the initiative. All it takes for them to nullify you as a threat is just turn around and take a few steps back, and if they realize that, you’re dead. You could go for wielded melee strikes after a failed melee combo, but IMO going for a sidearm is a better option. Wielded melee is a powerful tool, just not when you get caught.
One exception is if you have a power knife, and an enemy caught you near a piece of cover. If you can use that piece of cover to equip and activate the knife, and then use the clientside advantage to run out and stab them in the face, then it will work great – assuming they chase you rather than just circle around cover and shoot you before you can close the distance.
It’s all situational and depends a lot on your immediate surrounding and enemy behavior. If you can’t run, engaging with a sidearm is the most reliable and independent option, so this should be your go-to option. There’s a reason why every infiltrator is expected to be good with their sidearm.