1

I am loading my assemblies at run time and I am also using Instance Activator to get the code at the runtime. Following code will summerize what I am doing:

dynamic powerTool;
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(
    @"C:\Users\c_desaik\Desktop\PowerTool.exe");
Type type = assembly.GetType("PowerTool.Automation");
powerTool = Activator.CreateInstance(type);    

Now, as you have noticed, Powertool has been declared as dynamic because I do not want my code to fail at Compile time and I want it to resolve all the operations at the Runtime.

While I do that the code fails to execute down the line with the following error:

An unexpected exception occurred while binding a dynamic operation

Now I thought that the entire concept behind the dynamic keyword was to be abl eto resove all the members and operation at the run time. How can I resolve this error?

devuxer
  • 41,681
  • 47
  • 180
  • 292
Lost
  • 12,007
  • 32
  • 121
  • 193
  • Related question: http://stackoverflow.com/questions/465488/can-i-load-a-net-assembly-at-runtime-and-instantiate-a-type-knowing-only-the-na – devuxer Aug 22 '13 at 23:28
  • Is the Automation type public? – Thomas Levesque Aug 22 '13 at 23:33
  • Yes, Automation is a public type. As a matter of fact I am getting that type using my code but further down it breaks on something like:` powerTool.WindowState = minimized ? PowerTool.WindowState.Minimized : PowerTool.WindowState.Normal;` wiht the error that I posted – Lost Aug 22 '13 at 23:38
  • The thing that I found was that the dynamically declared object Powertool does not have any problem in accessing the methods from API nor does it have any problem while values are assigned to the types which are primitive. It only creates problem when we try to access it's property which is native to it's assembly for example ` result = powerTool.OpenApplication(iniFile, waitLimit); powerTool.BatterySize = 10; powerTool.WindowState = minimized ? PowerTool.WindowState.Minimized : PowerTool.WindowState.Normal;` only fails when we try assingn powerTool.WindowState – Lost Aug 23 '13 at 00:25
  • Is there an inner exception? Could you upload PowerTool.exe somewhere so we can re-create the problem? – Hack Aug 23 '13 at 00:25
  • Inner exception for this error is null. I have uploaded binary for your reference http://www.speedyshare.com/AaSRh/PowerTool.exe – Lost Aug 23 '13 at 00:41
  • I have kind of an idea why is this happening. I am just loading one class of an assembly NOT all the types. So when my code tries to refer the native types of that assembly it fails. Need to figure out how to make my class aware of every type in that assembly – Lost Aug 23 '13 at 00:46

0 Answers0