So there's a new year and I thought of "what can I post to help developers out there in the wild". And here's my solution: Some useful helpers bringing me through each day of developing my final project. It's nothing very special but some very cool extensions for extending factory methods and stuff.

public static T _<T>(this object target, Action<T> a)
{
    if (!(target is T))
        throw new InvalidCastException();
    T t = (T)target;
    a(t);
    return t;
}

public static T _<T>(this T t, Action<T> a)
{
    a(t);
    return t;
}

You're using it just like SomeFactory.Create<SomeType>()._(variable => { });. For a proof of usage, here's my statemachine with generic creation of objects without being able to expose some variables.

StateMachine.SetState<EnterBuildingState>()._(s =>
{
    s.TargetBuilding = (BuildingEntity)settler.Workplace.Entity;
});

If you encounter any thing related to maths and stuff, feel free to visit Sean Eron Andersons Bit Twiddling Hacks.

Previous Post Next Post