Solved

Please help on namespaces

Posted on 2003-11-21
9
426 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

615 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