C# 在运行时调用静态方法而无需构建时引用?

发布于 2024-08-03 23:47:27 字数 247 浏览 6 评论 0原文

我正在用 C# .net (2.0) 编写一个系统。它具有可插入模块的架构。无需重建基本模块即可将组件添加到系统中。为了连接到新模块,我希望尝试按名称调用其他模块中的静态方法。我不希望在构建时以任何方式引用被调用的模块。

当我从 .dll 文件的路径开始编写非托管代码时,我会使用 LoadLibrary() 将 .dll 放入内存,然后使用 get GetProcAddress() 获取指向我希望调用的函数的指针。如何在 C# / .NET 中实现相同的结果。

I am writing a system in C# .net (2.0). It has a pluggable module sort of architecture. Assemblies can be added to the system without rebuilding the base modules. To make a connection to the new module, I wish to attempt to call a static method in some other module by name. I do not want the called module to be referenced in any manner at build time.

Back when I was writing unmanaged code starting from the path to the .dll file I would use LoadLibrary() to get the .dll into memory then use get GetProcAddress() get a pointer to the function I wished to call. How do I achieve the same result in C# / .NET.

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

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

发布评论

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

评论(2

沫雨熙 2024-08-10 23:47:27

使用 Assembly.LoadFrom(...) 加载程序集后,您可以通过名称获取类型并获取任何静态方法:

Type t = Type.GetType(className);

// get the method
MethodInfo method = t.GetMethod("MyStaticMethod",BindingFlags.Public|BindingFlags.Static);

Then you call the method:

method.Invoke(null,null); // assuming it doesn't take parameters

After the assembly is loaded using Assembly.LoadFrom(...), you can get the type by name and get any static method:

Type t = Type.GetType(className);

// get the method
MethodInfo method = t.GetMethod("MyStaticMethod",BindingFlags.Public|BindingFlags.Static);

Then you call the method:

method.Invoke(null,null); // assuming it doesn't take parameters
妄司 2024-08-10 23:47:27

这是一个示例:

        string assmSpec = "";  // OS PathName to assembly name...
        if (!File.Exists(assmSpec))
            throw new DataImportException(string.Format(
                "Assembly [{0}] cannot be located.", assmSpec));
        // -------------------------------------------
        Assembly dA;
        try { dA = Assembly.LoadFrom(assmSpec); }
        catch(FileNotFoundException nfX)
        { throw new DataImportException(string.Format(
            "Assembly [{0}] cannot be located.", assmSpec), 
            nfX); }
        // -------------------------------------------
        // Now here you have to instantiate the class 
        // in the assembly by a string classname
        IImportData iImp = (IImportData)dA.CreateInstance
                          ([Some string value for class Name]);
        if (iImp == null)
            throw new DataImportException(
                string.Format("Unable to instantiate {0} from {1}",
                    dataImporter.ClassName, dataImporter.AssemblyName));
        // -------------------------------------------
       iImp.Process();  // Here you call method on interface that the class implements

here's a sample:

        string assmSpec = "";  // OS PathName to assembly name...
        if (!File.Exists(assmSpec))
            throw new DataImportException(string.Format(
                "Assembly [{0}] cannot be located.", assmSpec));
        // -------------------------------------------
        Assembly dA;
        try { dA = Assembly.LoadFrom(assmSpec); }
        catch(FileNotFoundException nfX)
        { throw new DataImportException(string.Format(
            "Assembly [{0}] cannot be located.", assmSpec), 
            nfX); }
        // -------------------------------------------
        // Now here you have to instantiate the class 
        // in the assembly by a string classname
        IImportData iImp = (IImportData)dA.CreateInstance
                          ([Some string value for class Name]);
        if (iImp == null)
            throw new DataImportException(
                string.Format("Unable to instantiate {0} from {1}",
                    dataImporter.ClassName, dataImporter.AssemblyName));
        // -------------------------------------------
       iImp.Process();  // Here you call method on interface that the class implements
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文