Is there a way to control type conversion in C #? So, for example, if I have two types with essentially the same details, but one is used for the internal operation of my application, and the other is the DTO, used to communicate with non-net applications:
public sealed class Player { public Player(string name, long score) { Name = name; Score = score; ID = Guid.NewGuid(); } public string Name { get; private set; } public Guid ID { get; private set; } public long Score { get; private set; } } public sealed class PlayerDTO { public PlayerDTO(string name, long score, string id) { Name = name; Score = score; ID = id; } public string Name { get; private set; }
Right now, I need to create a new PlayerDTO instance each time from my Player instance, and I'm looking for a better, cleaner way to do this. One of my ideas was to add the AsPlayerDTO () method to the player class, but it would be nice if I could control the type conversion process so that I could do this instead:
var playerDto = player as PlayerDTO;
Does anyone know if this is possible and how I can do it?
Thanks,
theburningmonk
source share