8

I'm working on a .NET 4 project, and would be able to benefit from the dynamic property access that HyperDescriptor provides, but it doesn't seem to be working properly when built in .NET 4. I downloaded the source from CodeProject, converted the solution an projects to VS2010, and updated the target framework to 4.0. While it builds, and the sample executes correctly, the timings show that dynamic property access with HyperDescriptor is the slowest possible way of getting/setting object values.

This problem is only when you build HyperDescriptor from source with .NET 4. If from your .NET 4 project, you add a reference to HyperDescriptor built with .NET 2, it works fine. This is an acceptable solution for now, but would there be some potential advantage to using a .NET 4 build? Anyone want to take a crack at HyperDescriptor, see why it's so slow with a .NET 4 build?

Samuel Meacham
  • 9,845
  • 7
  • 42
  • 50

1 Answers1

13

I downloaded the source code and ran the test with .NET 4. There's an impressive number of InvalidOperationException thrown and caught, causing the slowness.

Go to HyperTypeDescriptionProvider.BuildDescriptor and replace:

[ReflectionPermission(SecurityAction.Assert, Flags = ReflectionPermissionFlag.AllFlags)]

by:

[SecuritySafeCritical]
[ReflectionPermission(SecurityAction.Assert, Unrestricted = true)]

AllFlags is deprecated and only causes a warning, but asserting from a security transparent method isn't valid in .NET 4. See Security Changes in the .NET Framework 4 for more information.

Julien Lebosquain
  • 38,690
  • 8
  • 99
  • 110
  • Worked perfectly, timings are super fast again. Thanks! – Samuel Meacham Jun 23 '10 at 22:35
  • Hi @julien, i tried to compile the project in .net 4.5.2, am getting below error. The Deny stack modifier has been obsoleted by the .NET Framework. any fix for this? thanks. – user1447718 Mar 26 '20 at 04:32
  • also, when i change the version to v4.0, i get error at static void Main() { // verify that things work OK without reflection access new ReflectionPermission(ReflectionPermissionFlag.AllFlags).Deny(); with error The Deny stack modifier has been obsoleted by the .NET Framework. – user1447718 Mar 26 '20 at 05:21
  • When i change the version to 4.5.2, i got above error. then i commented it and ran the code, it executed fine. but then the output file was blank. even with 4.0, the output file was blank. i went ahead and used the dll and tried to convert a List to datatable. but the table was empty. when i debugged, i below property was coming blank. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); appreciate any help in this regard. objective is to make it work with .net 4.5.2. thanks in advance. – user1447718 Mar 26 '20 at 06:34