Skip to content
/ Ionad Public

Replaces static method calls.

Notifications You must be signed in to change notification settings

Fody/Ionad

Repository files navigation

Ionad.Fody

Chat on Gitter NuGet Status

Ionad replaces static types with your own.

See Milestones for release notes.

This is an add-in for Fody

It is expected that all developers using Fody become a Patron on OpenCollective. See Licensing/Patron FAQ for more information.

Usage

See also Fody usage.

NuGet installation

Install the Ionad.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package Ionad.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Your Code

[StaticReplacement(typeof(DateTime))]
public static class DateTimeSubstitute
{
    public static IDateTime Current { get; set; }

    public static DateTime Now { get { return Current.Now; } }
}

public void SomeMethod()
{
    var time = DateTime.Now;
    // ...
}

What gets compiled

public void SomeMethod()
{
    var time = DateTimeSubstitute.Now;
    // ...
}

You can also reference methods within the original static class to add defaults to optional parameters. For example:

[StaticReplacement(typeof(System.Reactive.Linq.Observable))]
public static class QueryableSubstitute
{
    public static IObservable<IList<TSource>> Delay<TSource>(this IObservable<TSource> source, TimeSpan timeSpan)
    {
        return Linq.Delay<TSource>(source, timeSpan, RxApp.TaskpoolScheduler);
    }
}

public async Task<int> SomeMethod()
{
    return await Observable.Return(1).Delay(TimeSpan.FromSeconds(1)).ToTask();
}

What gets compiled

public async Task<int> SomeMethod()
{
    return await Observable.Return(1).Delay(TimeSpan.FromSeconds(1), RxApp.TaskpoolScheduler).ToTask();
}

Icon

Interchange designed by Laurent Patain from The Noun Project.