问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/3105763/does-hyperdescriptor-work-when-built-in-net-4