Solved

Please help on namespaces

Posted on 2003-11-21
9
420 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:knowlton
  • 6
  • 3
9 Comments
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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
Comment Utility
     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:knowlton
Comment Utility
Any other option beside system.reflection???  Looks like a big mess.
0
 
LVL 1

Expert Comment

by:nettnerd
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 5

Author Comment

by:knowlton
Comment Utility
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:knowlton
Comment Utility
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
Comment Utility
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:knowlton
Comment Utility
Thank you, netnerd!

Tom
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now