title: Pass a Delphi set to an external Delphi function from C#
body: <p>I'm trying to call an external Delphi function from C# which takes a Delphi set as a parameter:</p>
  
  <h2>Delphi code</h2>
  
  <pre><code>type
    tStatus = (sIn, sOut, sAbsent, sSick);
    tStatusSet = set of tStatus;
  
  function LoadEmployees(tStatusSet aStatusSet): tEmpList;
  </code></pre>
  
  <p>I need to marshall a C# array of enum values (that is elements from <code>tStatus</code>) into a format that Delphi will read as a <code>tStatusSet</code> type:</p>
  
  <h2>C# code</h2>
  
  <pre><code>tStatusSet lStatusSet = ConvertToDelphiSet(sIn, sOut);
  
  tEmpList lEmpList = LoadEmployees(aStatusSet);
  </code></pre>
  
  <p><code>ConvertToDelphiSet</code> should ideally be a generic solution, able to cope with any enum. We have it defined as:</p>
  
  <pre><code>int ConvertToDelphiSet&lt;T&gt;(params T[] aArgs) {
    int lResult = 0;
  
    foreach (T lItem in aArgs)
    {
      int lValue = lItem.ToInt32();
      lValue = (int)Math.Pow(2, lValue);
  
      lResult |= lValue;
    }
  </code></pre>
  
  <p>But this isn't returning the correct value (for example passing it all four <code>tStatus</code> values results in Delphi only seeing the third value in the set).</p>
  
  <p>Is there documentation on how Delphi internally represents a set? Is it a simple bit-field of all the values? Is there a more robust way of achieving this? Is this future proof or am I relying on undocumented internal features likely to change?</p>