/// <summary> /// Gets flags set on an enum property /// </summary> /// <param name="p">The propertyInfo</param> /// <param name="target">The instance of the object</param> /// <returns>A list of the set enum values</returns> public static IEnumerable <EnumValue> GetFlags(this IMetaProperty p, IMetaObject target) { if (p is null) { throw new System.ArgumentNullException(nameof(p)); } if (target is null) { throw new System.ArgumentNullException(nameof(target)); } if (!p.Type.HasAttribute <FlagsAttribute>()) { throw new ArgumentException($"Property type {p.Type.FullName} does not have flags attribute"); } long l = p.GetValue(target).Convert <long>(); foreach (EnumValue thisValue in p.Type.Values) { long thisVal = thisValue.Value.Convert <long>(); if (TestFlags(l, thisVal)) { yield return(thisValue); } } }
public static T MaxValue <T>(this IMetaProperty p) { if (p is null) { throw new System.ArgumentNullException(nameof(p)); } if (p.Attributes.AnyNotNull(a => a.Type.Name == typeof(RangeAttribute).Name)) { if (typeof(T).InvokeMember("MaxValue", BindingFlags.GetField, null, null, null, CultureInfo.CurrentCulture) is T val) { foreach (T checkVal in p.Attributes.Where(a => a.Type.Name == typeof(RangeAttribute).Name).Select(a => a[nameof(RangeAttribute.Maximum)].GetValue <T>())) { val = Min(val, checkVal); } return(val); } } else { if (typeof(T).InvokeMember("MaxValue", BindingFlags.GetField, null, null, null, CultureInfo.CurrentCulture) is T val) { return(val); } } throw new System.Exception("Somehow we failed to find a non-null max value for the requested type"); }
/// <summary> /// Constructs a new instance of the settings object /// </summary> /// <param name="type">An optional override type for the renderer</param> /// <param name="property">The IMetaProperty that references the object being resolved</param> /// <param name="fileProvider">A file provider used for checking for the existence of views</param> public DynamicRendererSettings(IMetaType type, IMetaProperty property, IFileProvider fileProvider) { this.Type = type ?? throw new ArgumentNullException(nameof(type)); this.TypeFullName = this.Type.FullName; this.Property = property; this.FileProvider = fileProvider; }
/// <summary> /// Retrieves a property from the selected type, recursively using "." as a delimiter /// </summary> /// <param name="target">The target to retrieve the property from</param> /// <param name="path">The path of the property to find</param> /// <returns>The object instance of the property being searched for</returns> public static IMetaProperty GetProperty(this IHasProperties target, string path) { if (target is null) { throw new System.ArgumentNullException(nameof(target)); } if (path is null) { throw new System.ArgumentNullException(nameof(path)); } IMetaProperty m = null; foreach (string chunk in path.Split('.')) { m = (m?.Type ?? target).Properties.FirstOrDefault(p => p.Name == chunk); if (m is null) { return(null); } } return(m); }
/// <summary> /// Checks if a property of a matching IMetaProperty exists /// </summary> /// <param name="o">The source</param> /// <param name="property">The IMetaProperty to match against</param> /// <returns>A bool indicating whether or not the property exists</returns> public static bool HasProperty(this IMetaObject o, IMetaProperty property) { if (o is null) { throw new System.ArgumentNullException(nameof(o)); } return(o.Properties.Any(p => p.Property.Name == property.Name)); }
/// <summary> /// Constructs a new instance of the settings object /// </summary> /// <param name="property">The IMetaProperty that references the object being resolved</param> /// <param name="fileProvider">A file provider used for checking for views</param> public DynamicRendererSettings(IMetaProperty property, IFileProvider fileProvider) { Contract.Requires(property != null); this.Type = property.Type; this.TypeFullName = this.Type.FullName; this.Property = property; this.FileProvider = fileProvider; }
/// <summary> /// Constructs a new instance of the settings object /// </summary> /// <param name="property">The IMetaProperty that references the object being resolved</param> /// <param name="fileProvider">A file provider used for checking for views</param> public DynamicRendererSettings(IMetaProperty property, IFileProvider fileProvider) { if (property is null) { throw new ArgumentNullException(nameof(property)); } this.Type = property.Type; this.TypeFullName = this.Type.FullName; this.Property = property ?? throw new ArgumentNullException(nameof(property)); this.FileProvider = fileProvider; }
/// <summary> /// Gets the casted value of an object based on its IMetaProperty /// </summary> /// <typeparam name="T">The type to cast the value as</typeparam> /// <param name="o">The source object</param> /// <param name="property">The IMetaProperty to get</param> /// <returns>The casted value of an object based on its IMetaProperty</returns> public static T GetValue <T>(this IMetaObject o, IMetaProperty property) { if (o is null) { throw new System.ArgumentNullException(nameof(o)); } if (property is null) { throw new System.ArgumentNullException(nameof(property)); } return(GetValue <T>(o.GetProperty(property.Name))); }
/// <summary> /// Gets the value of the property from the speficied source casted to specified type /// </summary> /// <typeparam name="T">The type to cast the return as</typeparam> /// <param name="p">The IMetaProperty defining the value to be returned</param> /// <param name="target">The source object</param> /// <returns>The source property casted to the specified type</returns> public static T GetValue <T>(this IMetaProperty p, IMetaObject target) { if (p is null) { throw new System.ArgumentNullException(nameof(p)); } if (target is null) { throw new System.ArgumentNullException(nameof(target)); } return(target.GetValue <T>(p.Name)); }
/// <summary> /// Gets the value of the property from the speficied source as a string /// </summary> /// <param name="p">The IMetaProperty defining the value to be returned</param> /// <param name="target">The source object</param> /// <returns>The value of the property from the speficied source as a string</returns> public static string GetValue(this IMetaProperty p, IMetaObject target) { if (p is null) { throw new System.ArgumentNullException(nameof(p)); } if (target is null) { throw new System.ArgumentNullException(nameof(target)); } return(target[p.Name].Value); }
/// <summary> /// Gets flags set on an enum property /// </summary> /// <param name="p">The propertyInfo</param> /// <param name="target">The instance of the object</param> /// <param name="otherFlags">returns a long representing the value of all flags on the object value that aren't declared explicitely</param> /// <returns>A list of the set enum values</returns> public static IList <EnumValue> GetFlags(this IMetaProperty p, IMetaObject target, out long otherFlags) { otherFlags = p.GetValue(target).Convert <long>(); List <EnumValue> toReturn = new List <EnumValue>(); foreach (EnumValue thisValue in p.GetFlags(target)) { toReturn.Add(thisValue); otherFlags &= ~thisValue.Value.Convert <long>(); } return(toReturn); }
/// <summary> /// Gets the view result containing editor view information for the current metaObject /// </summary> /// <param name="metaObject">The MetaObject to get view information for</param> /// <param name="requestContext">The current RequestContext</param> /// <param name="displayType">The type to be used when finding the view, if not the MetaObject type</param> /// <returns>A result containing editor view information</returns> protected DynamicViewResult?GetView(IMetaObject metaObject, DisplayContexts requestContext, IMetaType?displayType = null) { if (metaObject is null) { throw new ArgumentNullException(nameof(metaObject)); } string BasePath = $"/Areas/Admin/Views/{requestContext}/"; if (metaObject.IsRoot()) { return(null); } IMetaProperty property = metaObject.Property; displayType ??= GetDisplayType(metaObject); string Key = $"{displayType.AssemblyQualifiedName}+{property?.Name}+{requestContext}"; if (!Views.TryGetValue(Key, out string?path)) { DynamicRenderer renderer = new DynamicRenderer(new DynamicRendererSettings(displayType, property, this.FileProvider) { BasePath = BasePath }); if (renderer.IsDynamic) { Views.TryAdd(Key, null); } else { path = renderer.MatchedPath; Views.TryAdd(Key, path); } } if (string.IsNullOrWhiteSpace(path)) { return(null); } else { return(new DynamicViewResult(path)); } }
public static string DisplayName(this IMetaProperty property) { if (property is null) { throw new System.ArgumentNullException(nameof(property)); } string display = property.AttributeRef <DisplayAttribute, string>(nameof(DisplayAttribute.Name)); if (string.IsNullOrWhiteSpace(display)) { return(property.Name); } else { return(display); } }
/// <summary> /// Returns an instance of a property by IMetaProperty /// </summary> /// <param name="metaProperty">The property to search for</param> /// <returns>The property if exists, or Error if not</returns> public IMetaObject this[IMetaProperty metaProperty] // Indexer declaration { get { if (metaProperty is null) { throw new ArgumentNullException(nameof(metaProperty)); } if (metaProperty.Type.IsNullable || metaProperty.Type.CoreType == CoreType.Reference) { return(this.Properties.FirstOrDefault(p => p.Property.Name == metaProperty.Name)); } else { return(this.Properties.First(p => p.Property.Name == metaProperty.Name)); } } }
public static T MinValue <T>(this IMetaProperty p, T Default) { if (p is null) { throw new System.ArgumentNullException(nameof(p)); } if (p.Attributes.AnyNotNull(a => a.Type.Name == typeof(RangeAttribute).Name)) { T val = Default; foreach (T checkVal in p.Attributes.Where(a => a.Type.Name == typeof(RangeAttribute).Name).Select(a => a[nameof(RangeAttribute.Minimum)].GetValue <T>())) { val = Max(val, checkVal); } return(val); } else { return(Default); } }
public InputListPageModel(IMetaObject Model, IMetaProperty labelProperty, IMetaProperty valueProperty, string searchUrl) : base(Model) { if (Model is null) { throw new System.ArgumentNullException(nameof(Model)); } if (labelProperty is null) { throw new System.ArgumentNullException(nameof(labelProperty)); } if (valueProperty is null) { throw new System.ArgumentNullException(nameof(valueProperty)); } this.SearchUrl = searchUrl; this.LabelPropertyName = labelProperty.Name; this.ValuePropertyName = valueProperty.Name; this.SetUp(Model); }
/// <summary> /// Constructs a new instance of this renderer /// </summary> /// <param name="property">The IMetaProperty leading to this serialized object</param> /// <param name="fileProvider">A file provider used for checking for the existence of views </param> public DynamicRenderer(IMetaProperty property, IFileProvider fileProvider) : this(new DynamicRendererSettings(property, fileProvider)) { }
public string Validate(IMetaProperty metaProperty, object value, bool validateOrThrow) { return(metaProperty.Validate(value, validateOrThrow)); }
IMetaObject IMetaObject.this[IMetaProperty metaProperty] => this[metaProperty.Name];