VersioningHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // System.Runtime.Versioning.VersioningHelper class
  3. //
  4. // Authors
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. namespace System.Runtime.Versioning {
  30. public static class VersioningHelper {
  31. static private int GetProcessId ()
  32. {
  33. // TODO - System.Diagnostics.Process class is located in System.dll
  34. return 0;
  35. }
  36. static string SafeName (string name, bool process, bool appdomain)
  37. {
  38. if (process && appdomain) {
  39. return String.Concat (name, "_", GetProcessId ().ToString (),
  40. "_", AppDomain.CurrentDomain.Id.ToString ());
  41. } else if (process) {
  42. return String.Concat (name, "_", GetProcessId ().ToString ());
  43. } else if (appdomain) {
  44. return String.Concat (name, "_", AppDomain.CurrentDomain.Id.ToString ());
  45. }
  46. // nothing, return original string
  47. return name;
  48. }
  49. static private string ConvertFromMachine (string name, ResourceScope to, Type type)
  50. {
  51. switch (to) {
  52. case ResourceScope.Machine:
  53. return SafeName (name, false, false);
  54. case ResourceScope.Process:
  55. return SafeName (name, true, false);
  56. case ResourceScope.AppDomain:
  57. return SafeName (name, true, true);
  58. default:
  59. throw new ArgumentException ("to");
  60. }
  61. }
  62. static private string ConvertFromProcess (string name, ResourceScope to, Type type)
  63. {
  64. if ((to < ResourceScope.Process) || (to >= ResourceScope.Private))
  65. throw new ArgumentException ("to");
  66. bool ad = ((to & ResourceScope.AppDomain) == ResourceScope.AppDomain);
  67. return SafeName (name, false, ad);
  68. }
  69. static private string ConvertFromAppDomain (string name, ResourceScope to, Type type)
  70. {
  71. if ((to < ResourceScope.AppDomain) || (to >= ResourceScope.Private))
  72. throw new ArgumentException ("to");
  73. return SafeName (name, false, false);
  74. }
  75. [MonoTODO ("process id is always 0")]
  76. static public string MakeVersionSafeName (string name, ResourceScope from, ResourceScope to)
  77. {
  78. return MakeVersionSafeName (name, from, to, null);
  79. }
  80. [MonoTODO ("type?")]
  81. static public string MakeVersionSafeName (string name, ResourceScope from, ResourceScope to, Type type)
  82. {
  83. if ((from & ResourceScope.Private) != 0) {
  84. to &= ~(ResourceScope.Private | ResourceScope.Assembly);
  85. } else if ((from & ResourceScope.Assembly) != 0) {
  86. to &= ~ResourceScope.Assembly;
  87. }
  88. string result = (name == null) ? String.Empty : name;
  89. switch (from) {
  90. case ResourceScope.Machine:
  91. case ResourceScope.Machine | ResourceScope.Private:
  92. case ResourceScope.Machine | ResourceScope.Assembly:
  93. return ConvertFromMachine (result, to, type);
  94. case ResourceScope.Process:
  95. case ResourceScope.Process | ResourceScope.Private:
  96. case ResourceScope.Process | ResourceScope.Assembly:
  97. return ConvertFromProcess (result, to, type);
  98. case ResourceScope.AppDomain:
  99. case ResourceScope.AppDomain | ResourceScope.Private:
  100. case ResourceScope.AppDomain | ResourceScope.Assembly:
  101. return ConvertFromAppDomain (result, to, type);
  102. default:
  103. throw new ArgumentException ("from");
  104. }
  105. }
  106. }
  107. }
  108. #endif