C # add link using only code (no IDE "Add link" features)

I am writing a plugin for a program, and I want to put my code in a DLL so that I can freely share the plugin without exposing (giving) my code.

Here is the basic structure that I have access to:

using System; public class Plugin { public void Initialize() { //do stuff here doWork(); } } 

Then I just refer to the .cs file where my code is located, and the program "eats" this plugin. Now I put a few logics, consisting mainly of functions that arent attached directly to "Initialize ()", only to the doWork () function, which starts the whole system.

Now I want to put all my code in a DLL, and then just call Initialize (), myDll.doWork () (or something like that) from the inside.

PS: This dll should be a compiled C # library (could it be called a dynamic assembly? Would it not be dynamic, since it would have been compiled in advance, anyway?)

PS2: So I can also add user resources like forms, images, etc., without too much difficulty?

PS3: Is there a free tool for protecting code inside such a DLL? (i.e. protect against easy bending)

Thanks in advance =)

+4
source share
4 answers

Found exactly what I was looking for, here it is:

 using System.Reflection; using System.IO; try { Assembly a = null; a = Assembly.LoadFrom(Application.StartupPath startupPath + "MyAssembly.dll"); Type classType = a.GetType("MyNamespace.MyClass"); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod("MyMethod"); mi.Invoke(obj, null); } catch (Exception e) { AddLog(e.Message); } 
+7
source

A better option would be to use a framework to create extensible applications:

If you want to do this manually, you can use Assembly.Load * () methods to load the assembly at run time. You can search for an assembly for types that implement a specific interface, and then create instances using Activator.CreateInstance . Assemblies can be compiled separately, you just need to reference the assembly containing the interface shared by the application and the plugin.

For obfuscation, there are a couple of obfuscation tools .

+3
source

Use the static methods Assembly.Load () / Assembly.LoadFile () / Assembly.LoadFrom () to dynamically load assemblies.

0
source

FYI, be sure to use a tool like Red-Gate.Net Reflector to check your DLL to make sure it is confused correctly. This free visual tool allows you to see the DLL, as your users will see it so that you know if your code is open.

alt text http://img149.imageshack.us/img149/2025/screenshotfullscreen.gif

.Net comes with Dotfuscator Community Edition , which you can use to obfuscate your code. No coding is required, see the User Guide for your way around the GUI if you need help.

0
source

All Articles