¿Puedo crear descriptores de acceso de estructuras para convertir automáticamente a / desde otros tipos de datos?

StackOverflow https://stackoverflow.com/questions/2353632

  •  23-09-2019
  •  | 
  •  

Pregunta

Es posible hacer algo como lo siguiente:

struct test
{
   this
   {
      get { /*do something*/ }
      set { /*do something*/ }
   }
}

de manera que si alguien trató de hacer esto,

test tt = new test();
string asd = tt; // intercept this and then return something else

Otros consejos

Se puede definir implícito y operadores de conversión explícitas desde y hacia su tipo personalizado

public static implicit operator string(test value)
{
    return "something else";
}

Ampliando la respuesta de Mikep desea algo como:

public static implicit operator Test( string value )
{
    //custom conversion routine
}

o

public static explicit operator Test( string value )
{
    //custom conversion routine
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top