Problem using interfaces

  • Thread starter Thread starter Mats-Lennart Hansson
  • Start date Start date
M

Mats-Lennart Hansson

Hi,
I want to use interfaces, but somehow it doesn't work as I want to. I have
these compoents:

1. IClass
A component that holds an interface for a class.

2. Class : IClass
A component that holds a class that implements the IClass interface.

3. UseClass
A component that wants to use Class (this component doesn't care of the
interface)

However, when trying to compile I get an error that says my UseClass needs a
reference to the IClass component as well. Why?

Thanks,
Mats-Lennart
 
Are you declaring IClass, Class and UseClass in different assemblies?

The framework needs to load the assembly that has IClass declared when you try to use the Class implementation in code. This is
because the assembly that contains the Class definition implements IClass.

Think about this: How can the framework reference a class that has a public interface member implementation, without the interface
definition? The metadata of the Class assembly just contains a reference to the interface in another assembly, but does not contain
the actual definition. So, UseClass must reference the assembly that contains the interface definition as well as the Class
assembly.
 
Yes, I have them in different assemblies. I see your point now, it just felt
odd that I needed to reference the interface even from "behind" the
interface structure.

Thanks for a fast and good answer!

/Mats-Lennart
 
Back
Top