Is there a better alternative than this to 'switch on type'?

后端 未结 30 2407
梦毁少年i
梦毁少年i 2020-11-22 03:28

Seeing as C# can\'t switch on a Type (which I gather wasn\'t added as a special case because is relationships mean that more than one distinct

相关标签:
30条回答
  • 2020-11-22 04:13

    C# 8 enhancements of pattern matching made it possible to do it like this. In some cases it do the job and more concise.

            public Animal Animal { get; set; }
            ...
            var animalName = Animal switch
            {
                Cat cat => "Tom",
                Mouse mouse => "Jerry",
                _ => "unknown"
            };
    
    0 讨论(0)
  • 2020-11-22 04:14

    Create a superclass (S) and make A and B inherit from it. Then declare an abstract method on S that every subclass needs to implement.

    Doing this the "foo" method can also change its signature to Foo(S o), making it type safe, and you don't need to throw that ugly exception.

    0 讨论(0)
  • 2020-11-22 04:14

    Yes - just use the slightly weirdly named "pattern matching" from C#7 upwards to match on class or structure:

    IObject concrete1 = new ObjectImplementation1();
    IObject concrete2 = new ObjectImplementation2();
    
    switch (concrete1)
    {
        case ObjectImplementation1 c1: return "type 1";         
        case ObjectImplementation2 c2: return "type 2";         
    }
    
    0 讨论(0)
  • 2020-11-22 04:16

    If you were using C# 4, you could make use of the new dynamic functionality to achieve an interesting alternative. I'm not saying this is better, in fact it seems very likely that it would be slower, but it does have a certain elegance to it.

    class Thing
    {
    
      void Foo(A a)
      {
         a.Hop();
      }
    
      void Foo(B b)
      {
         b.Skip();
      }
    
    }
    

    And the usage:

    object aOrB = Get_AOrB();
    Thing t = GetThing();
    ((dynamic)t).Foo(aorB);
    

    The reason this works is that a C# 4 dynamic method invocation has its overloads resolved at runtime rather than compile time. I wrote a little more about this idea quite recently. Again, I would just like to reiterate that this probably performs worse than all the other suggestions, I am offering it simply as a curiosity.

    0 讨论(0)
  • 2020-11-22 04:20

    You're looking for Discriminated Unions which are a language feature of F#, but you can achieve a similar effect by using a library I made, called OneOf

    https://github.com/mcintyre321/OneOf

    The major advantage over switch (and if and exceptions as control flow) is that it is compile-time safe - there is no default handler or fall through

    void Foo(OneOf<A, B> o)
    {
        o.Switch(
            a => a.Hop(),
            b => b.Skip()
        );
    }
    

    If you add a third item to o, you'll get a compiler error as you have to add a handler Func inside the switch call.

    You can also do a .Match which returns a value, rather than executes a statement:

    double Area(OneOf<Square, Circle> o)
    {
        return o.Match(
            square => square.Length * square.Length,
            circle => Math.PI * circle.Radius * circle.Radius
        );
    }
    
    0 讨论(0)
  • 2020-11-22 04:22

    Another way would be to define an interface IThing and then implement it in both classes here's the snipet:

    public interface IThing
    {
        void Move();
    }
    
    public class ThingA : IThing
    {
        public void Move()
        {
            Hop();
        }
    
        public void Hop(){  
            //Implementation of Hop 
        }
    
    }
    
    public class ThingA : IThing
    {
        public void Move()
        {
            Skip();
        }
    
        public void Skip(){ 
            //Implementation of Skip    
        }
    
    }
    
    public class Foo
    {
        static void Main(String[] args)
        {
    
        }
    
        private void Foo(IThing a)
        {
            a.Move();
        }
    }
    
    0 讨论(0)
提交回复
热议问题