yuan8825399 / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

GetSettableProps(Type t) throw Null reference exception. #53

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
SUMMARY: GetSettableProps(Type t) throw Null reference exception.

It seems the exception happens in the following code:
...
Setter = p.DeclaringType == t ? p.GetSetMethod(true) : 
p.DeclaringType.GetProperty(p.Name).GetSetMethod(true),
...

there's a big chance that p.DeclaringType.GetProperty(p.Name) returns null, 
then the next GetSetMethod(true) is not feasible.

Original issue reported on code.google.com by jianghongfei@gmail.com on 27 Jul 2011 at 8:15

GoogleCodeExporter commented 9 years ago
Also getting this issue when using from the "Cassini" ASP.NET host when trying 
to access an internal property on another assembly  -- perhaps Cassini is 
running in medium trust, so it can't do reflection on private members? For my 
needs, I'm just removing the BindingFlags.Private from the GetProperties call 
in this containing method, until this is fixed.

Original comment by paulir...@gmail.com on 9 Aug 2011 at 5:00

GoogleCodeExporter commented 9 years ago
I have checked in my fix for this issue at a clone: 
http://code.google.com/r/paulirwin-dapper-dot-net/source/detail?r=0697f35f471687
329ae46732110ca6651afaae90

I didn't run or update unit tests or anything since I am just using the NuGet 
version and don't want to install Mercurial just yet, but thought it might be 
helpful to others in getting this issue resolved. I'll try and run unit tests 
tonight to see if all still pass.

Original comment by paulir...@gmail.com on 10 Aug 2011 at 1:15

GoogleCodeExporter commented 9 years ago
Issue occurs for types containing non-public inherited properties. Part of the 
code "p.DeclaringType.GetProperty(p.Name)" for non-public properties will 
always return null. Changing above part to "p.DeclaringType.GetProperty(p.Name, 
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)" will fix 
the issue.

Original comment by siz...@gmail.com on 16 Aug 2011 at 12:52

GoogleCodeExporter commented 9 years ago
Thanks for looking into it, comment #3. However, that doesn't fix the issue if 
you aren't running in full trust - even with specifying those flags the method 
will return null. Actually, those flags are already specified on the prior 
GetProperties call, so specifying the flags on GetProperty is redundant. If 
Dapper is going to support partial trust, it needs to not to non-public member 
reflection. My fix (although rudimentary) effectively detects partial trust, 
and changes the flags to not include NonPublic if you aren't in full trust to 
still provide public-level access in partial trust while still retaining 
private/internal access in full trust. I'm interested in seeing, though, if 
someone comes up with a more elegant solution than my "fail detection" fix.

Original comment by paulir...@gmail.com on 16 Aug 2011 at 1:19

GoogleCodeExporter commented 9 years ago
To comment #4. Actually, specifying flags in GetProperty method is not 
redundant because GetProperty method without flags specified searches type only 
for Public instance or static properties. 

For the same reason your partially trusted environment detection functionality 
will always fail because "typeof(FullTrustTester).GetProperty("Property")" will 
always return null no matter what environment code will run at.

Original comment by siz...@gmail.com on 16 Aug 2011 at 8:09

GoogleCodeExporter commented 9 years ago
Ah, good catch. I was so focused on the partial trust issue, I just copied the 
existing call from the other method as-is and modified it to use the new type. 
Thanks for catching that!

Original comment by paulir...@gmail.com on 16 Aug 2011 at 8:13

GoogleCodeExporter commented 9 years ago
I have checked in that change, and fixed an issue where TortoiseHg was 
reporting that I backed out and re-added the entire file, which wasn't the 
case. Ran all unit tests in full trust and they all pass. 

Original comment by paulir...@gmail.com on 16 Aug 2011 at 9:17