Another way to workaround "The type 'SelectedSerialNumbersRow' exists in both 'ERP.Contracts.BO.InvTransfer' and ERP.Contracts.BO.IssueReturn"

I had a need to call both InvTransfer and IssueReturn to move serialized parts in the same function library. However if you reference both services you will get an error like below when you try to create the SelectedSerialNumbersRow.

“The type ‘SelectedSerialNumbersRow’ exists in both ‘ERP.Contracts.BO.InvTransfer’ and ERP.Contracts.BO.IssueReturn”

This is because there is the same type in both dlls and the compiler doesn’t know which one you want. There are a few posts with the suggestion to split your function library up to avoid this issue. I think I have found another option thanks to this post by Kevin. Call store procedure in Kinetic Function not working - #8 by klincecum

While pondering splitting up my function library I thought If only there was some way I can tell the compiler which class to get the SelectedSerialNumberRows from. Then It hit me I had to use this trick to get the compiler to find the Microsoft.Data.SqlClient so why not use the same trick here…

Here is the Code.

First add the service references to the function library
image

Add a using for System.Reflection
image

In the code for the invTransfer function use reflection to get the correct class. Do similar for the IssueReturn function.

   if (!string.IsNullOrEmpty(SerialNum))
    {
    
      // Setting up reflection to get the SelectedSerialNumbersRow class as it exists in 2 services referenced by this function library
      Assembly Erp_Contracts_BO_InvTransfer = Assembly.Load("Erp.Contracts.BO.InvTransfer");
      Type typeSelectedSerialNumbersRow = Erp_Contracts_BO_InvTransfer.GetType("Erp.Tablesets.SelectedSerialNumbersRow");

      transfer.RowMod = "U";
      
      BO.GetSelectSerialNumbersParams(ref ds);
      
     // use reflection to instantiate the correct class
      dynamic snRow = Activator.CreateInstance(typeSelectedSerialNumbersRow); 
      snRow.Company = Company;
      snRow.PartNum = PartNum;
      snRow.SerialNumber = SerialNum;
      snRow.SNBaseNumber = SerialNum;
      snRow.RowMod = "A";

      ds.SelectedSerialNumbers.Add(snRow);
      
      transfer.RowMod = "U";
    
    }

Happy days you can reference both IssueReturn and InvTransfer in the same function library. From what I understand reflection is slow so if you need performance this may not be the best option.

putting it out there in case someone finds it useful.

Cheers
Brett

2 Likes

Cool. Nice job.

It very much depends on what you’re doing. For this, negligible.