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

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()
            Case “FTP”:
            TransFTP.Session tObject = new TransFTP.Session();
                                TransmissionObject =  (TransFTP.Session )tObject;
            Case “HTTP”:
                                TransHTTP.Session tObject = new TransHTTP.Session();
                                TransmissionObject =  (TransHTTP.Session )tObject;
            Case “NTFS”:
                                TransNTFS.Session tObject = new TransNTFS.Session();
                                TransmissionObject =  (TransNTFS.Session )tObject;
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

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

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?
mandalorian4Author Commented:

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.

I am happy that I was helpful. Thanks

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.