PageRenderTime 96ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/NekoKun.Serialization.RubyMarshal/Objects/RubyObject.cs

https://bitbucket.org/nekokun/nekokun
C# | 123 lines | 107 code | 16 blank | 0 comment | 1 complexity | fc566f325c54ce2ba849a8b0c5aefab5 MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace NekoKun.Serialization.RubyMarshal
  5. {
  6. [System.Diagnostics.DebuggerTypeProxy(typeof(RubyObjectDebugView))]
  7. public class RubyObject
  8. {
  9. internal class RubyObjectDebugView
  10. {
  11. internal RubyObject obj;
  12. public RubyObjectDebugView(RubyObject obj)
  13. {
  14. this.obj = obj;
  15. }
  16. public RubySymbol ClassName
  17. {
  18. get { return obj.ClassName; }
  19. }
  20. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
  21. public KeyValuePair<RubySymbol, object>[] Keys
  22. {
  23. get
  24. {
  25. KeyValuePair<RubySymbol, object>[] keys = new KeyValuePair<RubySymbol, object>[obj.InstanceVariables.Count];
  26. int i = 0;
  27. foreach (KeyValuePair<RubySymbol, object> key in obj.InstanceVariables)
  28. {
  29. keys[i] = key;
  30. i++;
  31. }
  32. return keys;
  33. }
  34. }
  35. }
  36. private RubySymbol className;
  37. private Dictionary<RubySymbol, object> variables;
  38. private RubyObjectInstanceVariableProxy variableproxy;
  39. private List<RubyModule> extmodules;
  40. public override string ToString()
  41. {
  42. return ("#<" + this.ClassName + ">");
  43. }
  44. public virtual RubySymbol ClassName
  45. {
  46. get { return this.className ?? (this.className = RubySymbol.GetSymbol("Object")); }
  47. set { this.className = value; }
  48. }
  49. public RubyClass Class
  50. {
  51. get { return RubyClass.GetClass(this.ClassName); }
  52. }
  53. public Dictionary<RubySymbol, object> InstanceVariables
  54. {
  55. get { return variables ?? (variables = new Dictionary<RubySymbol, object>()); }
  56. }
  57. public RubyObjectInstanceVariableProxy InstanceVariable
  58. {
  59. get { return variableproxy ?? (variableproxy = new RubyObjectInstanceVariableProxy(this)); }
  60. }
  61. public List<RubyModule> ExtendModules
  62. {
  63. get { return extmodules ?? (extmodules = new List<RubyModule>()); }
  64. }
  65. public virtual Encoding Encoding
  66. {
  67. get;
  68. set;
  69. }
  70. public class RubyObjectInstanceVariableProxy
  71. {
  72. RubyObject obj;
  73. internal RubyObjectInstanceVariableProxy(RubyObject obj)
  74. {
  75. this.obj = obj;
  76. }
  77. public object this[RubySymbol key]
  78. {
  79. get
  80. {
  81. return obj.InstanceVariables.ContainsKey(key) ?
  82. obj.InstanceVariables[key] is RubyNil ?
  83. null :
  84. obj.InstanceVariables[key] :
  85. null;
  86. }
  87. set
  88. {
  89. if (obj.InstanceVariables.ContainsKey(key))
  90. obj.InstanceVariables[key] = value;
  91. else
  92. obj.InstanceVariables.Add(key, value);
  93. }
  94. }
  95. public object this[string key]
  96. {
  97. get { return this[RubySymbol.GetSymbol(key)]; }
  98. set { this[RubySymbol.GetSymbol(key)] = value; }
  99. }
  100. public object this[RubyString key]
  101. {
  102. get { return this[RubySymbol.GetSymbol(key)]; }
  103. set { this[RubySymbol.GetSymbol(key)] = value; }
  104. }
  105. }
  106. }
  107. }