Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for dll #5

Closed
robertdj opened this issue Sep 21, 2020 · 2 comments
Closed

Example for dll #5

robertdj opened this issue Sep 21, 2020 · 2 comments

Comments

@robertdj
Copy link

Hi

This package looks very interesting!
Can you provide an example on how to call methods from a DLL? If I have a Class Library with a single file:

namespace JuliaTest
{
    public class Foo
    {
        public double Bar(double x)
        {
            return 2 * x;
        }
    }
}

How do I call Bar from the generated dll? (Please correct me if this is the wrong way to generate the dll :-))

Thanks.

@azurefx
Copy link
Owner

azurefx commented Sep 21, 2020

Sure.

PS C:\Users\Azure> Add-Type -Namespace JuliaTest -Name Foo -MemberDefinition @'
>> public double Bar(double x)
>> {
>>     return 2 * x;
>> }
>> '@ -OutputAssembly Example.dll

For convenience, I generated the DLL by PowerShell, so the assembly name looks wierd (di0ato2a.2tx in this case, and I don't know how to change this...).

julia> using DotNET

julia> asm=T"System.Reflection.Assembly".LoadFrom("Example.dll")
System.Reflection.RuntimeAssembly("di0ato2a.2tx, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")

The assembly is loaded with Assembly.LoadFrom method, just like what we do in C#.

Then we have two ways to reference our JuliaTest.Foo class:

julia> Foo=asm.GetType("JuliaTest.Foo")
JuliaTest.Foo

julia> T"JuliaTest.Foo, di0ato2a.2tx"
JuliaTest.Foo

Since Bar is not a static method, we need an instance of Foo:

julia> foo=Foo.new()
JuliaTest.Foo("JuliaTest.Foo")

julia> foo.Bar(3.14)
6.28

@robertdj
Copy link
Author

Thanks a lot for your fast reply :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants