Skip to Content
avatar image
Former Member

Create wrapper for SAPbobsCOM usign reflection In C#

Hello sir ,

I have a method here on how to connect ,and get PopulateGLAccounts(). My problem is how can i get the classes of the SAPbobsCOM using reflection? This is my code so far :

public static void LoadDLL() { try { var DLL = Assembly.LoadFrom(Application.StartupPath + @"\\Interop.SAPbobsCOM.dll"); foreach (Type type in DLL.GetExportedTypes()) { //if (type.Name.Equals("CompanyClass") || type.Name.Equals("RecordsetClass") || type.Name.Equals("ChartOfAccountsClass")) if ( type.Name.Equals("RecordsetClass")) { //var c = Activator.CreateInstance(type); ConstructorInfo ci = null; try { ci = type.GetConstructor(Type.EmptyTypes); } catch (Exception ex) { throw new Exception(ex.Message); } switch (type.Name) { //case "CompanyClass": //SAPEntities.Company = ci.Invoke(null); //break; case "RecordsetClass": SAPEntities.RecordSet = ci.Invoke(null); break; //case "ChartOfAccountsClass": // //SAPEntities.ChartOfAccount = ci.Invoke(null); // break; } } if (type.Name.Equals("BoDataServerTypes")) goCompany.DbServerType = type; if (type.Name.Equals("BoSuppLangs")) goCompany.Language = type; } } catch (Exception ex) { throw ex; } }

Connecting to SAP :

private async Task<bool> Connect() { bool _success = false; if (!loSAPConnection.gCancelSAP) { try { try { SAPEntities.Company.GetType().GetMethod("Disconnect").Invoke(SAPEntities.Company, null); } catch { } Task<int> _connected = Task.Run(() => int.Parse(SAPEntities.Company.GetType().GetMethod("Connect").Invoke(SAPEntities.Company, null).ToString())); if (await _connected != 0) { SAPEntities.Company.GetType().GetMethod("GetLastError").Invoke(SAPEntities.Company, new object[] { ErrorObject.ErrorCode, ErrorObject.ErrorMessage }); throw new Exception(ErrorObject.ErrorMessage); } else { SAPMethod.PopulateGLAccounts(); _success = true; MessageBox.Show("Connection to SAP server was successful.", "SAP Connected", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { _success = false; MessageBox.Show("An error occured upon connecting to the SAP server:\n" + ex.Message, "SAP Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } this.Cursor = Cursors.Default; return _success; }

Populate GL Accounts :

public static void PopulateGLAccounts() { try { string _query = "Select AcctCode from OACT"; //SAPEntities.RecordSet.GetType().GetMethod("DoQuery").Invoke(SAPEntities.RecordSet, null).ToString(); SAPEntities.RecordSet.GetType().GetMethod("DoQuery").Invoke(SAPEntities.RecordSet, new object[] { _query }).ToString(); GlobalVariables.FCVariables.gCOAAttributes = new List<COAAttributes>(); while (bool.Parse(SAPEntities.RecordSet.GetType().GetProperty("EoF").GetValue(SAPEntities.RecordSet, null).ToString()).Equals(true)) { SAPEntities.ChartOfAccount.GetType().GetMethod("GetByKey").Invoke(SAPEntities.RecordSet, new object[] { SAPEntities.RecordSet.GetType().GetProperty("AcctCode").GetValue(SAPEntities.RecordSet) }).ToString(); COAAttributes _fieldsMapping = new COAAttributes(); _fieldsMapping.AccountCode = SAPEntities.ChartOfAccount.GetType().GetProperty("Code").GetValue(SAPEntities.RecordSet, null).ToString(); _fieldsMapping.AccoutName = SAPEntities.ChartOfAccount.GetType().GetProperty("Name").GetValue(SAPEntities.RecordSet, null).ToString(); GlobalVariables.FCVariables.gCOAAttributes.Add(_fieldsMapping); SAPEntities.RecordSet.GetType().GetMethod("MoveNext").Invoke(SAPEntities.RecordSet, new object[] { }); } } catch (Exception ex) { throw ex; } }

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

0 Answers