Reputation: 4768
I have a solution called Tools that has three projects in it; Tools, PerecentageBar, MessageBox. Each of these projects is part of the Tools assembly.[In the properties page of each I have set the AssemblyName to MyCompany.Tools.Controls].
I have in Tools Project:
namespace MyCompany.Tools.Controls
{
public class MyPictureBox : PictureBox
{}
}
And in the MessageBox project.
namespace MyCompany.Tools.Controls
{
public class MyMessageBox : Form
{}
}
And in the PercentageBar project
namespace MyCompany.Tools.Controls
{
public class PercentageBar: UserControl
{}
}
This solution is built and a MyCompany.Tools.Controls.dll created.
In another solution, I have a project called MapperTool.
namespace MapperTool
{
public class MapperToolFacade : IDisposable, IDiagService
{}
}
In this project I add a reference to the dll above AND add a using statement thus:
using MyCompany.Tools.Controls;
All good so far, however. In this project I can only 'see' and use MyPictureBox with the above using statement. The other types MyMessageBox and PercentageBar are not available.
Why is this?
EDIT: Investigation shows that the other types are there and under the namespace MyCompany.Tools.Controls. However, looking in class view they are not listed in the MyCompany.Tools.Controls assembly. Instead they are organised thus:
MyCompany.Tools.Controls (assembly)
MyCompany.Tools.Controls (namespace)
MyPictureBox (type)
PercentageBar
MyCompany.Tools.Controls
PercentageBar
MyMessageBox
MyCompany.Tools.Controls
MyMessageBox
So I guess the next question is, if I have specified the assembly name as MyCompany.Tools.Controls in each project why have the second two projects been built as an assembly with a different name? [ The dll in the output directory does however have the naem MyCompany.Tools.Controls.dll]
Also, I want to creat all my controls in the MyCompany.Tools.Controls namespace and build them into a single MyCompany.Tools.Controls assembly such that I can add just this reference to any other project that needs to use them.
It would appear from what is happening above that each seperate tools project in my MyCompany.Tools.Controls solution would have to be built into a unique assembly and that each would have to be referenced individually...?
The problem was my understanding of projects and assemblies. It would appear that it is not possible to have the 3 projects build into one assembly. Each must build it's own assembly - I will then have to use a merge tool to combine them into one assembly.
Upvotes: 1
Views: 10887
Reputation: 48583
Edit - based on your follow-up comments, I'll hazard a guess about what's wrong:
Assembly1 defines the class MyMessageBox
in the namespace MyCompany.Tools.Controls
.
Assembly2 references Assembly1 and defines at least one class in the namespace Controls
. That is, the class declaration looks like this:
namespace Controls {
public class MyControl {
// ...
}
}
Assembly3 references the compiled Assembly2.
If this is the case, Assembly3 can see the namespace Controls
but not MyCompany
or MyCompany.Tools
because neither of those namespaces are used by a class in the referenced assembly. The project's default namespace it is only used to create the default for new class files and to generate namespaces for embedded resources - it does not actually exist in the assembly unless you define a type that uses it.
If I've guessed right, you should add a type to Assembly2 within the MyCompany.Tools.Controls
namespace. This will make the top-level namespaces visible to Assembly3.
If Assembly2 contains the other portion of the partial class MyMessageBox
, it will need to be declared within the same namespace as the other portion.
That looks like it should work (and does in my tests). In the meantime, try changing it to:
namespace MyCompany.Tools.Controls
{
public partial class MyMessageBox : Form {}
}
It shouldn't make any difference, but it is a more idiomatic way to define nested namespaces in C#.
Upvotes: 1
Reputation: 46366
To help clear up the issue you can use the Class Explorer in Visual Studio to inspect the namespace the class is in. I would first verify that MyMessageBox is in fact in the namespace you believe it to be.
Then, if it's as expected, you should focus on your second project. Is your other class within the MyCompany.Tools namespace? Does it, by chance, declare classes name MyCompany or Tools (visual studio can trip up if a local class conflicts with an imported namespace).
Upvotes: 0
Reputation: 12401
Use the using
directive.
So in your case, it would be: using MyCompany.Tools
-- this goes with the other using statements that were auto-generated in the source file.
Edit: try using Controls = MyCompany.Tools.Controls
. I don't generally reference namespaces in code using an alias unless I absolutely have to, but I think this should work.
Upvotes: 4
Reputation: 10389
Namespaces are not exported over a reference boundary. Only types (and functions). In order to 'see' the MyCompany namespace, you would need to add a type (a class) directly underneath that namespace.
Upvotes: 2