mandalorian4
asked on
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;
}
}
}
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;
}
}
}
You have a classic use of interface. Create an interface and have all the classes implement that interface.
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 (ITransSrv Process),n ull,null);
Thank you very much for the tip.
Mandalorian
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
Thank you very much for the tip.
Mandalorian
I am happy that I was helpful. Thanks
Dharmesh
Dharmesh