JsonMemberInfo.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // JsonKit v0.5 - A simple but flexible Json library in a single .cs file.
  2. //
  3. // Copyright (C) 2014 Topten Software (contact@toptensoftware.com) All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this product
  6. // except in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the
  11. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. // either express or implied. See the License for the specific language governing permissions
  13. // and limitations under the License.
  14. using System;
  15. using System.Reflection;
  16. namespace Topten.JsonKit
  17. {
  18. // Information about a field or property found through reflection
  19. class JsonMemberInfo
  20. {
  21. // The Json key for this member
  22. public string JsonKey;
  23. // True if should keep existing instance (reference types only)
  24. public bool KeepInstance;
  25. // True if deprecated
  26. public bool Deprecated;
  27. // Reflected member info
  28. MemberInfo _mi;
  29. public MemberInfo Member
  30. {
  31. get { return _mi; }
  32. set
  33. {
  34. // Store it
  35. _mi = value;
  36. // Also create getters and setters
  37. if (_mi is PropertyInfo)
  38. {
  39. GetValue = (obj) => ((PropertyInfo)_mi).GetValue(obj, null);
  40. SetValue = (obj, val) => ((PropertyInfo)_mi).SetValue(obj, val, null);
  41. }
  42. else
  43. {
  44. GetValue = ((FieldInfo)_mi).GetValue;
  45. SetValue = ((FieldInfo)_mi).SetValue;
  46. }
  47. }
  48. }
  49. // Member type
  50. public Type MemberType
  51. {
  52. get
  53. {
  54. if (Member is PropertyInfo)
  55. {
  56. return ((PropertyInfo)Member).PropertyType;
  57. }
  58. else
  59. {
  60. return ((FieldInfo)Member).FieldType;
  61. }
  62. }
  63. }
  64. // Get/set helpers
  65. public Action<object, object> SetValue;
  66. public Func<object, object> GetValue;
  67. }
  68. }