Dynamic assemblies and serialization

The AbstractBindableWrapper is getting more and more difficult. I am facing a lot of problems regarding Windows Forms and WPF databinding and of course regarding Reflection.Emit. AOP is very cool but is not easy to implement. I've written a dynamic proxy generator which creates an assembly and classes inside it. The idea is to have a double proxy for every business object that needs to be bound to the UI.

  1. The first proxy extends the business object and implements the INotifyPropertyChanged interface. This proxy is created with the DynamicPropertyChangedProxy class. It has been a challenge to write that class.
  2. The second one uses the interception pattern and is generated by Castle.DynamicProxy2 and its purpose is to raise the PropertyChanged event everytime the bound properties are changed. This second extends from the first

Now the last problem I've found is the deserialization of a class that is contained within a dynamic assembly, that is, the deserialization of the proxies. I've found also the solution and implemented it. It is working fine.

"In general, the formatter, during deserialization, loads the assembly by calling Load or LoadWithPartialName and because of which CLR will not find a dynamic assembly. One solution is to provide an event handler for AppDomain.CurrentDomain.AssemblyResolve event which gets called whenever the formatter fails to load an assembly. The identity of the assembly that failed during load is passed to this method. Your event handler can use the name to construct a path where the assembly file is available and use Assembly.LoadFrom(assemblyPath) to return Assembly."

   1:  public static Assembly OnAssemblyResolve(Object sender, ResolveEventArgs args)
   2:  {
   3:      if (args.Name.StartsWith(DYNAMIC_MOD_NAME))
   4:      {
   5:          return Assembly.GetAssembly(MyAssemblyBuilder.GetTypes()[0]);
   6:      }
   7:      return null;
   8:  }

The idea is to release Boxerp 0.2 within a month including all this stuff and these posts are part of the documentation.

Could not load type X from assembly Y

There are a bunch of reasons you can get this issue but one of the most difficult to find out is this:

  • You're adding methods to an assembly that is also installed in the GAC
  • You have a reference to that assembly in another project. The reference is not pointing to the GAC but to a file within a folder
  • So you can see the new methods in the objects explorer (VS), clicking the assembly twice, but you still get the exception in runtime

Tricky, isn't it?. Just remove the assembly from the GAC 🙂

Updating a WPF Treeview

I did post about this few days ago... but I made a big mistake!!!. Fortunately my post was online for 8 hours or so.

What I was wondering is how to create new items in a WPF Treeview that has been created using a HierarchicalDataTemplate with a collection of objects bound to it (ItemsSource={Binding} + DataContext), rather than using TreeViewItem objects programmatically. Becase if you have your treeview populated from C# using TreeViewItem objects then you can use the Header property to store the object itself and the Items property to append child objects but... how do you do otherwise?.

The solution is the ObservableCollection<T> class. I created a class containing a Parent property to keep track of the upper levels and an Items property which is an ObservableCollection. The I can access the selected object by casting the treeview.SelectedItem property and I can delete or add nodes to the Items collection so that the treeview gets refreshed.