ManualC (Inline C#)

ManualC C#

Write C# code directly inside your XML. Compile it at runtime and execute it seamlessly.


1. Introduction

Sometimes declarative XML isn't enough. You might need complex math, system API access, or advanced logic. ManualC allows you to embed standard C# methods right into your application.

speed

Runtime Compile

Code is compiled in memory when the app starts. No Visual Studio needed.

api

Full .NET Access

Use `System.IO`, `System.Net`, `System.Math` and more.

integration_instructions

Easy Integration

Call C# methods from XML handlers and get return values back.


2. Syntax

Use the <ManualC> tag inside <Logic>. Always wrap your code in <![CDATA[ ... ]]> to avoid XML parsing errors.

App.xml
<Logic>
    <ManualC Id="MyMath">
        <![CDATA[
        using System;

        public static class MyMath 
        {
            public static double CalculateArea(double radius) 
            {
                return Math.PI * radius * radius;
            }
            
            public static string ReverseString(string input)
            {
                char[] charArray = input.ToCharArray();
                Array.Reverse(charArray);
                return new string(charArray);
            }
        }
        ]]>
    </ManualC>
</Logic>
info
Requirement: Your C# methods must be public static. The class name inside CDATA doesn't matter, but using `Id` is best practice.

3. Calling C# from XML

Use the <CallManualC> command to execute your methods. You can pass parameters and store the result in a variable.

<Handler Name="OnCalculate">
    <!-- Calculate Area -->
    <CallManualC 
        Id="MyMath" 
        Method="CalculateArea" 
        Params="5.5" 
        ToState="areaResult"/>
        
    <Set Target="lblResult" Property="Text" Value="Area: {areaResult}"/>
    
    <!-- Reverse String -->
    <CallManualC 
        Id="MyMath" 
        Method="ReverseString" 
        Params="{userInput}" 
        ToState="reversed"/>
</Handler>
AttributeDescription
IdThe ID of the ManualC block to call.
MethodThe exact name of the C# static method.
ParamsComma-separated arguments (e.g., "10,hello,true").
ToState(Optional) Variable name to store the return value.

4. Importing External Files

Instead of writing C# inside XML, you can import external `.cs` files. This keeps your XML clean.

<Logic>
    <!-- Import Helper.cs -->
    <ImportManualC Source="scripts/Helper.cs" Id="Helper"/>
    
    <Handler Name="UseHelper">
        <CallManualC Id="Helper" Method="DoWork"/>
    </Handler>
</Logic>

scripts/Helper.cs:

using System;
using System.Windows;

public static class Helper 
{
    public static void DoWork() 
    {
        MessageBox.Show("Working from external file!");
    }
}

5. Real-World Examples

Example 1: Dynamic Image Generation

Generate a Bitmap in memory and display it in an Image control.

<ManualC Id="ImgGen">
    <![CDATA[
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    
    public static ImageSource GenerateGradient()
    {
        RenderTargetBitmap bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
        // ... (Drawing logic)
        return bmp;
    }
    ]]>
</ManualC>

<Handler Name="ShowImage">
    <CallManualC Id="ImgGen" Method="GenerateGradient" ToControl="myImage"/>
</Handler>

Example 2: System Info

Get detailed OS information that isn't available via standard XML commands.

<ManualC Id="SysInfo">
    <![CDATA[
    using System;
    public static string GetOS() 
    {
        return Environment.OSVersion.ToString() + " " + (Environment.Is64BitOperatingSystem ? "64-bit" : "32-bit");
    }
    ]]>
</ManualC>

<Handler Name="LoadInfo">
    <CallManualC Id="SysInfo" Method="GetOS" ToState="osName"/>
    <Set Target="lblOS" Property="Text" Value="{osName}"/>
</Handler>