Skip to main content

Microsoft Silverlight

Answered Question DynamicMethod Invoke Instance MethodRSS Feed

(0)

Kieren5
Kieren5

Member

Member

26 points

34 Posts

DynamicMethod Invoke Instance Method

Is it possible to invoke an instance method from a DynamicMethod in Silverlight?

This sample shows how to access an instance field on a class:
http://msdn.microsoft.com/en-us/library/z43fsh67(VS.95).aspx

Is it possible to call a method on a class using a similar technique, any guidance or samples.

There is a sample for .Net 3.5, but it doesn't work in Silverlight because you can only use the first DynamicMethod constructor (the rest have a secuirty attribute :S ):
http://msdn.microsoft.com/en-us/library/ms228976.aspx

Cheers

samcov
samcov

Participant

Participant

969 points

379 Posts

Re: DynamicMethod Invoke Instance Method

Hi Kieren, take a look at this thread.  It may answer some of your questions.

http://silverlight.net/forums/t/14087.aspx

Sam...

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

Kieren5
Kieren5

Member

Member

26 points

34 Posts

Answered Question

Re: Re: DynamicMethod Invoke Instance Method

Using reflection emit and the technique described in this post (with sample code):

http://www.xtremedotnettalk.com/showthread.php?t=96952

I was able to invoke an instance method on a class.

The only change necessary is the constructor of the DynamicMethod. The sample code uses this constructor, which is not allowed in Silverlight (for security):
[SecurityCriticalAttribute]
public DynamicMethod(
string name,
Type returnType,
Type[] parameterTypes,
Type owner
)

Change it to this one:
public DynamicMethod(
string name,
Type returnType,
Type[] parameterTypes
)

samcov
samcov

Participant

Participant

969 points

379 Posts

Re: Re: DynamicMethod Invoke Instance Method

Kieren5:

Using reflection emit and the technique described in this post (with sample code):

http://www.xtremedotnettalk.com/showthread.php?t=96952

I was able to invoke an instance method on a class.

The only change necessary is the constructor of the DynamicMethod. The sample code uses this constructor, which is not allowed in Silverlight (for security):
[SecurityCriticalAttribute]
public DynamicMethod(
string name,
Type returnType,
Type[] parameterTypes,
Type owner
)

Change it to this one:
public DynamicMethod(
string name,
Type returnType,
Type[] parameterTypes
)

That information and the reasons for it are ALL in the link I gave you, but I'm glad you're satisfied.

"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities