The interface is just an interface, that the class implements.
When I use GetInterface, it says that the class implements
my interface, but when I want to cast it doesn't want anymore.
To be more explicit, I'll include the code for the interface and
class.
public interface iType1
{
}
public class Type1 : iType1
{
}
and that's it... I compile it and make a dll then ...
Originally posted by trobert I don't understand your question.
The interface is just an interface, that the class implements.
When I use GetInterface, it says that the class implements
my interface, but when I want to cast it doesn't want anymore.
To be more explicit, I'll include the code for the interface and
class.
public interface iType1
{
}
public class Type1 : iType1
{
}
and that's it... I compile it and make a dll then ...
Sorry I wasn't clear. I just wanted to make sure that you weren't defining the same interface twice ( once in each assembly )
You might be right. I include the interface in both
assemblies, because the dll needs it to implement
it, and the exe needs it to cast.
But the namespace is the same for everybody.
And I don't know if it is possible to reference an
interface from an assembly... probably not.
By the way, can I cast to a variable that is a instance
of Type?
Thanks a lot jparsons, you were right.
I know c++ and java, but I am kind of new
with c#. So I didn't understand very well
in the beginning about the references...
I have been having the exact same problem. I didn't have the same interface defined twice in the two assemblies, but I referenced the dll assembly and used the interface referenced in the dll assmbly for casting. I still got the casting error. Do the interface and class have to be in the same namespace? Any other helpful infromation would be appreciated. Thanks, Marko
My mistake, if I recall well, was that when I was compiling using csc I included the file with the interface in both dll. I didn't know then to use /r in csc
I understand that you don't have the same problem...
About the namespace, I had only one namespace, but I don't think there would be any problem... still you have to use the complete name of the class or interface (namespace.class) when casting or implementing the interface.
I can't tell you more... if you still can't solve it, maybe you can post your code...
The DLL is specified in the exe assemnbly as a reference.
Note that I pruposly didn't new it from the reference since I want to specify the implementation to be loaded at runtime through a path string.
I hope this clarifies the situation and somebody can tell me what's wrong. Basically I want to have an interface in my exe that I can use to communicate to a dll library of implementations of this interface, but I wan't to be able to specify the dll at runtime. Typical add-in design.
I didn't try your example, and I might be wrong ... but I have some comments on your code...
First of all I don't see any reason why you would need to inherit from object... still there is no problem if you do so (but it is something that is automatically happening - every class inherits).
The second comment is that when you are implementing the interface you write like this:
void ILayoutComponent.Calculate()
It seemed strange to me... I am used with java and c++, but it works, still you can't declare it for example
public virtual void ILayoutComponent.Calculate() and I think you might need to do so if you want to inherit even more from Component1.
You can simply write:
public virtual void Calculate() //it understands that it is the method that you want to implement.
And the last comment which might help, I wrote about it in my previous post too, is that you have to use the full name when you say that you want to implement the interface:
public class Component1: ComponentLibrary1.ILayoutComponent
I didn't check this, but for me it seems common sense to be like this, and I believe that the error should be when you compile the class because the compiler doesn't know who is the interface.
Also in the exe you say:
ILayoutComponent lc = (ComponentLibrary1.ILayoutComponent)obj;
You should say:
ComponentLibrary1.ILayoutComponent lc = (ComponentLibrary1.ILayoutComponent)obj;
Hope it helps, and good luck again
Robi
P.s: If still doesn't work, post also the way you compile...
Originally posted by trobert
The second comment is that when you are implementing the interface you write like this:
void ILayoutComponent.Calculate()
It seemed strange to me... I am used with java and c++, but it works, still you can't declare it for example
public virtual void ILayoutComponent.Calculate() and I think you might need to do so if you want to inherit even more from Component1.
You can simply write:
public virtual void Calculate() //it understands that it is the method that you want to implement.
This is called explicit interface implementation and is used to disambiguate when you are implementing multiple interfaces that have methods with the same name. But it is not necessary in this case.
let's say I have a base class which implements 2 interfaces that have one common method. So I use this way. And let's say I also want to inherit from this base class. And let's say I want to have that method public and overridable. What do I do?
Because in the declaration of the interface I can't put the public or virtual keyword in front of the method (I really don't know why), and if a method is not virtual you can't inherit it.
I didn't look into this a lot, because I didn't need this, yet... maybe you know more... please let me know.
I'm posting this as an attempt to keep this thread on topic. I am not completly stuck to the specifics of the implementation, and that is not as important ot me as the goal that I'm trying to achieve.
Namely, I want to be able to have an interface ILayoutComponent that has a number of pures in it. I want to make many Component classes that implement this interface that can reside across multiple DLLs, and also I want to be able to specify a DLL to be loaded at runtime that would contain a class that implements this interface. An example would be a drawing program. You have a DLL with a Circle and Line classes implementing ILayoutComponent. Then you should be able to speicify at runtime, hey here is a new DLL that has a Triangle and Square components implementing the same interface and you shouldn't have to recompile the main exe.
My implementation was an attempt to do that. The explicit interface implementations are not a problem i think. Also, in a lot of places you'll see just ILayoutComponent instead of the full namespace.interface reference, since I have the using directive on the top of the file which I haven't included in the post (sorry). As far as the namespaces go, I think that if it compiles, then it shouldn't be a problem.
The problem is that I have a class that legally implements an interface. This interface is legally referenced in another module. The object is legally created, and the constructor shows a message box "Hello" when the objectis created confirming that the desired class has been instantiated. But when I try to cast the object to the exact interface that it implements (trough a reference) then it complains. To me this is mystical and I have no clue what I'm doing wrong.
Again, thanks for your posts guys, but unfortunately the problem still exsits and I am completely stuck.
Oh one more thing, VERY WEIRD!!!
If I use the the new <reference class> approach:
Component1 comp = new Component1;
and then:
ILayoutComponent lc = (ILayoutComponent)comp;
IT WORKS!!!
But if I use the assembly to create the object then it doesn't work
And, also the derivation from object was just another attempt to solve the problem by stabbing it in the dark.
Please post also what you didn't post earlier... the way you are referencing the dll.
About the "compiling ok" => "no problem" I don't agree... because it would compile very well if it can see the interface code, but it would generate another assembly in the exe... It's just an ideea... it will be clearer after you post more from the code.
Here is a zip with the whole solution with two assemblies, one os the class library the other one ia sample exe that is supposed to instantiate the Component1 class form the class library and cast it to the ILayoutComponent.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.