MethodAccessException with Activator.CreateInstance

Hi

I have a simple windows application project (MyProject) with one Form (MyForm) and one user control (MyControl)

I'm trying to create a new instance of MyControl by using the Activator.CreateInstance method, this failed with the inner exception:

System.MethodAccessException {MyProject.MyControl.InitializeComponent}

the help says that this exception occurs when trying to access a private or protected method, but in my case the method I'm accessing by the Activator.CreateInstance is the default constructor which is not private or protected

After many trials I found that the this exception happens ONLY if MyControl implements an interface!!

I went further and created a new UserControl (NewControl) which inherits MyControl, I added a button: btnDownload and tried to run method again, this time the exception is strange:

System.MethodAccessException {MyProject.NewControl.get_BtnDownload()}

I donnt have a 'get_btnDownload' method in my 'NewControl'!!!!!

one more thing: if I try to create an instance of NewControl in the trivial way, it works !! :

Dim C as new NewControl

the frustrating thing is that the Activator.CreateInstance have been working for more than 2 months and now its raising an exception

I'm working on Visual Studio Orcas Beta 1

Any Ideas?!

Thanks

[1403 byte] By [INVENTBroadcast] at [2008-3-5]
# 1

>
I donnt have a 'get_btnDownload' method in my 'NewControl'!!!!!

But you do realise that refers to the getter of your property BtnDownload ?

I'm having the same problem - for me it works under ASP.NET but not a Win Form used at design time by VS. I think it has something to do with this -> http://web.archive.org/web/20070112153238/http://www.gotdotnet.com/team/clr/LoadFromIsolation.aspx

I've seen other posts mentioning interfaces being an issue (although I dont have one) - I think it's caused by a mismatch between the interface the Activator sees and the interface that your class is referencing (I know they're the 'same' but to the runtime at that moment, they aren't). It's kind of like creating 2 different DLLs with 'identical' copies of the interface in them - they are the 'same', but to the runtime, they aren't.

I'm still scratching my head on this one.

jdw-75 at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Visual Basic Orcas...
# 2
Well, I solved my problem - basically it was down to how VS loads assemblies (it shallow copies design time assemblies to it's own folder, unless they're in the GAC)

http://www.dotnet247.com/247reference/msgs/41/208479.aspx

Stick your assembly holding the interface into the gac and it might work.

jdw-75 at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Visual Basic Orcas...

Visual Studio Orcas

Site Classified