Operador implícito?
-
08-07-2019 - |
Pregunta
Necesito ayuda. Estoy creando una clase SelectItem como esta:
public class SelectItem<T> where T : class
{
public bool IsChecked { get; set; }
public T Item { get; set; }
}
Me gustaría que el siguiente código sea válido
SelectItem<String> obj = new SelectItem<String> { Item = "Value" };
obj.IsChecked = true;
String objValue = obj;
En lugar de tener que hacer esto:
String objValue = obj.Item;
¿Cómo puedo lograr esto?
Solución
public static implicit operator T(SelectItem<T> obj) {
return obj.Item;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow