Casting a generic Object in C# to a custom Object

I have a situation where I need to declare a generic object globally in a Public class. Then later in a Switch statement I instantiate an instance of a custom DLL based on a value.  There are about 5 DLLs, all with the same public routines. The routines are
INSTANTIATE
CONNECT
NAVIGATE
SEND
CONFIRM
DISCONNECT

The code within these routine are all different (based on Protocols) but the Routine's names and return values are all the same.

Once the switch statement finds a match I try to cast my Global Generic Object to that of the Custom DLL object type.  It is not working.  Can anyone help me out?

Public class MyClass
{
   Private Object TransmissionObject;
   Private string Transit_Type;

   Public string TransitType
   {
       Get{return Transit_Type;}
       Set{Transit_Type = value;}
    }

    public void MyRoutine()
    {
      Switch(Transit_Type)
      {
            Case “FTP”:
            TransFTP.Session tObject = new TransFTP.Session();
                                TransmissionObject =  (TransFTP.Session )tObject;
            Break;
            Case “HTTP”:
                                TransHTTP.Session tObject = new TransHTTP.Session();
                                TransmissionObject =  (TransHTTP.Session )tObject;
            Break;
            Case “NTFS”:
                                TransNTFS.Session tObject = new TransNTFS.Session();
                                TransmissionObject =  (TransNTFS.Session )tObject;
            Break;
      }      
       }
}
LVL 3
mandalorian4Asked:
Who is Participating?
 
dharmesh_amityConnect With a Mentor Commented:
No, I am not suggesting that.

Lets assume that you have interface IMyInterface which has declarations of the methods whose implementation will be the classes you provide.

Lets say one of that class is TransHTTP which implements all the methods of the interface, which is in the namespace TransHTTPNamespace.


Now while constructing the object you will have

IMyInterface tObject = new TransHTTPNamespace.TransHTTP();

Lets say your TransHTTP class is in some dll called MyHTTPDLL. You add the refence of this dll from by doing a add reference into your project. So now when you instantiate TransHTTP class you know exactly which class's object you are instantiating.

After this you can use tObject and at runtime the proper method implementation will be called as the compiler will know what type of object is in the reference tObject.

Hope this helps


0
 
dharmesh_amityCommented:
You have a classic use of interface. Create an interface and have all the classes implement that interface.
0
 
mandalorian4Author Commented:
dharmesh_amity,

I have created an Interface object.  However, if the different DLLs implement the same object how will the interface DLL know which DLL to use?  Are you suggesting That the DLLs use different Routine names and have the interface implement all the routines?
0
 
mandalorian4Author Commented:
dharmesh_amity,

That was exactly what I needed. I tried that in the past but I was instantiating the Object through the interface incorrectly:

TransObject = (ITransSrvProcess) Activator.GetObject(typeof(ITransSrvProcess),null,null);

Thank you very much for the tip.


Mandalorian
0
 
dharmesh_amityCommented:
I am happy that I was helpful. Thanks

Dharmesh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.