#
Reflection Support
h5 includes runtime support for standard .NET reflection capabilities, allowing you to inspect types, members, and attributes at runtime. This is achieved through generated metadata that accompanies your compiled JavaScript code.
#
How It Works
By default, h5 generates metadata for all public types in your project. This metadata includes information about constructors, methods, properties, fields, events, and attributes.
The metadata is stored in a separate .meta.js file (if configured) or embedded within the main JavaScript file. When your application starts, this metadata is loaded and made available through the System.Reflection API.
#
Usage
You can use standard reflection APIs:
using System;
using System.Reflection;
public class Person
{
public string Name { get; set; }
public void SayHello() { }
}
public class Program
{
public static void Main()
{
Type personType = typeof(Person);
// Get properties
foreach (var prop in personType.GetProperties())
{
Console.WriteLine("Property: " + prop.Name);
}
// Get methods
foreach (var method in personType.GetMethods())
{
Console.WriteLine("Method: " + method.Name);
}
}
}
#
The [Reflectable] Attribute
To control which types have metadata generated (and thus are reflectable), you can use the [Reflectable] attribute.
- Assembly Level: Apply to the assembly to set default behavior.
- Class Level: Apply to specific classes.
- Member Level: Apply to specific members.
[assembly: Reflectable(false)] // Disable by default
[Reflectable(true)] // Enable for this class
public class MyReflectableClass
{
public int Id { get; set; }
[Reflectable(false)] // Exclude this property
public string Secret { get; set; }
}
#
Configuring Metadata Generation
You can configure reflection settings in h5.json:
{
"reflection": {
"disabled": false,
"filter": "MyNamespace.*",
"target": "File"
}
}
disabled: Set totrueto completely disable reflection support (reduces bundle size).filter: A wildcard pattern to include/exclude types.target:File(default) orInline.
#
Performance Impact
Generating metadata increases the size of your JavaScript bundle. If your application does not use reflection extensively, consider disabling it or using [Reflectable] to only include necessary types.