DynamicObjectWrapper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Dynamic;
  6. using System.Reflection;
  7. using System.Xml.Linq;
  8. #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
  9. namespace EasyDevCore.Common.Wrapper
  10. {
  11. /// <summary>
  12. /// Call static member
  13. /// </summary>
  14. public class DynamicObjectWrapper : DynamicObject
  15. {
  16. private Type _type;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="DynamicObjectWrapper"/> class.
  19. /// </summary>
  20. /// <param name="type">The type.</param>
  21. public DynamicObjectWrapper(Type type) { _type = type; }
  22. // Handle static properties
  23. /// <summary>
  24. /// 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.
  25. /// </summary>
  26. /// <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>
  27. /// <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>
  28. /// <returns>
  29. /// 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.)
  30. /// </returns>
  31. public override bool TryGetMember(GetMemberBinder binder, out object result)
  32. {
  33. PropertyInfo prop = _type.GetProperty(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);
  34. if (prop == null)
  35. {
  36. result = null;
  37. return false;
  38. }
  39. result = prop.GetValue(null, null);
  40. return true;
  41. }
  42. /// <summary>
  43. /// 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.
  44. /// </summary>
  45. /// <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>
  46. /// <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>
  47. /// <param name="result">The result of the member invocation.</param>
  48. /// <returns>
  49. /// 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.)
  50. /// </returns>
  51. public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
  52. {
  53. MethodInfo method = _type.GetMethod(binder.Name, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public);
  54. if (method == null)
  55. {
  56. result = null;
  57. return false;
  58. }
  59. result = method.Invoke(null, args);
  60. return true;
  61. }
  62. }
  63. }