menu_bookDocumentation

CodeGenerator: Compile-Time Method and Property Wrapping via Attributes

calendar_today May 20, 2026 schedule ~1 min read person PatrickJr verified 50

The [CodeGenerator] attribute in s&box lets you create custom attributes that wrap methods and properties at compile time. This is the mechanism behind [Sync] properties and [Rpc.Broadcast] — you can use it to build your own systems.

Wrapping Methods

Create an attribute that intercepts method calls:

CSHARP
[AttributeUsage( AttributeTargets.Method )]
[CodeGenerator( CodeGeneratorFlags.WrapMethod | CodeGeneratorFlags.Instance, "OnMethodInvoked" )]
public class MyRPC : Attribute { }

public class MyObject
{
    [MyRPC]
    public void SendMessage( string message )
    {
        Log.Info( message );
    }

    internal void OnMethodInvoked( WrappedMethod m, params object[] args )
    {
        // Do something before/instead of the original call
        // e.g., send over network
        m.Resume(); // Call the original method
    }
}

Wrapping Properties (Get/Set)

Intercept property reads and writes:

CSHARP
[AttributeUsage( AttributeTargets.Property )]
[CodeGenerator( CodeGeneratorFlags.WrapPropertySet | CodeGeneratorFlags.Instance, "OnWrapSet" )]
[CodeGenerator( CodeGeneratorFlags.WrapPropertyGet | CodeGeneratorFlags.Instance, "OnWrapGet" )]
public class NetVar : Attribute { }

public class MyObject
{
    [NetVar] public string Name { get; set; }

    internal void OnWrapSet<T>( WrappedPropertySet<T> p )
    {
        // Custom logic before setting
        p.Setter( p.Value ); // Call original setter
    }

    internal T OnWrapGet<T>( WrappedPropertyGet<T> p )
    {
        // Return custom value or original
        return p.Value;
    }
}

CodeGeneratorFlags

FlagEffect
WrapMethodIntercept method calls
WrapPropertySetIntercept property setter
WrapPropertyGetIntercept property getter
InstanceApply to instance members
StaticApply to static members (callback must also be static)
Flags can be combined. An attribute can have multiple [CodeGenerator] decorations for different scenarios.

Callback Resolution

Was this helpful?