From playing hyper-masculine Barney Stinson to transsexual Hedwig, how the onetime child star met himself