Switch the prefab of a game object from a script

If I drag&drop a prefab onto a gameobject in the hierarchy while holding the alt-key, the gameobject will be replaced with an instance of that prefab, while maintaining its name and transform. How can I do this in script?

http://www.unifycommunity.com/wiki/index.php?title=ReplaceSelection does something like this

leave the blank gameobject as a parent to the object you intend to replace.

when you wish to change it, have your script destroy the child object then:

instantiate the replacement,

parent

local rotation and local position set to "0"

and you're done