Dynamic load of DLL compiled at runtime throwing exception

We recently implemented SmartAssembly for our software, and discovered that existing code to dynamically load a DLL compiled at runtime is throwing the exception at the bold line of code below: "An unexpected exception occurred while binding a dynamic operation."  If we disable obfuscation on the affected DLLs it does not fix the error.  We can disable embedding the DLLs referenced by this compiled DLL in the SmartAssembly designer, and the error goes away.  However, since those DLLs are referenced extensively in our project it results in significantly reduced obfuscation of our code since SmartAssembly will not build due to other embedded assemblies that reference them.  Is there a way to dynamically load a DLL at runtime and invoke a method without requiring this change?

System.Reflection.Assembly library = System.Reflection.Assembly.LoadFrom(dllPath);
ObservableCollection<ViewModel> viewModels = new ObservableCollection<ViewModel>();
Type type = library.GetType("TypeName");
if (null != type)
   dynamic c = Activator.CreateInstance(type);
   MethodInfo method = type.GetMethod("MethodName");
   viewModels = method?.Invoke(c, null);

Sign In or Register to comment.