Created
August 6, 2020 11:28
-
-
Save HolyMonkey/6e57d7e2d1ac26f2c64c2b09bfe0fd24 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace OOP6._2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
IPlayerPresenter enteredObject = new Car(); | |
if(new[]{ typeof(Bicycle), typeof(Soldier), typeof(Car) } | |
.Contains(enteredObject.GetType())) | |
{ | |
Console.WriteLine(true); | |
} | |
////V1 | |
//if(enteredObject.Is(typeof(Bicycle), typeof(Soldier), typeof(Car))) | |
//{ | |
// Console.WriteLine(true); | |
//} | |
if (enteredObject.Is<Bicycle>().Or<Soldier>().Or<Car>()) | |
{ | |
Console.WriteLine(true); | |
} | |
} | |
} | |
class Bicycle : IPlayerPresenter { } | |
class Soldier : IPlayerPresenter { } | |
class Car : IPlayerPresenter { } | |
class Helicopter : IPlayerPresenter { } | |
interface IPlayerPresenter { } | |
////V1 | |
//static class TypeCheckingExtensions | |
//{ | |
// public static bool Is(this object obj, params Type[] types) | |
// { | |
// var type = obj.GetType(); | |
// return types.Contains(type); | |
// } | |
//} | |
static class TypeCheckingExtensions | |
{ | |
public static Builder Is<T>(this object obj) | |
{ | |
var type = typeof(T); | |
return new Builder(obj.GetType(), type); | |
} | |
public class Builder | |
{ | |
private List<Type> _types = new List<Type>(); | |
private Type _targetType; | |
public Builder(Type targetType, Type whiteType) | |
{ | |
_targetType = targetType; | |
_types.Add(whiteType); | |
} | |
public bool Check() | |
{ | |
return _types.Contains(_targetType); | |
} | |
public static implicit operator bool(Builder builder) | |
{ | |
return builder.Check(); | |
} | |
public Builder Or<T>() | |
{ | |
_types.Add(typeof(T)); | |
return this; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment