ParameterInfo.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Runtime.Serialization;
  6. namespace System.Reflection
  7. {
  8. public class ParameterInfo : ICustomAttributeProvider, IObjectReference
  9. {
  10. protected ParameterInfo() { }
  11. public virtual ParameterAttributes Attributes => AttrsImpl;
  12. public virtual MemberInfo Member => MemberImpl;
  13. public virtual string Name => NameImpl;
  14. public virtual Type ParameterType => ClassImpl;
  15. public virtual int Position => PositionImpl;
  16. public bool IsIn => (Attributes & ParameterAttributes.In) != 0;
  17. public bool IsLcid => (Attributes & ParameterAttributes.Lcid) != 0;
  18. public bool IsOptional => (Attributes & ParameterAttributes.Optional) != 0;
  19. public bool IsOut => (Attributes & ParameterAttributes.Out) != 0;
  20. public bool IsRetval => (Attributes & ParameterAttributes.Retval) != 0;
  21. public virtual object DefaultValue { get { throw NotImplemented.ByDesign; } }
  22. public virtual object RawDefaultValue { get { throw NotImplemented.ByDesign; } }
  23. public virtual bool HasDefaultValue { get { throw NotImplemented.ByDesign; } }
  24. public virtual bool IsDefined(Type attributeType, bool inherit)
  25. {
  26. if (attributeType == null)
  27. throw new ArgumentNullException(nameof(attributeType));
  28. return false;
  29. }
  30. public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData();
  31. public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; }
  32. public virtual object[] GetCustomAttributes(bool inherit) => Array.Empty<object>();
  33. public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
  34. {
  35. if (attributeType == null)
  36. throw new ArgumentNullException(nameof(attributeType));
  37. return Array.Empty<object>();
  38. }
  39. public virtual Type[] GetOptionalCustomModifiers() => Array.Empty<Type>();
  40. public virtual Type[] GetRequiredCustomModifiers() => Array.Empty<Type>();
  41. public virtual int MetadataToken => MetadataToken_ParamDef;
  42. public object GetRealObject(StreamingContext context)
  43. {
  44. // Once all the serializable fields have come in we can set up the real
  45. // instance based on just two of them (MemberImpl and PositionImpl).
  46. if (MemberImpl == null)
  47. throw new SerializationException(SR.Serialization_InsufficientState);
  48. ParameterInfo[] args = null;
  49. switch (MemberImpl.MemberType)
  50. {
  51. case MemberTypes.Constructor:
  52. case MemberTypes.Method:
  53. if (PositionImpl == -1)
  54. {
  55. if (MemberImpl.MemberType == MemberTypes.Method)
  56. return ((MethodInfo)MemberImpl).ReturnParameter;
  57. else
  58. throw new SerializationException(SR.Serialization_BadParameterInfo);
  59. }
  60. else
  61. {
  62. args = ((MethodBase)MemberImpl).GetParametersNoCopy();
  63. if (args != null && PositionImpl < args.Length)
  64. return args[PositionImpl];
  65. else
  66. throw new SerializationException(SR.Serialization_BadParameterInfo);
  67. }
  68. case MemberTypes.Property:
  69. args = ((PropertyInfo)MemberImpl).GetIndexParameters();
  70. if (args != null && PositionImpl > -1 && PositionImpl < args.Length)
  71. return args[PositionImpl];
  72. else
  73. throw new SerializationException(SR.Serialization_BadParameterInfo);
  74. default:
  75. throw new SerializationException(SR.Serialization_NoParameterInfo);
  76. }
  77. }
  78. public override string ToString() => ParameterType.FormatTypeName() + " " + Name;
  79. protected ParameterAttributes AttrsImpl;
  80. protected Type ClassImpl;
  81. protected object DefaultValueImpl;
  82. protected MemberInfo MemberImpl;
  83. protected string NameImpl;
  84. protected int PositionImpl;
  85. private const int MetadataToken_ParamDef = 0x08000000;
  86. }
  87. }