/// <summary>A ParameterInfo extension method that converts a pi to a property.</summary> /// /// <param name="pi">The pi to act on.</param> /// /// <returns>pi as a MetadataPropertyType.</returns> public static MetadataPropertyType ToProperty(this ParameterInfo pi) { var propertyAttrs = pi.GetCustomAttributes(false); var property = new MetadataPropertyType { Name = pi.Name, Attributes = propertyAttrs.ToAttributes(), Type = pi.ParameterType.Name, }; var descAttr = propertyAttrs.OfType<DescriptionAttribute>().FirstOrDefault(); if (descAttr != null) { property.Description = descAttr.Description; } return property; }
/// <summary>A ParameterInfo extension method that converts a pi to a property.</summary> /// /// <param name="pi"> The pi to act on.</param> /// <param name="instance">The instance.</param> /// /// <returns>pi as a MetadataPropertyType.</returns> public static MetadataPropertyType ToProperty(this PropertyInfo pi, object instance = null) { var property = new MetadataPropertyType { Name = pi.Name, Attributes = pi.GetCustomAttributes(false).ToAttributes(), Type = pi.PropertyType.Name, DataMember = pi.GetDataMember().ToDataMember(), GenericArgs = pi.PropertyType.IsGenericType ? pi.PropertyType.GetGenericArguments().Select(x => x.Name).ToArray() : null, }; if (instance != null) { var value = pi.GetValue(instance, null); if (value != pi.PropertyType.GetDefaultValue()) { property.Value = value.ToJson(); } } return property; }