Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juliusfriedman/24acb47874239fadb6ae5123b0b65329 to your computer and use it in GitHub Desktop.
Save juliusfriedman/24acb47874239fadb6ae5123b0b65329 to your computer and use it in GitHub Desktop.
Indirect Enumerator
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class IndirectEnumerator<T> : System.Collections.Generic.IEnumerator<T>
{
public readonly System.Collections.Generic.IEnumerator<T> Enumerator;
T Current;
ref T CurrentReference => ref Current;
T IEnumerator<T>.Current => Current;
object IEnumerator.Current => Current;
bool IEnumerator.MoveNext()
{
var r = Enumerator.MoveNext();
Current = Enumerator.Current;
return r;
}
void IEnumerator.Reset() => Enumerator.Reset();
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Enumerator.Dispose();
}
}
void System.IDisposable.Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
System.GC.SuppressFinalize(this);
}
public IndirectEnumerator(System.Collections.Generic.IEnumerator<T> enumerator) => Enumerator = enumerator;
public virtual bool ReferenceEquals(ref T t, ref T b) => Holder<T>.ReferenceEquals(ref t, ref b);
}
public static class Holder<T>//DynamicHolder
{
public static T SystemDefault = default;
public static T CurrentDefault = SystemDefault;
public static T Value;
public static System.Collections.Generic.IEqualityComparer<T> EqualityComparer = System.Collections.Generic.EqualityComparer<T>.Default;
public static bool ReferenceEquals(ref T current, ref T other) => current.GetHashCode() == current.GetHashCode();
public static bool ReferenceEquals(ref T other) => ReferenceEquals(ref Value, ref other);
public static bool Equals(T t, T other) => ReferenceEquals(ref t, ref other);
//DynamicEquals
public static dynamic AsDynamic => Value;
public static bool DynamicEquals(T t, T other) => (dynamic)t == other;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment