Solved

Please help on namespaces

Posted on 2003-11-21
9
425 Views
Last Modified: 2010-04-16
I want to create a bunch of class libraries that are ALL under the same namespace.  Each Class Library will be under it's own Solution / Project but will have the same namespace.

Is this possible?

Here is an example of what I mean...


~~~~~~~~~~~~~~~~~~~~~
DLL "A"
~~~~~~~~~~~~~~~~~~~~~


using System;

namespace GlobalNamespace{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      public class ClassTest
      {
            public ClassTest()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            public string Speak()
            {
                  return "Bark Bark Ruff Ruff";
            }
      }
}


~~~~~~~~~~~~~~~~~~~~~
DLL "B"
~~~~~~~~~~~~~~~~~~~~~


using System;

namespace GlobalNamespace{

      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      public class ClassTest
      {
            public ClassTest()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            public string Speak()
            {
                  return "Bark Bark Ruff Ruff";
            }
      }
}





How do I distinguish between DLL A  Speak(  )  and   DLL B Speak(  )
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 9800149
I guess I am asking how to organize seperately complied Assemblies under one namespace and avoid name collisions.
0
 
LVL 1

Expert Comment

by:nettnerd
ID: 9800419
     Check into the System.Reflection Namespace.  I havent tested this there may be more too it.

                System.Reflection.Assembly myAssemblyA = System.Reflection.Assembly.LoadFile("A.DLL");
      System.Type myTypeA = myAssemblyA.CreateInstance("ClassTest");
      System.Reflection.MethodInfo myMethodA = myTypeA.GetMethod("Insert");
      myMethod.Invoke(myTypeA,myParamsA);

      System.Reflection.Assembly myAssemblyB = System.Reflection.Assembly.LoadFile("B.DLL");
      System.Type myTypeB = myAssemblyB.CreateInstance("ClassTest");
      System.Reflection.MethodInfo myMethodB = myTypeA.GetMethod("Insert");
      myMethodB.Invoke(myTypeB,myParamsB);
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 9800449
Any other option beside system.reflection???  Looks like a big mess.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 1

Expert Comment

by:nettnerd
ID: 9800550
The could create wrapper functions to the Reflection Code to clean it up:

private void InvokeMethodA
{
     System.Reflection.Assembly myAssemblyA = System.Reflection.Assembly.LoadFile("A.DLL");
     System.Type myTypeA = myAssemblyA.CreateInstance("ClassTest");
     System.Reflection.MethodInfo myMethodA = myTypeA.GetMethod("Insert");
     myMethod.Invoke(myTypeA,myParamsA);

}
private void InvokeMethodB
{
     System.Reflection.Assembly myAssemblyB = System.Reflection.Assembly.LoadFile("B.DLL");
     System.Type myTypeB = myAssemblyB.CreateInstance("ClassTest");
     System.Reflection.MethodInfo myMethodB = myTypeA.GetMethod("Insert");
     myMethodB.Invoke(myTypeB,myParamsB);
}

Youll have to look deeper into it to get the return values and such, Also, System.Reflection is AutoGeneration Friendly, you can Iterate Through Class Members and Auto Generate Wrapper Classes.

Somehow your going to have to specify wich Class you want to create an instance of, or wich class you want to Invoke a static method or property on.  I cant think of a better way then the way the comiler does it behind the scenes.

If you dont mind, why do you need to have Classes With the exact same name and Namespace?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 9800612
netnerd:

You asked:

>>>If you dont mind, why do you need to have Classes With the exact same name and Namespace?<<<

Answer:  I dunno.  :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I think in the meantime I have found an answer I can live with:


~~~~~~~~~~~~~~~~~~~~~
DLL "A"
~~~~~~~~~~~~~~~~~~~~~


namespace BuyersFund
{
      namespace SimpleTest
      {
            /// <summary>
            /// Summary description for Class1.
            /// </summary>
            public class ClassTest
            {
                  public ClassTest()
                  {
                        //
                        // TODO: Add constructor logic here
                        //
                  }

                  public string Speak()
                  {
                        return "Buyers Fund Simple Test";
                  }
            }
      }
}


~~~~~~~~~~~~~~~~~~~~~
DLL "B"
~~~~~~~~~~~~~~~~~~~~~


namespace BuyersFund
{
      namespace AnotherSimpleTest
      {
            /// <summary>
            /// Summary description for Class1.
            /// </summary>
            public class ClassTest
            {
                  public ClassTest()
                  {
                        //
                        // TODO: Add constructor logic here
                        //
                  }

                  public string Speak()
                  {
                        return "Buyers Fund Simple Test";
                  }
            }
      }
}


Now I can do things like


<%
Set DBObj = Server.CreateObject("BuyersFund.SimpleTest.ClassTest")
Set DBObjOther = Server.CreateObject("BuyersFund.SimpleTest.ClassTest")
Response.Write("From CSharp COM Object:  " & DBObj.Speak()) & "\n"
Response.Write("From CSharp COM Object:  " & DBObjOther.Speak()) & "\n"
Set DBObj = Nothing
%>

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 9800635
Errr....maybe should have been:

<%
Set DBObj = Server.CreateObject("BuyersFund.SimpleTest","ClassTest")
Set DBObjOther = Server.CreateObject("BuyersFund.AnotherSimpleTest","ClassTest")
Response.Write("From CSharp COM Object:  " & DBObj.Speak() & "\n")
Response.Write("From CSharp COM Object:  " & DBObjOther.Speak() & "\n")
Set DBObj = Nothing
%>
0
 
LVL 1

Accepted Solution

by:
nettnerd earned 500 total points
ID: 9800645
Yes that would work of course you would have to change this part :)
Set DBObj = Server.CreateObject("BuyersFund.SimpleTest.ClassTest")
Set DBObjOther = Server.CreateObject("BuyersFund.AnotherSimpleTest.ClassTest")

you can also avoid nesting namespaces by using
namespace BuyersFund.SimpleTest
{
}
namespace BuyersFund.AnotherSimpleTest
{
}

Reflection is still the only way I see if you want the namespaces to be exactly the same.  Good Luck!
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 9800660
Thank you, netnerd!

Tom
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 9800911
Actually, it seems like THIS was actually correct:

<%
Set DBObj = Server.CreateObject("BuyersFund.SimpleTest.ClassTest")
Set DBObjOther = Server.CreateObject("BuyersFund.AnotherSimpleTest.ClassTest")
Response.Write("From CSharp COM Object:  " & DBObj.Speak()) & "\n"
Response.Write("From CSharp COM Object:  " & DBObjOther.Speak()) & "\n"
Set DBObj = Nothing
%>
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# XML Get Values 4 54
Visual C# Windows Project - Clone Form 4 39
What is GIS method of Geometry data type? 6 36
Easiest intro into .NET CORE? 2 33
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question