How to make a conditional if depending on which model type my variable contains?

0

I have the following code below:

var response = await ...

the response of the await can be of two types, for example of type Model1 or of type Model2 depending on which type model is the answer I need to do certain things, it is here as I do not know if there is something in c # to be able to tell if variable response contains or has a model of type Modelo1 that does this or else does this another one.

    
asked by vcasas 06.06.2018 в 21:13
source

2 answers

2

If I did not misunderstand your question, you can use typeof and GetType()

You should use it like this

var response = await ....   
if(response != null)
{
    if(typeof(Modelo1) == response.GetType())
    {
    //Lógica si es Modelo1
    }
else
    {
      //Lógica si es Modelo2 u cualquier otro Objeto
    }
}

Sorry if there is a syntax error but I do not have IDE at hand, I hope it is what you are looking for, greetings!

    
answered by 06.06.2018 / 21:21
source
0

Create a generic class, this is the one I use:

public interface ITypeOf<T>
{
    T Value {get;}
}

public abstract class TypeOf<T1, T2>
{
    private TypeOf() {}

    public static TypeOf<T1, T2> Type1(T1 value) =>
        new Type1(value);

   public static TypeOf<T1, T2> Type2(T2 value) =>
       new Type2(value);

    public abstract bool IsType1 { get; }
    public abstract bool IsType2 { get; }
    public abstract TResult Switch<TResult>(Func<T1, TResult> isType1, Func<T2, TResult> isType2);

   private static class Types
   {
       public class Type1 : TypeOf<T1, T2>, ITypeOf<T1>
       {
           public Type1(T1 value)
           {
               Value = value;
           }

           public T1 Value {get;}
           public override bool IsType1 => true;
           public override bool IsType2 => false;
           public override TResult Switch<TResult>(Func<T1, TResult> isType1, Func<T2, TResult> isType2) =>
               isType1(Value);
       }

       public class Type2 : TypeOf<T1, T2>, ITypeOf<T2>
       {
           public Type2(T2 value)
           {
               Value = value;
           }

           public T2 Value {get;}
           public override bool IsType1 => false;
           public override bool IsType2 => true;
           public override TResult Switch<TResult>(Func<T1, TResult> isType1, Func<T2, TResult> isType2) =>
               isType2(Value);
       }
    }
}

And to use it:

public string GetResult(TypeOf<string, int> value)
{
    if (value.IsType1) // evitamos un casteo innecesario
    {
        var result = value as ITypeOf<string>;
        return $"El resultado es string: {result.Value}"; // la propiedad Value es de tipo string
    }
   else
   {
       var result = value as ITypeOf<int>;
       return $"El resultado es int: {result.Value}"; // la propiedad Value es de tipo int
    }

    // con c# 7:
    // if (value is IType<string> result) 
    // {
    //      return $"El res.....: {result.Value}";
    // }
}

Or using the switch method:

public string GetResult(TypeOf<string, int> value)
{
    return value.Switch(
        isType1: s => $"El resultado es string: {s}", // en esta función lambda, s es de tipo string
        isType2: i => $"El resultado es int: {i}); // en esta función lambda, i es de tipo int
}

And so you create the object:

var opcion1 = TypeOf<string, int>.Type1("hola");
var opcion2 = TypeOf<string, int>.Type2(3);

var x = GetResult(opcion1); // x debería ser igual a "El resultado es string: hola"
var y = GetResult(opcion2); // y debería ser igual a "El resultado es int: 3"
    
answered by 07.06.2018 в 04:22