Skip to main content

Microsoft Silverlight

Answered Question Fast Property setting: DynamicMethod ProblemRSS Feed

(0)

samcov
samcov

Participant

Participant

969 points

379 Posts

Fast Property setting: DynamicMethod Problem

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?).

"The difference between genius and stupidity is that genius has its limits." - Albert Einstein

Alex DeJarnatt - MSFT
Alex DeJ...

Member

Member

62 points

11 Posts

Microsoft

Re: Fast Property setting: DynamicMethod Problem

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.

Alex DeJarnatt
Connected Framework Team - Silverlight
Microsoft

samcov
samcov

Participant

Participant

969 points

379 Posts

Re: Fast Property setting: DynamicMethod Problem

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....

"The difference between genius and stupidity is that genius has its limits." - Albert Einstein

Jabb
Jabb

Member

Member

462 points

81 Posts

Re: Fast Property setting: DynamicMethod Problem

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.

samcov
samcov

Participant

Participant

969 points

379 Posts

Re: Fast Property setting: DynamicMethod Problem

Thanks Jabb, that constructor works.

Now I only have to modify my code since the type is unknown in the delegate now.

Sam...

"The difference between genius and stupidity is that genius has its limits." - Albert Einstein

samcov
samcov

Participant

Participant

969 points

379 Posts

Re: Fast Property setting: DynamicMethod Problem

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,

Sam...

"The difference between genius and stupidity is that genius has its limits." - Albert Einstein

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Fast Property setting: DynamicMethod Problem

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?

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

samcov
samcov

Participant

Participant

969 points

379 Posts

Re: Fast Property setting: DynamicMethod Problem

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 XML
2.  The DataGrid won't let me set the cells without an object, so I have to create a collection of objects
3.  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.

Thanks,

Sam...

"The difference between genius and stupidity is that genius has its limits." - Albert Einstein

samcov
samcov

Participant

Participant

969 points

379 Posts

Re: Fast Property setting: DynamicMethod Problem

I forgot one thing.

The reason I need reflection is to free myself from coding each datagrid every time the object changes.

Sam...

"The difference between genius and stupidity is that genius has its limits." - Albert Einstein

samcov
samcov

Participant

Participant

969 points

379 Posts

Answered Question

Re: Fast Property setting: DynamicMethod Problem

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).

Thanks,

Sam...

"The difference between genius and stupidity is that genius has its limits." - Albert Einstein

cklein
cklein

Member

Member

46 points

34 Posts

Re: Fast Property setting: DynamicMethod Problem

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 

Live by a Better Code

http://www.raincoder.com

Equal parts art and science

Email: cguo@raincoder.com

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities