DataRecordDynamic.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Data.Common;
  2. using System.Dynamic;
  3. using System.Reflection;
  4. namespace EasyDevCore.Database
  5. {
  6. /// <summary>
  7. /// Call static member
  8. /// </summary>
  9. public class DataRecordDynamic : DynamicObject
  10. {
  11. private DbDataReader _Reader;
  12. private Dictionary<string, int> _Fields = null;
  13. private Dictionary<string, int> Fields
  14. {
  15. get
  16. {
  17. if (_Fields == null)
  18. {
  19. _Fields = new(StringComparer.InvariantCultureIgnoreCase);
  20. for (int i = 0; i < _Reader.FieldCount; i++)
  21. {
  22. _Fields[_Reader.GetName(i)] = i;
  23. }
  24. }
  25. return _Fields;
  26. }
  27. }
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="DataRecordDynamic"/> class.
  30. /// </summary>
  31. /// <param name="reader">The reader.</param>
  32. public DataRecordDynamic(DbDataReader reader)
  33. {
  34. _Reader = reader;
  35. }
  36. /// <summary>
  37. /// Returns the enumeration of all dynamic member names.
  38. /// </summary>
  39. /// <returns>
  40. /// A sequence that contains dynamic member names.
  41. /// </returns>
  42. public override IEnumerable<string> GetDynamicMemberNames()
  43. {
  44. return Fields.Keys;
  45. }
  46. /// <summary>
  47. /// Gets the values.
  48. /// </summary>
  49. /// <returns></returns>
  50. public dynamic GetValues() => _Reader.GetRow();
  51. // Handle static properties
  52. /// <summary>
  53. /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
  54. /// </summary>
  55. /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
  56. /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result"/>.</param>
  57. /// <returns>
  58. /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
  59. /// </returns>
  60. public override bool TryGetMember(GetMemberBinder binder, out object result)
  61. {
  62. try
  63. {
  64. result = _Reader.GetValue(Fields[binder.Name]);
  65. return true;
  66. }
  67. catch
  68. {
  69. result = null;
  70. return false;
  71. }
  72. }
  73. /// <summary>
  74. /// Provides the implementation for operations that invoke a member. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as calling a method.
  75. /// </summary>
  76. /// <param name="binder">Provides information about the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleMethod". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
  77. /// <param name="args">The arguments that are passed to the object member during the invoke operation. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, <paramref name="args"/> is equal to 100.</param>
  78. /// <param name="result">The result of the member invocation.</param>
  79. /// <returns>
  80. /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)
  81. /// </returns>
  82. public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
  83. {
  84. MethodInfo method = _Reader.GetType().GetMethod(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);
  85. if (method == null)
  86. {
  87. result = null;
  88. return false;
  89. }
  90. result = method.Invoke(null, args);
  91. return true;
  92. }
  93. }
  94. }