RawRpcParameter.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. namespace JsonRPC4.Router
  7. {
  8. public class RawRpcParameter : IRpcParameter
  9. {
  10. public RpcParameterType Type
  11. {
  12. get;
  13. }
  14. public object Value
  15. {
  16. get;
  17. }
  18. public RawRpcParameter(RpcParameterType type, object value)
  19. {
  20. Type = type;
  21. Value = value;
  22. }
  23. public bool TryGetValue(Type type, out object value)
  24. {
  25. if (Type == RpcParameterType.Null)
  26. {
  27. value = null;
  28. return false;
  29. }
  30. if (Value == null)
  31. {
  32. value = null;
  33. return false;
  34. }
  35. Type type2 = Value.GetType();
  36. if (type2 == type)
  37. {
  38. value = Value;
  39. return true;
  40. }
  41. TypeConverter converter = TypeDescriptor.GetConverter(type);
  42. if (converter != null && converter.CanConvertFrom(type2))
  43. {
  44. value = converter.ConvertFrom(Value);
  45. return true;
  46. }
  47. if (TypeDescriptor.GetConverter(type2) != null && converter.CanConvertTo(type2))
  48. {
  49. value = converter.ConvertTo(Value, type);
  50. return true;
  51. }
  52. value = null;
  53. return false;
  54. }
  55. }
  56. }