Sharing classes across projects
Hi,
Here's my problem: I create one project that uses two classes and another project in another solution that uses the same two classes (as links from the *one* version of the source in the other solution). Now, I am trying to set variables via properties in an interface that is referenced from a DLL from both solutions. But when I actually do:
ClassThatImplementsInterface.MyProperty = MyObject
I get an "unable to cast" exception. I believe this is because each solution (and the interface) does not use exactly the same version of my class. they classes in each solution and the interface have the same source code, but are compiled into different assemblies.
This is probably a simple problem, but please tell me the solution...
Thanks,
Alex
[771 byte] By [
AlexReg] at [2008-2-7]
As you have noticed, sharing source code in this way will give you two distinct and unrelated types. The identity of the type is *not* based on the source code that was used to compile it - the type's identity includes (among other things) the containing assembly.
What you want to do is to either:
- put the shared type in it's own assembly, make the class public and reference this assembly from both projects in both of your solutions
or
- make the class public in one of your existing projects and reference the generated assembly from your other project in the other solution.
Best regards,
Johan Stenberg
Hi Johan,
Thanks for your reply - I think I will take your first suggestion, as I only need 3 classes (in 2 files), and referencing the whole project would be pointless.
Alex