11 releases

0.1.10 Sep 13, 2022
0.1.9 Sep 13, 2022
0.1.8 Sep 14, 2021

#5 in #mono

27 downloads per month

MIT license

335KB
10K SLoC

Contains (Windows DLL, 4KB) Test.dll

crates.io github/actions

mono-rs

Lightweight Mono wrapper

Example

using System;

namespace TestNS
{
    class TestClass
    {
        String TestField = "Test";

        String getTestField()
        {
            return "Call result: " + this.TestField;
        }
    }
}
mcs -target:library -out:Test.dll Test.cs
use mono_rs::{domain::Domain, method::Arguments, value::Value, MonoResult};

fn main() -> MonoResult<()> {
    println!("Creating Domain");
    let domain = Domain::new("Default")?;

    println!("Opening Assembly");
    let assembly = domain.open_assembly("Test.dll")?;

    println!("Getting Image");
    let image = assembly.get_image()?;

    println!("Getting Class");
    let class = image.get_class_by_name("TestNS", "TestClass")?;

    println!("Creating Object");
    let object = class.create_object()?;

    println!("Calling Constructor");
    object.construct(None)?;

    println!("Getting Field");
    let field = object.get_field_by_name("TestField")?;

    println!("Getting Field Value");
    let value_object = field.get_value_object()?;
    if let Value::Str(value_string) = value_object.try_into()? {
        println!("FieldValue: {}", value_string);
    } else {
        println!("FieldValue is not a string");
    }

    println!("Getting MethodDesc");
    let method = object.get_method_by_name("TestClass:getTestField()")?;

    println!("Calling Method");
    let result_object = method.invoke(Arguments::new())?;

    if let Value::Str(result_string) = result_object.try_into()? {
        println!("{}", result_string);
    } else {
        println!("Result is not a string");
    }

    Ok(())

bindgen -o src/bindings.rs src/bindings.h -- -I/usr/include/mono-2.0

Dependencies