可红宝石导入一个.NET的DLL? [英] Can Ruby import a .NET dll?

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

问题描述

我很感兴趣的一个项目,我必须使用.NET的DLL使用/学习的回报率。是红宝石能够导入一个.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短期工作(这将是字面上没有code在所有),它被抛弃微软,它从来没有得到足够大的开源社区保持这一事件发生后下去。我不会推荐它这几天

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对象调用到红宝石code。在Ruby中加载COM对象,你只需要在 WIN32OLE 库,它是作为Windows红宝石的标准库的一部分。

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可见。 在.NET框架限定了 ComVisibleAttribute ,它可以应用到无论是整个组件,或特定类的组件中。如果设置为true,无论是整机组装,或任何类,则该dll将已经从COM调用没有任何包装code。

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.

下面是一个测试,我做到了。

Here's a test I did.

创建一个新的.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项目下,有一个名为属性包含的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程序集,那么你可以把它留在设置为false 的AssemblyInfo.cs ,而是把它要公开每一个类,这样上面的:

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项目本身,并从弹出菜单中选择属性。在部分的列表中,选择构建

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

向下滚动,并勾选为COM Interop注册复选框。现在,当你编译这个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!')

其中[解决方案名称]是由刚刚创建的解决方案的名称所取代(默认:ClassLibrary1的)

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

红宝石的红宝石文件,preSTO!你应该看到文本被写入 C:\ log.file

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

与此解决方案的一个问题是,它要求.NET的DLL已经的COM可见,如果不是的话,你必须重新编译的能力。如果没有这些东西都是真实的,那么你可能需要看看其他的选择。

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.

祝你好运!

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

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