C# 仅使用代码添加引用(没有 IDE“添加引用”功能)

发布于 2024-08-04 03:03:29 字数 607 浏览 7 评论 0原文

我正在为一个程序编写一个插件,我想将我的代码放入 DLL 中,这样我就可以自由地共享该插件,而无需暴露(泄露)我的代码。

这是我可以访问的基本结构:

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

然后我只需引用我的代码所在的 .cs 文件,程序就会“吃掉”这个插件。现在,我在那里放置了几个逻辑,主要由不直接与“Initialize()”绑定的函数组成,仅在启动整个系统的 doWork() 函数上。

现在,我想将所有代码放入 DLL 中,然后从 Initialize()、myDll.doWork() (或类似的东西)内部调用。

PS:这个 dll 将是一个已编译的 C# 库(可以称为动态程序集导入吗?它不会真正是动态的,因为无论如何它都会在执行前进行编译,对吧?)

PS2:这样我还可以添加自定义资源,例如表单、图像等没有太大困难吧?

PS3:有没有免费的工具来保护此类DLL中的代码? (即防止被轻易地重新设计)

提前致谢=)

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

Here is the basic structure i have access to :

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

Then i just reference the .cs file my code is at and the program "eats" up this Plugin. Now, i have put several logic in there, consisting mostly of functions that arent tied directly to "Initialize()", only on the doWork() function, that starts the whole system.

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

PS: This dll would ofc be a compiled C# library (could it be called dynamic assembly import? it wouldnt really be dynamic since it would be compiled pre-execution anyways, right?)

PS2: This way I can also add custom resources like forms, images and such without much difficulty right?

PS3: Is there a free tool for protecting the code inside such DLL? (ie protect from beeing easily re engineered)

Thanks in advance =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掩饰不了的爱 2024-08-11 03:03:29

找到了我正在寻找的东西,这里是:

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);            
}

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);            
}
分開簡單 2024-08-11 03:03:29

最好的选择是使用框架来创建可扩展应用程序:

如果你想手动执行,可以使用 Assembly.Load*() 加载程序集的方法在运行时。您可以在程序集中搜索实现特定接口的类型,然后使用 Activator.CreateInstance。程序集可以单独编译,您只需引用包含应用程序和插件共享的接口的程序集即可。

对于混淆,有一些混淆工具可用。

The best option would be to use a framework for creating extensible applications:

If you want to do it manually, you can use the Assembly.Load*() methods to load an assembly at runtime. You can search the assembly for types that implement a specific interface and then create instances using Activator.CreateInstance. The assemblies can be compiled separately, you just have to reference an assembly that contains the interface shared by application and plugin.

For obfuscation, there are a couple of obfuscation tools available.

囍孤女 2024-08-11 03:03:29

使用静态 Assembly.Load() / Assembly.LoadFile() / Assembly.LoadFrom() 方法动态加载程序集。

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

回忆那么伤 2024-08-11 03:03:29

仅供参考,请记住使用 Red-Gate 的 .Net Reflector 等工具来检查您的DLL 以确保它被正确混淆。这个免费的可视化工具可让您像用户一样查看 DLL,以便您知道您的代码是否暴露

替代文本 http://img149.imageshack.us/img149/2025/screenshotfullscreen.gif

.Net 附带 Dotfuscator Community Edition 可用于混淆您的代码。无需编码,请参阅他们的用户指南来了解 GUI 的使用方法如果您需要帮助。

FYI, remember to use a tool like Red-Gate's .Net Reflector to inspect your DLL to make sure it is properly obfuscated. This free, visual tool lets you see the DLL as your users will see it so you'll know if your code is exposed

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

.Net comes with Dotfuscator Community Edition which can be used to obfuscate your code. No coding is required, see their user guide to learn your way around the GUI if you need help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文