mono-rs

Crates.iomono-rs
lib.rsmono-rs
version0.1.10
sourcesrc
created_at2021-09-05 14:19:24.501426
updated_at2022-09-13 18:27:32.925951
descriptionLightweight wrapper around mono.
homepage
repositoryhttps://github.com/b4rti/mono-rs
max_upload_size
id447138
size335,086
Bartosz 'b4rti' Gorol (b4rti)

documentation

README

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
Commit count: 14

cargo fmt