Ruby 可以导入 .NET dll 吗? [英] Can Ruby import a .NET dll?

查看:17
本文介绍了Ruby 可以导入 .NET dll 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在必须使用 .NET dll 的项目中使用/学习 RoR 感兴趣.Ruby 是否能够导入 .NET dll?

I am interested in using/learning RoR in a project where I have to use a .NET dll. Is Ruby capable of importing a .NET dll?

推荐答案

虽然 IronRuby 可以缩短与 .NET dll 对话的工作(实际上根本没有代码),但它已被微软放弃,并且从来没有一个足够大的开源社区在那个事件之后继续下去.这些天我不推荐它

While IronRuby will make short work of talking to your .NET dll (it'll be literally no code at all), it was abandoned by microsoft, and it never got a large enough open source community to keep it going after that event. I wouldn't recommend it these days

关于 COM 解决方案,这实际上可能是一个很好的方法.

Regarding the COM solution, this may actually be a good way to go.

您不需要 RubyCOM 库 - 它允许其他 COM 对象调用 ruby​​ 代码.要从 ruby​​ 加载 COM 对象,您只需要 win32ole 库,它是 windows ruby​​ 标准库的一部分.

You don't need the RubyCOM library - that lets other COM objects call into ruby code. To load COM objects from ruby, you just need the win32ole library, which comes as part of the standard library on windows ruby.

您是否可以从 COM 加载 dll 将取决于 .NET dll 是否构建为Com Visible"..NET 框架定义了一个 ComVisibleAttribute,它可以应用于整个程序集或程序集中的特定类.如果它为整个程序集或任何类设置为 true,则 dll 将已经可以从 COM 调用而无需任何包装代码.

Whether or not you can load the dll from COM will depend if the .NET dll was built to be 'Com Visible'. The .NET framework defines a ComVisibleAttribute, which can be applied to either an entire assembly, or specific classes within an assembly. If it is set to true for either the whole assembly, or any classes, then the dll will already be callable from COM without any wrapper code.

这是我做过的测试.

创建一个新的 .NET dll 项目(类库).这是我使用的示例类:

Create a new .NET dll project (class library). Here's an example class I used:

using System;
using System.IO;

namespace ComLib
{
    public class LogWriter
    {
        public void WriteLine( string line )
        {
            using( var log = new StreamWriter( File.OpenWrite( @"c:log.file" ) ) )
            {
                log.WriteLine( line );
            }
        }
    }
}

现在,在visual studio 项目下,有一个名为Properties 的目录,其中包含AssemblyInfo.cs.在这个文件中,会有以下内容

Now, under the visual studio project, there is a directory called Properties which contains AssemblyInfo.cs. In this file, there will be the following

[assembly: ComVisible( false )]

把假改成真.如果您不希望程序集中的每个类都暴露给 COM,那么您可以在 AssemblyInfo.cs 中将其设置为 false,而是将其放在您想要的每个类之上暴露,像这样:

Change the false to true. If you don't want every class in the assembly exposed to COM, then you can leave it set to false in AssemblyInfo.cs and instead put it above each class you want to expose, like this:

[ComVisible( true )]
public class LogWriter ....

现在右键单击 dll 项目本身,然后从弹出菜单中选择属性".在部分列表中,选择 Build

Now right click on the dll project itself, and from the popup menu, select 'properties'. In the list of sections, choose Build

向下滚动,然后勾选注册 COM 互操作"复选框.现在,当您编译此 DLL 时,visual studio 将执行必要的操作以将 COM 信息加载到注册表中.请注意,如果您使用的是 vista,则需要以管理员身份运行 VS 才能正常工作.

Scroll down, and tick the 'Register for COM interop' checkbox. Now when you compile this DLL, visual studio will do the neccessary stuff to load the COM information into the registry. Note if you're on vista you need to run VS as an administrator for this to work.

既然已经完成了,重新编译你的 dll,然后创建一个新的 ruby​​ 文件.

Now that this is done, recompile your dll, and then create a new ruby file.

在这个 ruby​​ 文件中,执行以下操作:

In this ruby file, do this:

require 'win32ole'

lib = WIN32OLE.new('[Solution name].ComLib.LogWriter')
lib.WriteLine('calling .net from ruby via COM, hooray!')

其中 [Solution name] 将替换为您刚刚创建的解决方案的名称(默认:ClassLibrary1")

Where [Solution name] is to be replaced by the name of the solution you just created (default: "ClassLibrary1")

Ruby 那个 ruby​​ 文件,并且很快!您应该看到文本被写入 c:log.file.

Ruby that ruby file, and presto! you should see that the text gets written to c:log.file.

此解决方案的一个问题是它要求 .NET dll 已经是 Com Visible,否则,您可以重新编译它.如果这两种情况都不对,那么您可能需要考虑其他选项.

One problem with this solution is that it requires that the .NET dll is already Com Visible, or if it's not, you have the ability to recompile it. If neither of these things are true, then you may have to look at other options.

祝你好运!

这篇关于Ruby 可以导入 .NET dll 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆