IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

problème avec Attribut perso


Sujet :

C#

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2008
    Messages
    28
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Mars 2008
    Messages : 28
    Points : 38
    Points
    38
    Par défaut problème avec Attribut perso
    Bonjour,

    j'ai créé un Attribut custom "AllowFilteringAttribute" qui hérite de BooleanFormatAttribute.
    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;
    		}
    	}
    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.

    exemple :
    sur la classe User, le filtre se fait sur Login et sur Name qui sont des string.
    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);
    			}
    		}
    }
    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
    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;
    		}
    Dans le cas d'un filtre sur User tout est Ok,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var attribute = propertyInfo.GetCustomAttributes(true).OfType<AllowFilteringAttribute>().FirstOrDefault();
    remonte bien l'attribut s'il est présent
    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 :
    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);
    			}
    		}
    Site est une propriété de User, qui hérite de EntityBase.
    Lorsque j'effectue le filtre,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var attribute = propertyInfo.GetCustomAttributes(true).OfType<AllowFilteringAttribute>().FirstOrDefault();
    ne me retourne aucun attribut pour la propriété Site.

    Donc si vous avez une solution à mon problème, merci d'avance.

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2008
    Messages
    28
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Mars 2008
    Messages : 28
    Points : 38
    Points
    38
    Par défaut
    Si je suis pas clair dites le moi. Mon explication me parait foireuse.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2008
    Messages
    28
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Mars 2008
    Messages : 28
    Points : 38
    Points
    38
    Par défaut solution
    je me réponds à moi même.
    J'utilise une autre méthode pour récupérer les attributs :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    					AllowFilteringAttribute attribute = (AllowFilteringAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(AllowFilteringAttribute));
    Je ne sais pas pourquoi celle ci marche mais bon j'ai la solution.

  4. #4
    Membre émérite
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Avril 2006
    Messages
    1 627
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 627
    Points : 2 331
    Points
    2 331
    Par défaut
    Ah, ta solution m'intéresse, merci d'avoir partagé

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [OL-2003] Problème avec attribut SenderEmailAddress
    Par julienjouanne dans le forum VBA Outlook
    Réponses: 0
    Dernier message: 23/02/2010, 12h10
  2. [GD] Problème avec attribution de couleurs
    Par rems033 dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 27/06/2008, 10h40
  3. [XSLT] problème avec attribut name
    Par hippoX dans le forum XSL/XSLT/XPATH
    Réponses: 7
    Dernier message: 17/04/2007, 17h26
  4. [Custom Tags 2.0] Problème avec les attributs
    Par uliss dans le forum Taglibs
    Réponses: 1
    Dernier message: 12/02/2006, 22h31
  5. Réponses: 1
    Dernier message: 25/11/2005, 20h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo