load_module/load_module.rs
1//! Loads an assembly and prints its types and constant field values, showcasing
2//! the typed APIs: `use hao::prelude::*;`, `TypeDef::kind` (the C# keyword
3//! instead of a hand-rolled classification), and `Constant::decode` ->
4//! `ConstantValue`.
5//!
6//! Usage: `cargo run --example load_module -- <assembly.dll>`
7
8use hao::prelude::*;
9
10fn main() {
11 let path = std::env::args()
12 .nth(1)
13 .expect("usage: load_module <assembly>");
14 let module = Module::from_path(&path).unwrap();
15 println!("loaded {}", module.name());
16
17 for ty in module.all_types() {
18 let ty = ty.get();
19 println!("{} {}.{}", ty.kind().keyword(), ty.namespace, ty.name);
20
21 // Decode compile-time constants (enum members and `const` fields) into a
22 // typed value, rather than hand-reading the raw blob.
23 for field in &ty.fields {
24 let field = field.get();
25 if let Some(value) = field.constant.as_ref().and_then(Constant::decode) {
26 println!(" {} = {value}", field.name);
27 }
28 }
29 }
30}