Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Please help on namespaces

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(  )
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

I guess I am asking how to organize seperately complied Assemblies under one namespace and avoid name collisions.
Avatar of nettnerd
nettnerd

     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);
Any other option beside system.reflection???  Looks like a big mess.
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?
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
%>

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
%>
ASKER CERTIFIED SOLUTION
Avatar of nettnerd
nettnerd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, netnerd!

Tom
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
%>