Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Fast Property setting: DynamicMethod Problem
10 replies. Latest Post by cklein on May 28, 2008.
(0)
samcov
Participant
969 points
379 Posts
04-13-2008 6:48 PM |
For some reason, in SilverLight, our calls to DynamicMethod fail.
It reports the following: "Attempt to access the method failed: System.Reflection.Emit.DynamicMethod..ctor(System.String, System.Type, System.Type[], System.Type, Boolean)"
We really need this to work because setting properties on Lists of objects using regular reflection is WAY too slow.
Does anyone know what's going on here? BTW, thsi code works perfectly on the server(permission problem?).
Alex DeJ...
Member
62 points
11 Posts
04-13-2008 7:34 PM |
Access to the .ctor overloads that take the bool skipVisibility option are restricted (because turning off member visibility checks would be a security risk). If you're passing 'false' for this bool, use one of the overloads that don't take the parameter instead.
04-13-2008 11:02 PM |
Yes, I was passing 'false' to the visibility check, so I removed it, and I still get the message, however, it references the new constructor.
Additional information: Attempt to access the method failed: System.Reflection.Emit.DynamicMethod..ctor(System.String, System.Type, System.Type[], System.Type)
The strange thing is that both constructors compile just fine, and only die at runtime.
Help....
Jabb
462 points
81 Posts
04-14-2008 12:13 AM |
Make sure the constructor you use is not marked with the SecurityCritical attribute. I think the only constructor available to user code is
DynamicMethod(string name, Type returnType, Type[] parameterTypes)
DynamicMethods created from user code cannot have an owner, but you can still create a delegate that is bound to an object.
04-14-2008 3:02 AM |
Thanks Jabb, that constructor works.
Now I only have to modify my code since the type is unknown in the delegate now.
Sam...
04-14-2008 8:33 PM |
I've looked at emitting code in a partially trusted environments, and it isn't pretty.
Can anyone at MSFT confirm that we this will be the only way to get faster property get/set access in SL2?
Thanks,
Yi-Lun L...
All-Star
25052 points
2,747 Posts
04-15-2008 2:25 AM |
Hello, can you give more details about your scenario? What do you mean by setting properties on lists of objects? Why do you think emitting is faster than reflection? Can you show some code?
04-15-2008 2:47 PM |
Yi-Lun Luo - MSFT: Hello, can you give more details about your scenario? What do you mean by setting properties on lists of objects? Why do you think emitting is faster than reflection? Can you show some code?
Here's the case.
1. I query the server, and it returns XML2. The DataGrid won't let me set the cells without an object, so I have to create a collection of objects3. Each object has to have the properties/fields set by reflection
Now if I use SetProperty on each object, it takes minutes when there are a lot of fields, and on the order 2000 objects.
If I use DynamicMethod and gnerate a delegate for each field/property, it takes milliseconds.
This is a well known problem, and was the reason DynamicMethod has been a popular solution. My problem is that SilverLight doesn't support binding DynamicMethod to a Class, and therefore to use it, you have to create another AppDomain, and then unload it.
So my queston was to find out if MSFT is going to support the constructor that binds to a class, or remain with the anonymous creation.
04-15-2008 2:56 PM |
I forgot one thing.
The reason I need reflection is to free myself from coding each datagrid every time the object changes.
04-17-2008 1:25 AM |
Yi-Lun Luo, thanks for casting some doubt on the speed of reflection. While still not as fast as compiled code, the difference is negligible.
We went back and refactored the code we use for setting properties on objects, and found we were doing some things that really only needed to be done once, especially if we were doing this for a collection of objects.
The speed improvement was enormous, and we now have very acceptable performance.
The moral of this tale is that one should NEVER put off refactoring simply because the code base is stable(lol).
cklein
46 points
34 Posts
05-28-2008 3:34 AM |
I got bitten by the DynamicMethod constructor also. I hate to say this, but really, why the heck we need this securitycitical attribute anyhow?
Anyway, took me hours to figure out the security model, I called a constructor with SecuritySafeCritical attribute, that worked. But the type I built does not work anymore. I use System.Reflection.TypeBuilder to build a type, call its constructor to create an instance.
As I used the same code under desktop .net framework, it works fine. I port it to silverlight almost without change anything. The compilation goes while. But the dynamically built type does not work anymore. Is there any restrictions to this also?
This is getting so frustrating, even I know it's just beta.
Calvin