Columns.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //------------------------------------------------------------------------------
  2. // <copyright file="Columns.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using System;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Globalization; //for CultureInfo
  9. using System.Runtime.InteropServices;
  10. namespace Experimental.System.Messaging.Interop
  11. {
  12. internal class Columns
  13. {
  14. private int maxCount;
  15. private MQCOLUMNSET columnSet = new MQCOLUMNSET();
  16. public Columns(int maxCount)
  17. {
  18. this.maxCount = maxCount;
  19. this.columnSet.columnIdentifiers = Marshal.AllocHGlobal(maxCount * 4);
  20. this.columnSet.columnCount = 0;
  21. }
  22. public virtual void AddColumnId(int columnId)
  23. {
  24. lock (this)
  25. {
  26. if (this.columnSet.columnCount >= this.maxCount)
  27. throw new InvalidOperationException(Res.GetString(Res.TooManyColumns, this.maxCount.ToString(CultureInfo.CurrentCulture)));
  28. ++this.columnSet.columnCount;
  29. this.columnSet.SetId(columnId, this.columnSet.columnCount - 1);
  30. }
  31. }
  32. public virtual MQCOLUMNSET GetColumnsRef()
  33. {
  34. return this.columnSet;
  35. }
  36. [StructLayout(LayoutKind.Sequential)]
  37. public class MQCOLUMNSET
  38. {
  39. public int columnCount;
  40. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  41. public IntPtr columnIdentifiers;
  42. ~MQCOLUMNSET()
  43. {
  44. if (this.columnIdentifiers != (IntPtr)0)
  45. {
  46. Marshal.FreeHGlobal(this.columnIdentifiers);
  47. this.columnIdentifiers = (IntPtr)0;
  48. }
  49. }
  50. public virtual void SetId(int columnId, int index)
  51. {
  52. Marshal.WriteInt32((IntPtr)((long)this.columnIdentifiers + (index * 4)), columnId);
  53. }
  54. }
  55. }
  56. }