/// <summary> /// 根据实体类,生成dataGridView表头 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dataGridView"></param> public static void SetDataGridView <T>(DataGridView dataGridView) { Type type = typeof(T); foreach (var item in type.GetProperties()) { object[] objAttrs = item.GetCustomAttributes(true); foreach (var attr in objAttrs) { if (attr is PropertyNameAttribute) { PropertyNameAttribute propertyNameAttribute = attr as PropertyNameAttribute; if (propertyNameAttribute.IsShow) { break; } DataGridViewTextBoxColumn dgColumn = new DataGridViewTextBoxColumn(); dgColumn.Name = item.Name; dgColumn.DataPropertyName = item.Name; dgColumn.HeaderText = propertyNameAttribute.ShowCName; dataGridView.Columns.Add(dgColumn); } } } }
/// <summary> /// Initializes a new instance of the <see cref="ChildPropertyInfo"/> class. /// </summary> /// <param name="property">The property.</param> /// <param name="itemType">Type of the item.</param> public ChildPropertyInfo(PropertyInfo property, Type itemType) { Property = property ?? throw new ArgumentNullException(nameof(property)); ItemType = itemType ?? throw new ArgumentNullException(nameof(itemType)); _childPropertyNameAttribute = property.GetCustomAttribute <PropertyNameAttribute>(true); Accessor = CreateGetter(property).Compile(); ShouldVisitChildren = GetShouldVisitChildrenGetter(property).Compile(); }
public void CreateProperty <T, TProperty>(Expression <Func <T, TProperty> > propertyExpr) where T : CrmPlusPlusEntity, new() { var entityName = EntityNameAttribute.GetFromType <T>(); var propertyName = PropertyNameAttribute.GetFromType(propertyExpr); var propertyInfo = PropertyInfoAttribute.GetFromType(propertyExpr); var attributes = ((MemberExpression)propertyExpr.Body).Member.GetCustomAttributes(true); CreateProperty(entityName, typeof(TProperty), attributes, propertyName, propertyInfo); }
public QueryFilterBuilder <T> Condition <TProperty>(Expression <Func <T, TProperty> > propertyExpr, ConditionOperator conditionOperator, string value) { var condition = new XElement("condition"); condition.Add(new XAttribute("attribute", PropertyNameAttribute.GetFromType(propertyExpr))); condition.Add(new XAttribute("operator", conditionOperator.Value.Trim().ToLower())); condition.Add(new XAttribute("value", value)); RootElement.Add(condition); return(this); }
// Method GetColumnNameInSQLByPropertyName public static string GetColumnNameInSQLByPropertyName <T>(string propertyName) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); PropertyNameAttribute propName = properties.Cast <PropertyDescriptor>().Where(p => (PropertyNameAttribute)p.Attributes[typeof(PropertyNameAttribute)] != null && p.DisplayName == propertyName).Select(p => ((PropertyNameAttribute)p.Attributes[typeof(PropertyNameAttribute)])).FirstOrDefault(); if (propName != null) { return(propName.Name); } return(""); }
/// <summary> /// 更新 /// WHERE {whereStr} /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <param name="whereStr"></param> /// <returns></returns> public bool Update <T>(T t, string whereStr) { var type = typeof(T); var values = new List <string>(); //无法将类型为“System.Data.SqlClient.SQLiteParameter”的对象强制转换为类型“System.Data.SQLite.SQLiteParameter”。 var par = new List <SQLiteParameter>(); bool isTableColumn = true; foreach (var item in type.GetProperties()) { isTableColumn = true; object[] objArr = item.GetCustomAttributes(true); foreach (var item2 in objArr) { if (item2 is PropertyNameAttribute) { PropertyNameAttribute pro = item2 as PropertyNameAttribute; if (pro.IsTableColumn) { isTableColumn = false; break; } } } if (isTableColumn) { var value = item.GetValue(t, null); if (item.Name.ToLower() != "id") { par.Add(new SQLiteParameter(item.Name, value)); values.Add(string.Format($"[{item.Name}] = @{item.Name}")); } } } var v = string.Join(",", values.ToArray()); var sql = string.Format($"UPDATE [{ExtendTableName.GetTableName(type)}] SET {v} WHERE {whereStr}"); return(ExcuteSql <bool>(sql, cmd => { foreach (var parameter in par) { cmd.Parameters.Add(parameter); } var result = cmd.ExecuteNonQuery(); return result > 0; })); // return RunCmd(sql, par.ToArray()); }
public Query <T> JoinNTo1 <TRelatedEntity>(Expression <Func <T, EntityReference <TRelatedEntity> > > joinExpr, JoinType joinType, Action <Query <TRelatedEntity> > queryBuilder) where TRelatedEntity : CrmPlusPlusEntity, new() { var to = PropertyNameAttribute.GetFromType(joinExpr); var from = EntityNameAttribute.GetFromType <TRelatedEntity>() + "id"; var entityQuery = new Query <TRelatedEntity>(query, from, to, joinType, linkedEntityDepth + 1); queryBuilder(entityQuery); EntityRootElement.Add(entityQuery.EntityRootElement); return(this); }
/// <summary> /// 将设置类转换成js设置类格式,如:animation:false /// </summary> /// <param name="settings"></param> /// <returns></returns> protected virtual string ToJSSettings() { if (Id.IsNullOrWhiteSpace()) { throw new ArgumentNullException("id"); } Type type = this.GetType(); string js = ""; foreach (var property in type.GetProperties()) //获取所有的属性 { string jsProperty = ""; //js属性名 string jsValue = ""; //js属性值 dynamic attrs = Attribute.GetCustomAttribute(property, typeof(PropertyNameAttribute)); if (attrs != null) { PropertyNameAttribute propertyName = (PropertyNameAttribute)attrs; if (propertyName.IsIngoreForJavaScript) { continue; } jsProperty = propertyName.PropertyName.LowercaseFirst(); } if (jsProperty.IsNullOrWhiteSpace()) { jsProperty = property.Name.LowercaseFirst(); } if (jsValue.IsNullOrWhiteSpace()) { jsValue = property.GetValue(this, null) == null ? "" : property.GetValue(this, null).ToString(); } if (!jsValue.IsNullOrWhiteSpace()) //如果属性值不为空,则显示 { if (property.PropertyType == typeof(string) || property.PropertyType.BaseType == typeof(Enum)) { js += jsProperty + ":'" + jsValue.LowercaseFirst() + "',"; } else { js += jsProperty + ":" + jsValue.LowercaseFirst() + ","; } } } return(js.TrimEnd(',')); }
public Retrieval <T> Include <TProperty>(Expression <Func <T, TProperty> > propertyExpr) { var propertyName = PropertyNameAttribute.GetFromType(propertyExpr); if (propertyName == "modifiedon" || propertyName == "createdon" || propertyName == "id") { return(this); } if (!IncludedColumns.Contains(propertyName.ToLower())) { IncludedColumns.Add(propertyName); } return(this); }
private T CreateT <T>(SQLiteDataReader reader) { var type = typeof(T); var t = Activator.CreateInstance(type); bool isTableColumn = true; foreach (PropertyInfo propertyInfo in type.GetProperties()) { object[] objArr = propertyInfo.GetCustomAttributes(true); isTableColumn = true; foreach (var item in objArr) { if (item is PropertyNameAttribute) { PropertyNameAttribute proper = item as PropertyNameAttribute; isTableColumn = proper.IsTableColumn; break; } } if (objArr.Length > 0 || !isTableColumn) { if (!isTableColumn) { if (propertyInfo.CanWrite) { var value = reader[propertyInfo.Name]; if (!(value is DBNull)) { propertyInfo.SetValue(t, value, null); } } } } else { if (propertyInfo.CanWrite) { var value = reader[propertyInfo.Name]; if (!(value is DBNull)) { propertyInfo.SetValue(t, value, null); } } } } return((T)t); }
internal Tuple <IList <string>, IEnumerable <PropertyInfo> > LoadEntity <T2>() { IEnumerable <PropertyInfo> listaPropiedades; IList <string> listaNombresColumnas; var c = typeof(T2).GetConstructors(); T2 entidadResponse = (T2)c.First().Invoke(new Object[0]); listaPropiedades = ensamblado.GetType(entidadResponse.GetType().FullName, true).GetProperties().ToList(); listaNombresColumnas = new List <string>(); listaPropiedades.ToList().ForEach(property => { var attributes = property.GetCustomAttributes(false); PropertyNameAttribute columnMapping = (PropertyNameAttribute)attributes.FirstOrDefault(a => a.GetType() == typeof(PropertyNameAttribute)); listaNombresColumnas.Add(columnMapping.MappingName); }); return(new Tuple <IList <string>, IEnumerable <PropertyInfo> >(listaNombresColumnas, listaPropiedades)); }
public void CreateOneToManyRelationship <TOne, TMany>(Expression <Func <TMany, EntityReference <TOne> > > lookupExpr, EntityAttributes.Metadata.AttributeRequiredLevel lookupRequiredLevel, string relationshipPrefix = "new") where TOne : CrmPlusPlusEntity, new() where TMany : CrmPlusPlusEntity, new() { Guard.This(relationshipPrefix).AgainstNullOrEmpty(); var oneEntityName = EntityNameAttribute.GetFromType <TOne>(); var manyEntityName = EntityNameAttribute.GetFromType <TMany>(); var oneDisplayName = EntityInfoAttribute.GetFromType <TOne>().DisplayName; var lookupPropertyName = PropertyNameAttribute.GetFromType(lookupExpr); var oneToManyRequest = new CreateOneToManyRequest { OneToManyRelationship = new OneToManyRelationshipMetadata { ReferencedEntity = oneEntityName, ReferencingEntity = manyEntityName, SchemaName = relationshipPrefix.EndsWith("_") ? relationshipPrefix : relationshipPrefix + "_" + oneEntityName + "_" + manyEntityName, AssociatedMenuConfiguration = new AssociatedMenuConfiguration { Behavior = AssociatedMenuBehavior.UseLabel, Group = AssociatedMenuGroup.Details, Label = oneDisplayName.ToLabel(), Order = 10000 }, CascadeConfiguration = new CascadeConfiguration { Assign = CascadeType.NoCascade, Delete = CascadeType.RemoveLink, Merge = CascadeType.NoCascade, Reparent = CascadeType.NoCascade, Share = CascadeType.NoCascade, Unshare = CascadeType.NoCascade } }, Lookup = new LookupAttributeMetadata { SchemaName = lookupPropertyName, DisplayName = (oneDisplayName + " Lookup").ToLabel(), RequiredLevel = new AttributeRequiredLevelManagedProperty(lookupRequiredLevel.ToSimilarEnum <AttributeRequiredLevel>()), Description = (oneDisplayName + " Lookup").ToLabel() } }; service.Execute(oneToManyRequest); }
public Query <T> Include <TProperty>(Expression <Func <T, TProperty> > propertyExpr) { if (!EntityRootElement.Elements().Any(e => e.Name == "all-attributes")) { var propertyName = PropertyNameAttribute.GetFromType(propertyExpr); if (propertyName == "modifiedon" || propertyName == "createdon" || propertyName == "id" || linkedEntityDepth > 1) { return(this); } var element = new XElement("attribute"); element.Add(new XAttribute("name", propertyName)); EntityRootElement.Add(element); } return(this); }
private string GetSql <T>(T t, bool IsGetId, ref List <SQLiteParameter> par) { //插入实体时要判断一下时间的插入.还可以通过特性来判断一下数据的长度 var type = typeof(T); var keys = new List <string>(); var values = new List <string>(); bool isTableColumn = true; foreach (var item in type.GetProperties()) { isTableColumn = true; object[] objArr = item.GetCustomAttributes(true); foreach (var obj in objArr) { if (obj is PropertyNameAttribute) { PropertyNameAttribute propertyNameAttribute = obj as PropertyNameAttribute; if (propertyNameAttribute.IsTableColumn || propertyNameAttribute.IsPrimaryKey == 1) { isTableColumn = false; break; } } } if (isTableColumn) { var value = item.GetValue(t, null); keys.Add(item.Name); values.Add("@" + item.Name); par.Add(new SQLiteParameter("@" + item.Name, value)); } } var c = string.Join(",", keys.ToArray()); var v = string.Join(",", values.ToArray()); string sqlStr = string.Format($"INSERT INTO [{ExtendTableName.GetTableName(type)}] ({c}) " + $"VALUES({v})"); if (IsGetId) { sqlStr += ";SELECT last_insert_rowid();"; } return(sqlStr); }
// Method HashObjectToArray public static dynamic HashObjectToDic(this object obj) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj.GetType()); Dictionary <string, string> dics = new Dictionary <string, string>(); dynamic dys = new ExpandoObject(); for (int i = 0; i < properties.Count; i++) { PropertyNameAttribute prop = (PropertyNameAttribute)properties[i].Attributes[typeof(PropertyNameAttribute)]; if (prop != null) { dics.Add(prop.Name, properties[i].GetValue(obj) == null ? null : properties[i].GetValue(obj).ToString()); } } return(dys = dics); }