/// <summary>Return a datatable of links for the specified type.</summary> /// <param name="type">The type to document.</param> private DataTable GetLinks(Type type) { DataTable links = new DataTable("Links"); links.Columns.Add("Name", typeof(string)); links.Columns.Add("Type", typeof(string)); links.Columns.Add("IsOptional?", typeof(bool)); foreach (FieldInfo field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy)) { LinkAttribute linkAttribute = field.GetCustomAttribute <LinkAttribute>(); if (linkAttribute != null) { DataRow row = links.NewRow(); row["Name"] = field.Name; row["IsOptional?"] = linkAttribute.IsOptional; row["Type"] = GetTypeName(field.FieldType); links.Rows.Add(row); } } if (links.Rows.Count == 0) { return(null); } else { return(links); } }
public void Link_GetOrderNotSetDefaultsToIntMaxValue_ReturnsDefaultOrderValue() { var systemUnderTest = new LinkAttribute("LinkName"); var order = systemUnderTest.Order; Assert.IsTrue(order == int.MaxValue); }
public void Link_GetOrderSetValue_ReturnsSetOrderValue() { var systemUnderTest = new LinkAttribute("LinkName"); systemUnderTest.Order = 1; var order = systemUnderTest.Order; Assert.IsTrue(order == 1); }
/// <summary> /// Create and return a new Link object for member /// </summary> /// <param name="field">The member</param> /// <param name="model">Model with the link</param> private static ModelDoc.Link DocumentLink(FieldInfo field, IModel model) { ModelDoc.Link link = new ModelDoc.Link(); link.Name = field.Name; if (field.FieldType.IsGenericType && field.FieldType.GetInterface("IList") != null) { link.TypeName = "List<" + field.FieldType.GenericTypeArguments[0].Name + ">"; } else { link.TypeName = field.FieldType.Name; } UnitsAttribute units = field.GetCustomAttribute <UnitsAttribute>(); if (units != null) { link.Units = units.ToString(); } DescriptionAttribute description = field.GetCustomAttribute <DescriptionAttribute>(); if (description != null) { link.Description = description.ToString(); } LinkAttribute linkAtt = field.GetCustomAttribute <LinkAttribute>(); link.IsOptional = linkAtt.IsOptional; object linkedObject = field.GetValue(model); if (linkedObject != null) { if (linkedObject is IModel) { link.LinkedModelName = Apsim.FullPath(linkedObject as IModel); } else { } } return(link); }
/// <summary> /// Get a table of all dependencies (Links) of a given model /// which match a given predicate. /// </summary> /// <param name="model">The model.</param> /// <param name="filter">Predicate on which to filter dependencies.</param> /// <returns></returns> private DataTable GetDependencies(IModel model, Predicate <MemberInfo> filter) { DataTable result = new DataTable("Functions"); result.Columns.Add(new DataColumn("Name", typeof(string))); result.Columns.Add(new DataColumn("Type", typeof(string))); result.Columns.Add(new DataColumn("Link Type", typeof(string))); result.Columns.Add(new DataColumn("Link by Name", typeof(string))); result.Columns.Add(new DataColumn("Optional", typeof(string))); result.Columns.Add(new DataColumn("Path", typeof(string))); result.Columns.Add(new DataColumn("Description", typeof(string))); result.Columns.Add(new DataColumn("Remarks", typeof(string))); BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; foreach (MemberInfo member in model.GetType().GetMembers(flags)) { LinkAttribute link = member.GetCustomAttribute <LinkAttribute>(); if (link != null && filter(member)) { DataRow row = result.NewRow(); row[0] = member.Name; row[1] = GetMemberType(member).Name; row[2] = link.Type.ToString(); row[3] = link.ByName.ToString(); row[4] = link.IsOptional.ToString(); row[5] = link.Path; row[6] = AutoDocumentation.GetSummary(member); row[7] = AutoDocumentation.GetRemarks(member); result.Rows.Add(row); } } return(result); }
public Boolean SetAttribute(LinkAttribute attr) { // Adds depending on the Link Format rules return(LinkFormat.AddAttribute(Attributes, attr)); }
public static IHtmlString Table <TModel, TRowType>(this HtmlHelper <TModel> html, IEnumerable <TRowType> rows, string targetId, out WebGrid grid, object htmlAttributes = null, string fieldNamePrefix = null, string ajaxUpdateCallbackFunction = null, params WebGridColumn[] commandColumns) { ModelMetadata rowMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TRowType)); List <WebGridColumn> columns = rowMetadata.Properties .Where(p => p.ShowForDisplay) .Select(p => { WebGridColumn col = new WebGridColumn { ColumnName = p.PropertyName, Header = p.DisplayName, CanSort = true }; if (p.DataTypeName == DataType.Currency.ToString() || p.ModelType.IsAssignableFrom(typeof(decimal)) || p.ModelType.IsAssignableFrom(typeof(double)) || p.ModelType.IsAssignableFrom(typeof(int))) { // if (p.ModelType.IsAssignableFrom(typeof(int)) || p.ModelType.IsAssignableFrom(typeof(int?))) col.Style = "right-cell"; } else if (p.DataTypeName == DataType.Date.ToString() || p.DataTypeName == DataType.DateTime.ToString()) { col.Style = "center-cell"; } //PropertyInfo propertyInfo = p.ContainerType.GetProperty(p.PropertyName); LinkAttribute linkAttribute = p.ContainerType.GetProperty(p.PropertyName).GetCustomAttributes(true).OfType <LinkAttribute>().FirstOrDefault(); if (linkAttribute != null) { // (string)getPropertyValue(p, item) -> ((object)getPropertyValue(p, item)).ToString() col.Format = (item) => { RouteValueDictionary routeValueDictionary = new RouteValueDictionary(); PropertyInfo valuePropertyInfo = p.ContainerType.GetProperty(linkAttribute.FieldId); dynamic idValue = valuePropertyInfo.GetValue((item as WebGridRow).Value); string text = (string)getPropertyValue(p, item); if (idValue != null) { routeValueDictionary.Add(linkAttribute.IdParamName, /* valuePropertyInfo.GetValue((item as WebGridRow).Value) */ idValue); return(html.ActionLink(/*(string)getPropertyValue(p, item)*/ text, linkAttribute.ActionName, linkAttribute.ControllerName, routeValueDictionary, null)); } else { return(text); } }; } if (!string.IsNullOrEmpty(p.DisplayFormatString)) { col.Format = (item) => string.Format(p.DisplayFormatString, /* Utils.GetPropValue(item, p.PropertyName) */ arg0: getPropertyValue(p, item)); } else if (p.ModelType.IsAssignableFrom(typeof(bool))) { col.Format = (item) => html.Raw("<input type='checkbox' " + ((getPropertyValue(p, item) == true) ? "checked" : "") + " disabled='disabled' />"); col.Style = "center-cell"; } return(col); }).ToList(); if (commandColumns != null) { columns.AddRange(commandColumns); } grid = new WebGrid(rows as IEnumerable <dynamic>, ajaxUpdateContainerId: targetId, ajaxUpdateCallback: ajaxUpdateCallbackFunction, fieldNamePrefix: fieldNamePrefix, selectionFieldName: "SelectedRow"); string className = "table table-bordered table-striped"; return(grid.GetHtml(tableStyle: className, columns: columns, htmlAttributes: htmlAttributes)); }
public void Link_GetTypeId_ReturnsTypeId() { var systemUnderTest = new LinkAttribute("LinkName"); Assert.IsNotNull(systemUnderTest.TypeId); }
internal static Boolean AddAttribute(ICollection<LinkAttribute> attributes, LinkAttribute attrToAdd) { if (IsSingle(attrToAdd.Name)) { foreach (LinkAttribute attr in attributes) { if (attr.Name.Equals(attrToAdd.Name)) { if (log.IsDebugEnabled) log.Debug("Found existing singleton attribute: " + attr.Name); return false; } } } // special rules if (attrToAdd.Name.Equals(ContentType) && attrToAdd.IntValue < 0) return false; if (attrToAdd.Name.Equals(MaxSizeEstimate) && attrToAdd.IntValue < 0) return false; attributes.Add(attrToAdd); return true; }