Bonjour,
J'ai testé l'utilisation d'une Expression (System.Linq.Expressions) dans le cas d'une exception personnalisée :Cela compile mais FxCop m'indique une erreur critique "DoNotNestGenericTypesInMemberSignatures".
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 [Serializable] public PropertyException<T> : Exception, ISerializable { public PropertyException(Expression<Func<T, object>> propertySelector) : base(GetMessage(propertySelector)) { } static string GetMessage(Expression<Func<T, object>> propertySelector) { MemberExpression memberExpr = (MemberExpression)propertySelector.Body; return String.Format(CultureInfo.InvariantCulture, MyResources.PropertyErrorMessage, memberExpr.Member.Name); // propertyName } [...] }
La description m'indique le texte suivant :
Je sais qu'FxCop n'est là que pour aider/conseiller lors du developpement.Resolution : "Consider a design where
'PropertyException<T>.PropertyException(Expression<Func<T, object>>)'
doesn't nest generic type 'Expression<Func<T, object>>'."
Info : "Avoid API that require users to instantiate a generic
type with another generic type as type argument. The
syntax gets too complex."
Mais je ne comprends pas en quoi la syntaxe est trop complexe ?
A l'utilisation on obtient un code plutôt simple je trouve :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 public string DoSomething(MyClass myObject) { if (myObject == null) throw new ArgumentNullException("myObject"); if (myObject.Id == null) throw new PropertyNullException<MyClass>(o => o.Id); [...] }
Partager