Files
sbox-public/engine/Sandbox.Bind/Proxy/MethodProxy.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

49 lines
1012 B
C#

namespace Sandbox.Bind;
/// <summary>
/// A proxy where set and get are done via functions
/// </summary>
internal class MethodProxy<T> : Proxy
{
public Func<T> Read { get; set; }
public Action<T> Write { get; set; }
public MethodProxy( Func<T> read, Action<T> write )
{
Name = "[MethodBind]";
Read = read;
Write = write;
}
public MethodProxy( object target, Func<T> read, Action<T> write )
{
Target = new WeakReference<object>( target );
Name = "[MethodBind]";
Read = read;
Write = write;
}
public override object Value
{
get => Read.Invoke();
set
{
if ( !CanWrite ) throw new System.NotSupportedException( "Binding is read only" );
if ( !Translation.TryConvert( value, typeof( T ), out var targetValue ) )
return;
Write( (T)targetValue );
}
}
public override bool CanRead => Read != null;
public override bool CanWrite => Write != null;
public override bool IsValid => CanRead || CanWrite;
public override string ToString() => $"[MethodBind]";
}