0

I am using the SharpObservation framework to dynamically generate weak event references. When I reference an action from within the delegate I get a TypeAccessException.

Attempt by method 'DynamicClass.Construct(System.EventHandler1<XYZ>, System.Action11<XYZ>>, System.Delegate)' to access type 'System.Action3c__DisplayClass6,System.Object,XYZ>' failed.

If I remove the action then things work fine. I have even tried commenting out the entire body of the action and it still fails. I believe it has something to do with the compiler created method from the action is private or internal or something so that it can not be accessed from outside. How would I get around this?

  Action eventCompleted = () =>
    {
        SomeMethod();
    };

  EventHandler<XYZ> eventDelegate = delegate
    {          
      System.Windows.Application.Current.Dispatcher.BeginInvoke(eventCompleted);
    };

  newItem.Event += eventDelegate.MakeWeak();
Telavian
  • 3,752
  • 6
  • 36
  • 60

1 Answers1

0

I had to modify the source to not do the JIT access checks. There is some security problems with this. (Added the true parameter)

var dynamicMethod = new DynamicMethod("Construct", typeof(TDelegate), args, true);
Telavian
  • 3,752
  • 6
  • 36
  • 60