Check for nulls and display the source of the error in the exception
message when a decorated member is not settable/gettable.

-- 
Emanuele Aina
Studio Associato Di Nunzio e Di Gregorio
http://dndg.it/
Via Maria Vittoria, 2
10123 Torino - Italy

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DbLinq" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/dblinq?hl=en
-~----------~----~----~----~------~----~------~--~---

>From 77e3f34c9a14b2d32ac9a354488a09a7c13db13f Mon Sep 17 00:00:00 2001
From: Emanuele Aina <[email protected]>
Date: Fri, 4 Sep 2009 15:26:30 +0200
Subject: [PATCH 7/7] Improve error handling in (Get|Set)MemberValue()

---
 src/DbLinq/Util/MemberInfoExtensions.cs |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/DbLinq/Util/MemberInfoExtensions.cs b/src/DbLinq/Util/MemberInfoExtensions.cs
index cbc9324..5f2794d 100644
--- a/src/DbLinq/Util/MemberInfoExtensions.cs
+++ b/src/DbLinq/Util/MemberInfoExtensions.cs
@@ -81,11 +81,17 @@ namespace DbLinq.Util
         /// <param name="o">The object</param>
         public static object GetMemberValue(this MemberInfo memberInfo, object o)
         {
+            if (memberInfo == null)
+                throw new ArgumentNullException("memberInfo");
+            if (o == null)
+                throw new ArgumentNullException("o");
             if (memberInfo is FieldInfo)
                 return ((FieldInfo)memberInfo).GetValue(o);
-            if (memberInfo is PropertyInfo)
+            if (memberInfo is PropertyInfo && ((PropertyInfo)memberInfo).CanRead)
                 return ((PropertyInfo)memberInfo).GetGetMethod().Invoke(o, new object[0]);
-            throw new ArgumentException();
+            throw new ArgumentException(String.Format(
+                "Member {0}.{1} is not a field not a readable property",
+                memberInfo.DeclaringType, memberInfo.Name));
         }
 
         /// <summary>
@@ -96,11 +102,17 @@ namespace DbLinq.Util
         /// <param name="value">The field/property value to assign</param>
         public static void SetMemberValue(this MemberInfo memberInfo, object o, object value)
         {
+            if (memberInfo == null)
+                throw new ArgumentNullException("memberInfo");
+            if (o == null)
+                throw new ArgumentNullException("o");
             if (memberInfo is FieldInfo)
                 ((FieldInfo)memberInfo).SetValue(o, value);
-            else if (memberInfo is PropertyInfo)
+            else if (memberInfo is PropertyInfo && ((PropertyInfo)memberInfo).CanWrite)
                 ((PropertyInfo)memberInfo).GetSetMethod().Invoke(o, new[] { value });
-            else throw new ArgumentException();
+            else throw new ArgumentException(String.Format(
+                "Member {0}.{1} is not a field not a writable property",
+                memberInfo.DeclaringType, memberInfo.Name));
         }
 
         /// <summary>
-- 
1.6.3.3

Reply via email to