Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Please help on namespaces

Posted on 2003-11-21
9
Medium Priority
?
429 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 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