weird?? inaccessible due to its protection level error

M

milund

I have a "funny" after upgrading to .NET2.0.

The following code is placed inside an unsafe method in assembly "A"

System.Object myIUnknownObject =
Marshal.GetObjectForIUnknown((System.IntPtr)m_someClass.MyProperty);

SomeClass is declared public in assembly "B".
MyProperty get is declared public and written in managed C++ (old
syntax) like this.

__property IUnknown* get_MyProperty()
{
return (IUnknown*)m_pStuff->GetStuff();
}


This works fine when assembly "B" is compiled under VS2003 and .NET1.1
When assembly "B" is compiled under VS2005 and .NET2.0 I get this
error (when compiling assembly "A"): "error CS0122:
'B.SomeClass.MyProperty' is inaccessible due to its protection level"

In both cases assembly "A" is compiled with VS2005 and .NET2.0

What has changed from .NET1.1 to .NET2.0 and how do I correct this?
(Note that I actually can get it to work using reflection, but that is
not the point - I would like to understand what is wrong here).

thanks,
Michael
 
J

Jon Skeet [C# MVP]

What has changed from .NET1.1 to .NET2.0 and how do I correct this?
(Note that I actually can get it to work using reflection, but that is
not the point - I would like to understand what is wrong here).

I suggest you have a look at assembly B (and particularly MyProperty)
using Reflector, compiled first with 2003 and then with 2005. My guess
is that you'll find the accessibility changes...
 
M

milund

I suggest you have a look at assembly B (and particularly MyProperty)
using Reflector, compiled first with 2003 and then with 2005. My guess
is that you'll find the accessibility changes...

Looking in reflector I see for both versions of assembly B:
public __gc class SomeClass: public System::IDisposable
{
<snip>
public: __property IUnknown __gc** get_MyProperty();
<snip>
}

Also dissasembling the getter method shows the same code.
 
J

Jon Skeet [C# MVP]

Looking in reflector I see for both versions of assembly B:
public __gc class SomeClass: public System::IDisposable
{
<snip>
public: __property IUnknown __gc** get_MyProperty();
<snip>

}

Also dissasembling the getter method shows the same code.

So, given that there must be *some* difference between the two
assemblies (if one works and the other doesn't) - run them both
through ildasm and run diff on the generated files...

Jon
 
M

milund

So, given that there must be *some* difference between the two
assemblies (if one works and the other doesn't) - run them both
through ildasm and run diff on the generated files...

Jon

Unfortunately that doesn't tell me very much. (It is a big assembly
and stuff has moved around - I have tried to compare all sections
involving MyProperty and I can't see anything strange...
I also tried to run ildasm with the /pubonly flag - and MyProperty
turns up fine.

I wonder if it has anything to do with the unsafe keyword - it is just
about the only place we use it...

Michael
 
G

Guest

I had a bit of code that I received this error on after "upgrading" to Net
2.0... the method in question was listed as protected originally. After I
changed it to protected internal, it worked fine.
 
A

Alvin Bruney [MVP]

Actually, there is quite a bit of issues with migration stemming from the
wizard. Even in the case of a smooth compile, there may be run-time issues.
Unit tests can help in that regard.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top