There is a sample of dynamic master pages here
...go down to the section 'Attaching Master Pages Dynamically'...
I am able to copy and paste it here and get it working as stated. I cannot get it to work if my Master pages have code behind files. I get a 'Compilation Error' that there is a conflict between BaseMaster and what appears to be another server created master page. I tried inheriting both BaseMaster and also System.Web.UI.MasterPage but both give the error.
So my question is how do I get it to work when the blue and green master pages have code behind files?