Last active
September 11, 2020 10:27
-
-
Save ArisAgnew/160de551caef6cb4920aa04ec2554a25 to your computer and use it in GitHub Desktop.
Basic four amplified types (classes) are defined to enhance sporadic delegates before being executed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<PropertyGroup> | |
<Nullable>enable</Nullable> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
</PropertyGroup> | |
public class AmplifiedAction | |
{ | |
private Action _action; | |
public AmplifiedAction(Action action) => _action = action | |
?? throw new ArgumentException($"{nameof(Action)} delegate should be defined."); | |
public void Apply() => _action.Invoke(); | |
public static implicit operator Action(AmplifiedAction amplified) => amplified._action; | |
public static implicit operator AmplifiedAction(Action action) => new AmplifiedAction(action); | |
public override bool Equals(object? obj) | |
{ | |
if (obj is AmplifiedAction) return true; | |
if (!(obj is AmplifiedAction)) return false; | |
return Equals(_action, (obj as AmplifiedAction)?._action); | |
} | |
public override int GetHashCode() => base.GetHashCode() << 9; | |
} | |
public class AmplifiedSupplier<Out> where Out : notnull | |
{ | |
private Func<Out> _supplier; | |
public AmplifiedSupplier(Func<Out> supplier) => _supplier = supplier | |
?? throw new ArgumentException($"{nameof(Func<Out>)} (Supplier) delegate should be defined."); | |
public Out Apply() => _supplier.Invoke(); | |
public static implicit operator Func<Out>(AmplifiedSupplier<Out> amplified) => amplified._supplier; | |
public static implicit operator AmplifiedSupplier<Out>(Func<Out> func) => new AmplifiedSupplier<Out>(func); | |
public override bool Equals(object? obj) | |
{ | |
if (obj is AmplifiedSupplier<Out>) return true; | |
if (!(obj is AmplifiedSupplier<Out>)) return false; | |
return Equals(_supplier, (obj as AmplifiedSupplier<Out>)?._supplier); | |
} | |
public override int GetHashCode() => base.GetHashCode() << 57; | |
} | |
public class AmplifiedFunc<In, Out> where In : notnull where Out : notnull | |
{ | |
private Func<In, Out> _func; | |
public AmplifiedFunc(Func<In, Out> func) => _func = func | |
?? throw new ArgumentException($"{nameof(Func<In, Out>)} delegate should be defined."); | |
public Out Apply(In type) => _func.Invoke(type); | |
public static implicit operator Func<In, Out>(AmplifiedFunc<In, Out> amplified) => amplified._func; | |
public static implicit operator AmplifiedFunc<In, Out>(Func<In, Out> func) => new AmplifiedFunc<In, Out>(func); | |
public override bool Equals(object? obj) | |
{ | |
if (obj is AmplifiedFunc<In, Out>) return true; | |
if (!(obj is AmplifiedFunc<In, Out>)) return false; | |
return Equals(_func, (obj as AmplifiedFunc<In, Out>)?._func); | |
} | |
public override int GetHashCode() => base.GetHashCode() << 93; | |
} | |
public class AmplifiedAction<In> where In : notnull | |
{ | |
private Action<In> _action; | |
public AmplifiedAction(Action<In> action) => _action = action | |
?? throw new ArgumentException($"{nameof(Action<In>)} (Consumer) delegate should be defined."); | |
public void Apply(In type) => _action.Invoke(type); | |
public static implicit operator Action<In>(AmplifiedAction<In> amplified) => amplified._action; | |
public static implicit operator AmplifiedAction<In>(Action<In> action) => new AmplifiedAction<In>(action); | |
public override bool Equals(object? obj) | |
{ | |
if (obj is AmplifiedAction<In>) return true; | |
if (!(obj is AmplifiedAction<In>)) return false; | |
return Equals(_action, (obj as AmplifiedAction<In>)?._action); | |
} | |
public override int GetHashCode() => base.GetHashCode() << 23; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment