Bonjour,
j'ai créé un Attribut custom "AllowFilteringAttribute" qui hérite de BooleanFormatAttribute.
J'ai créé une autocompletetextbox, cet attribut est utilisé pour marquer les propriétés sur lesquelles le filtre ce fera lorsque l'utilisateur tape du texte.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 /// <summary> /// indicate if we can filter on this property in autocompletextbox /// </summary> public class AllowFilteringAttribute : BooleanFormatAttribute { public AllowFilteringAttribute(bool value) : base(value) { } } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)] public abstract class BooleanFormatAttribute : Attribute { public bool Value; public BooleanFormatAttribute(bool value) { this.Value = value; } }
exemple :
sur la classe User, le filtre se fait sur Login et sur Name qui sont des string.
lors du filtre je récupère les propriétés qui possèdent l'attribut [AllowFiltering(true)].
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class User : EntityBase<User> {[Display( GroupName = FrameworkConstants.PROP_CATEGORY_GENERAL, AutoGenerateField = true, Order = 2, Description = "Login", Prompt = "the user login")] [IsNullable(false)] [Required] [MaxLength(16)] [AllowFiltering(true)] public virtual string Login { get { return _login; } set { this.ChangeProperty<string>(() => Login, ref this._login, value); } } [Display( GroupName = FrameworkConstants.PROP_CATEGORY_GENERAL, AutoGenerateField = true, Order = 3, Description="Name", Prompt="the user name"), ] [IsNullable(false)] [Required] [MaxLength(255)] [AllowFiltering(true)] public virtual string Name { get { return _name; } set { this.ChangeProperty<string>(() => Name, ref this._name, value); } } }
Dans le cas d'un filtre sur User tout est Ok,
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public bool FilterItem(object item) { bool isMatched = false; if (String.IsNullOrEmpty(this._txt_Filter.Text)) { isMatched = true; } else { Type type = item.GetType(); foreach (var propertyInfo in type.GetProperties()) { var attribute = propertyInfo.GetCustomAttributes(true).OfType<AllowFilteringAttribute>().FirstOrDefault(); if (attribute != null) { if (attribute.Value == true) { switch (this.FilteringMode) { case FilteringMode.Contains: isMatched = type.GetProperty(propertyInfo.Name).GetValue(item, null).ToString().ToLowerInvariant().Contains(this._txt_Filter.Text.ToLowerInvariant()); break; case FilteringMode.StartWith: isMatched = type.GetProperty(propertyInfo.Name).GetValue(item, null).ToString().ToLowerInvariant().StartsWith(this._txt_Filter.Text.ToLowerInvariant()); break; default: break; } } } if (isMatched) return isMatched; } } return isMatched; }
remonte bien l'attribut s'il est présent
Code : Sélectionner tout - Visualiser dans une fenêtre à part var attribute = propertyInfo.GetCustomAttributes(true).OfType<AllowFilteringAttribute>().FirstOrDefault();
En fait l'attribut est bien trouvé lorsque la propriété est un type de base (genre string, int...).
Par contre l'attribut n'est pas trouvé lorsque la propriété est de type "EntityBase" (mes objets).
ex :
Site est une propriété de User, qui hérite de EntityBase.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 [AllowFiltering(true)][/INDENT]public virtual Site Site { get { return _site; } set { this.ChangeProperty<Site>(() => Site, ref this._site, value); } }
Lorsque j'effectue le filtre,
ne me retourne aucun attribut pour la propriété Site.
Code : Sélectionner tout - Visualiser dans une fenêtre à part var attribute = propertyInfo.GetCustomAttributes(true).OfType<AllowFilteringAttribute>().FirstOrDefault();
Donc si vous avez une solution à mon problème, merci d'avance.
Partager