JsonMemberInfo.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // Define JsonKit_NO_DYNAMIC to disable Expando support
  15. // Define JsonKit_NO_EMIT to disable Reflection.Emit
  16. // Define JsonKit_NO_DATACONTRACT to disable support for [DataContract]/[DataMember]
  17. using System;
  18. using System.Reflection;
  19. namespace Topten.JsonKit
  20. {
  21. // Information about a field or property found through reflection
  22. class JsonMemberInfo
  23. {
  24. // The Json key for this member
  25. public string JsonKey;
  26. // True if should keep existing instance (reference types only)
  27. public bool KeepInstance;
  28. // True if deprecated
  29. public bool Deprecated;
  30. // Reflected member info
  31. MemberInfo _mi;
  32. public MemberInfo Member
  33. {
  34. get { return _mi; }
  35. set
  36. {
  37. // Store it
  38. _mi = value;
  39. // Also create getters and setters
  40. if (_mi is PropertyInfo)
  41. {
  42. GetValue = (obj) => ((PropertyInfo)_mi).GetValue(obj, null);
  43. SetValue = (obj, val) => ((PropertyInfo)_mi).SetValue(obj, val, null);
  44. }
  45. else
  46. {
  47. GetValue = ((FieldInfo)_mi).GetValue;
  48. SetValue = ((FieldInfo)_mi).SetValue;
  49. }
  50. }
  51. }
  52. // Member type
  53. public Type MemberType
  54. {
  55. get
  56. {
  57. if (Member is PropertyInfo)
  58. {
  59. return ((PropertyInfo)Member).PropertyType;
  60. }
  61. else
  62. {
  63. return ((FieldInfo)Member).FieldType;
  64. }
  65. }
  66. }
  67. // Get/set helpers
  68. public Action<object, object> SetValue;
  69. public Func<object, object> GetValue;
  70. }
  71. }