2
Answers

To load DLL on a run time or not

Hello,
I need advice about direction in expanding existing program.
I want to enable to users to develop  their own external modules or methods for program,
that would be called and executed from main (existing) program, by selecting modules from
some sort of drop down menu. For this by my knowledge there are two options :
 
1. Enable user to write its own DLL. Load it to program during run time and execute it.
2. Enable to user to write its own program  that receves execute call from main program.
 
In both cases main program passes some parameters to external methods, and accepts result.
 
What would be the better solution, considereing speed and safety.
 
All the best,
Željko Peric
Answers (2)
1
Jefferson S. Motta

Jefferson S. Motta

87 21.2k 872.1k 6y
Some day I'll do it too.
 
And I want to make with Roslyn.
 
You can create a base method return to get values from scripts:
 
 And execute scripting looks like that:
  1. Console.WriteLine(CSharpScriptEngine.Execute("new ScriptedClass().HelloWorld"));  
  2.         Console.ReadKey();  
 Look to:
 
 https://blog.jayway.com/2015/05/09/using-roslyn-to-build-a-simple-c-interactive-script-engine/
 
https://www.google.com.br/search?q=create+scriptings+with+roslyn&ie=&oe=
 
Suggestion: You 'll need take care about the code that your end user will put, could be dangerous.
 
Good look. 
 
Accepted
0
Željko Perić

Željko Perić

396 4k 241.9k 6y
Thanks to Jefferson for answer,
 
I have used  this  :
User selects .dll file from drop down list.
File is then loaded to DLL variable trough Assembly.LoadFile(File path) method, namespace System.Reflection.
Then get all types from DLL into array  t.
Then create variable c as instance of t[0], that would be first class inside DLL.
This is according to demands of my program. If there are more than one classes inside DLL there is need to know which one is to be used, and adapt program accordingly.
Get wanted method from selected class by its name, in this case it is method Pack_Elements with many parameters, and one returning value type of DataGridView.
Invoke selected method.
In this case DataGridView Table_List is populated with returned values inside DataGridView from called method.
 
  1. if(External_Algorithms.Checked==true)  
  2. {  
  3.   
  4.     var DLL = Assembly.LoadFile(External_Algorithms_File_Paths[External_Algorithms_List.SelectedIndex]);  
  5.       
  6.     Type [] t = DLL.GetTypes();  
  7.     var c = Activator.CreateInstance(t[0]);  
  8.     var method = t[0].GetMethod("Pack_Elements");  
  9.     Table_List = (DataGridView)method.Invoke(c, new object[{table_width*Multiply,
  10.                                                 table_height*Multiply,  
  11.                                                 saw_blade_tickness,  
  12.                                                 cleaning_left_table_edge*Multiply,  
  13.                                                 cleaning_right_table_edge*Multiply,  
  14.                                                 cleaning_up_table_edge*Multiply,  
  15.                                                 cleaning_down_table_edge*Multiply,  
  16.                                                 horizontal,  
  17.                                                 vertical,  
  18.                                                 Element_List_Copy,  
  19.                                                 sort_by_area});  

 This woks fine, but there is problem with security and possibility that user with its own error inside program code turns down the main application, so it seems that second option is better solution.
 
All the best,
Željko Peric