Generates Blazor ↔ JavaScript strongly typed interop methods by parsing the JavaScript source and generating extension methods for IJSRuntime.
- Automatic type inference via Esprima AST parsing + JSDoc
@param {type}/@returns {type}comments - Arrow function & async function support
- Callback parameter detection → generates
Actiondelegate types - Date.now() →
numberdetection - Nested class generation for JS object hierarchies (e.g.
window.blazorInterop.modal.show→ nestedModalstatic class) - JSDoc descriptions extracted (
@paramdescriptions,@returnsdescriptions, summary text) - Config validation with diagnostics on errors (missing source, empty ObjectToInterop)
- JSON Schema for
BlazorInterop.jsoneditor autocomplete - Simplified output — max 3 methods per JS function (Void, Generic, Typed) instead of 18
Install via NuGet: GoLive.Generator.BlazorInterop
Add an AdditionalFile in your .csproj:
<ItemGroup>
<AdditionalFilesInclude="BlazorInterop.json" />
</ItemGroup>Add the settings file and configure:
{
"$schema": "./blazorinterop.schema.json",
"Files": [
{
"Output": "JSInterop.cs",
"Source": "wwwroot/blazorinterop.js",
"Namespace": "MyApp.Client",
"ObjectToInterop": "window.blazorInterop",
"Init": ["window={}"]
}
],
"InvokeVoidString": "await JSRuntime.InvokeVoidAsync(\"{0}\", {1});",
"InvokeString": "return await JSRuntime.InvokeAsync<T>(\"{0}\",{1});"
}| Key | Description |
|---|---|
Files | Array of file objects for interop generation |
Files[].Output | Generated C# filename |
Files[].Source | Path to JavaScript source file |
Files[].Namespace | C# namespace for generated code |
Files[].ObjectToInterop | JS object to interop (e.g. window.blazorInterop) |
Files[].Init | Init scripts executed before interop (e.g. ["window={}"]) |
InvokeVoidString | Template for void calls. {0} = function name, {1} = args |
InvokeString | Template for returning calls. {0} = function name, {1} = args |
Types are inferred automatically from JavaScript source:
- JSDoc annotations —
@param {string} name,@returns {boolean} - AST analysis — return statement expression types (literals,
Date.now(), template literals, comparisons, etc.) - Callback detection — params called as functions →
Actiontype - Fallback —
objectwhen ambiguous
Supported JSDoc types: string, number, boolean, datetime, object, array, void.
For JS:
window.blazorInterop={/** @param {string} dialogId @returns {boolean} */showModal: function(dialogId){returntrue;},modal: {/** @param {string} text */setText: function(text){ ... }}}Generates:
publicstaticclassJSInterop{publicstaticstring_showModal=>"showModal";publicstaticasyncTask<bool>showModalAsync(thisIJSRuntimeJSRuntime,stringdialogId,CancellationTokenct=default)=>awaitJSRuntime.InvokeAsync<bool>("showModal",newobject[]{dialogId},ct);publicstaticclassmodal{publicstaticstring_setText=>"modal.setText";publicstaticasyncTasksetTextVoidAsync(thisIJSRuntimeJSRuntime,stringtext,CancellationTokenct=default)=>awaitJSRuntime.InvokeVoidAsync("modal.setText",newobject[]{text},ct);}}| ID | Severity | Description |
|---|---|---|
| BI001 | Error | JS parse error (Esprima/Jint) |
| BI002 | Error | Config error (missing file, invalid JSON, empty ObjectToInterop) |
| BI003 | Warning | Type inference fallback (unknown type → object) |