Hi Ohad,
Thank you for posting in the community!
Based on my understanding, you want to know if the interface and implement
class have different accessibility, what will be the result accessibility
of the methods.
==========================================
I think the community member Stoitcho has provided you a full description
and example about all the possible issue.(Thanks for his contribution to
the community!)
But I want to add some more information.
First, "Interface members implicitly have public declared accessibility. No
access modifiers are allowed on interface member declarations.", so in
Stoitcho's case 3, the explicit implement of interface should not be
applied any access modifier(Public should be removed). Anyway, this is a
little thing
For the test of Stoitcho's sample, in case 2 and 3, because the class is
marked as "internal", so you can not create an instance of the class in
EXTERNAL assembly. I think you should use a "help" class to return a
reference of the class(Actually it is a reference to the interface) for
external assembly.
For case 4(The interface and class are both internal), there is no way to
use them, but you can use Reflection to invoke the member of the class and
interface, I provide a sample in the code. Reflection is powerful tool,
which can even invoke a class's private member. But it needs a high
privilege on that code.(Which is not recommanded to use, this is just a
sample for you.)
Sample code like this:
//The implement assembly code
namespace internalinterface
{
internal interface IFoo1
{
void Method();
}
public class Bar1:IFoo1
{
public void Method()
{
Console.WriteLine("Bar1 output");
}
}
public interface IFoo2
{
void Method();
}
internal class Bar2:IFoo2
{
public void Method()
{
Console.WriteLine("Bar2 output");
}
}
public interface IFoo3
{
void Method();
}
internal class Bar3:IFoo3
{
void IFoo3.Method()
{
Console.WriteLine("Bar3 output");
}
}
internal interface IFoo4
{
void Method();
}
internal class Bar4:IFoo4
{
void IFoo4.Method()
{
Console.WriteLine("Bar4 output");
}
}
public class helpIFoo2
{
public IFoo2 getinterface()
{
Bar2 obj=new Bar2();
return (IFoo2)obj;
}
}
public class helpIFoo3
{
public IFoo3 getinterface()
{
Bar3 obj=new Bar3();
return (IFoo3)obj;
}
}
}
//The test assembly's code
class Class1
{
[STAThread]
static void Main(string[] args)
{
internalinterface.Bar1 obj1=new internalinterface.Bar1();
obj1.Method();
internalinterface.helpIFoo2 h2=new internalinterface.helpIFoo2();
internalinterface.IFoo2 obj2=h2.getinterface();
obj2.Method();
internalinterface.helpIFoo3 h3=new internalinterface.helpIFoo3();
internalinterface.IFoo3 obj3=h3.getinterface();
obj3.Method();
try
{
Assembly ass=Assembly.LoadFrom("internalinterface.dll");
object objclass=ass.CreateInstance("internalinterface.Bar4");
Type interfacetype=ass.GetType("internalinterface.IFoo4");
if(objclass!=null)
{
MethodInfo meth=interfacetype.GetMethod("Method");
meth.Invoke(objclass,new object[]{});
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
==========================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.