c# - Loading same Assembly with different versions and instnatiating multiple .xaml instances -


this follow-up question here.

i want load same assembly in different versions , create multiple instances of types these assemblies.

here have:

i have assembly asm.dll whos version (within assemblyinfo.cs) set 1.0.0.0.

then, modify code , increment version 2.0.0.0 , build again asm.dll.

now, have dir1/asm.dll , dir2/asm.dll.

here do:

assembly = assembly.loadfile(assemblyfile);  var types = assembly.gettypes();  type type = types.first<type>(t => t.name.equals(backbonememberclass + "editor"));  myobject myobject = (myobject)assembly.createinstance("theclassiwanttoinstantiate", false, bindingflags.createinstance, null, new object[] { }, null, null); 

here problem:

the above works fine if use "dir1/asm.dll" assemblyfile: call assembly.createinstance(...) returns me requested instance.

if use again wit "dir2/asm.dll" still works fine. assembly.createinstance returns correct instance.

but, if again want create instance of object did create before (through calling assembly.createinstance) getting following exception(s):

a first chance exception of type 'system.exception' occurred in presentationframework.dll first chance exception of type 'system.reflection.targetinvocationexception' occurred in mscorlib.dll 

the class loading .xaml wpf usercontrol , stacktrace of exception says initializecomponent() within .xaml.cs file throwing exception because cannot find .baml file.

after week suffering , laboring issue, found both reason problem , solution.

the problem lies within auto-generated *.g.i.cs file, called initializecomponent() method of usercontrol, seen following:

enter image description here

this file generates string (a resource locator) expresses path xaml-component, seen following:

enter image description here

now, if have multiple versions of same assembly , both versions include same xaml-file, wpf not know xaml-file instantiate, because resource locator references name of assembly not version.

this results in targetinvocationexception, saying

{"the component 'mynamespace.myusercontrol' not have resource identified uri '/myassembly;comoponent/myusercontrol.xaml'"}

as follows:

enter image description here

the simple (but not obvious) solution add version of assembly resource locator. can achieved modifying build-file of project adding <assemblyversion>-tag follows:

enter image description here

credits go to:


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -