Inconsistent accessibility error when using a less accessible class as a generic type parameter

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have the following situation:
<div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white
<table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0>
<colgroup>
<col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap
<tbody>
<tr>
<td><font style="font-size:11px </font><font style="color:green // A public interface of some kind </font><font style="font-size:11px  </font></td></tr>
<tr>
<td style="background-color:#f7f7f7 </font><font style="color:blue public</font><font style="font-size:11px  </font><font style="color:blue interface</font><font style="font-size:11px  IMyInterface  </font></td></tr>
<tr>
<td>{  </td></tr>
<tr>
<td style="background-color:#f7f7f7     </font><font style="color:blue int</font><font style="font-size:11px  Something { </font><font style="color:blue get</font><font style="font-size:11px ; </font><font style="color:blue set</font><font style="font-size:11px ; }  </font></td></tr>
<tr>
<td>}  </td></tr>
<tr>
<td style="background-color:#f7f7f7  </td></tr>
<tr>
<td></font><font style="color:green // An internal class that implements the public interface. </font><font style="font-size:11px  </font></td></tr>
<tr>
<td style="background-color:#f7f7f7 </font><font style="color:green // Despite the internal/public mismatch, this works. </font><font style="font-size:11px  </font></td></tr>
<tr>
<td></font><font style="color:blue internal</font><font style="font-size:11px  </font><font style="color:blue class</font><font style="font-size:11px  MyInternalConcrete : IMyInterface  </font></td></tr>
<tr>
<td style="background-color:#f7f7f7 {  </td></tr>
<tr>
<td>    </font><font style="color:blue public</font><font style="font-size:11px  </font><font style="color:blue int</font><font style="font-size:11px  Something { </font><font style="color:blue get</font><font style="font-size:11px ; </font><font style="color:blue set</font><font style="font-size:11px ; }  </font></td></tr>
<tr>
<td style="background-color:#f7f7f7 }  </td></tr>
<tr>
<td> </td></tr>
<tr>
<td style="background-color:#f7f7f7 </font><font style="color:green // A generic class with an interface-restricted type parameter. </font><font style="font-size:11px  </font></td></tr>
<tr>
<td></font><font style="color:green // The instance is *never* exposed as a public, or even protected member. </font><font style="font-size:11px  </font></td></tr>
<tr>
<td style="background-color:#f7f7f7 </font><font style="color:blue public</font><font style="font-size:11px  </font><font style="color:blue class</font><font style="font-size:11px  MyClass<T> where T : IMyInterface, </font><font style="color:blue new</font><font style="font-size:11px ()  </font></td></tr>
<tr>
<td>{  </td></tr>
<tr>
<td style="background-color:#f7f7f7     T myInterfaceInstance;  </td></tr>
<tr>
<td> </td></tr>
<tr>
<td style="background-color:#f7f7f7     </font><font style="color:blue public</font><font style="font-size:11px  MyClass()  </font></td></tr>
<tr>
<td>    {  </td></tr>
<tr>
<td style="background-color:#f7f7f7         myInterfaceInstance = </font><font style="color:blue new</font><font style="font-size:11px  T();  </font></td></tr>
<tr>
<td>    }  </td></tr>
<tr>
<td style="background-color:#f7f7f7 }  </td></tr>
<tr>
<td> </td></tr>
<tr>
<td style="background-color:#f7f7f7 </font><font style="color:green // Attempting to implement concrete class... Inconsistent Accessibility Error! </font><font style="font-size:11px  </font></td></tr>
<tr>
<td></font><font style="color:blue public</font><font style="font-size:11px  </font><font style="color:blue class</font><font style="font-size:11px  MySpecificClass : MyClass<MyInternalConcrete>  </font></td></tr>
<tr>
<td style="background-color:#f7f7f7 {  </td></tr>
<tr>
<td>} </td></tr></tbody></table>
When trying to implement MySpecificClass, I get the error "Inconsistent accessibility: base class WindowsFormsApplication1.MyClass<WindowsFormsApplication1.MyInternalConcrete> is less accessible than class WindowsFormsApplication1.MySpecificT".
All classes are in the same module.
I somewhat understand why this would be happening,  but I wonder why does it restrict me using mixed accessibility type parameters if they are never exposed to a point where a non-internal method might access it. In fact, the very point of creating the MySpecificClass is so I can use it from a different module without having to touch the internal class. The compiler is smart enough to detect accessibility conflicts method by method, so technically, this should be legal.
Is this a limitation of the C# generics or am I doing something wrong?

View the full article
 
Back
Top