Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lgolubyev
Created May 23, 2022 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgolubyev/01931596732bc91d4991aad439434f65 to your computer and use it in GitHub Desktop.
Save lgolubyev/01931596732bc91d4991aad439434f65 to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
ExternalEntity entity = new ExternalEntity()
{
Id = 1001,
FirstName = "Dave",
LastName = "Johnson"
};
MyEntity convertedEntity = (MyEntity)entity;
}
}
class MyEntity
{
public int Id { get; set; }
public string FullName { get; set; }
public static explicit operator MyEntity(ExternalEntity externalEntity)
{
return new MyEntity()
{
Id = externalEntity.Id,
FullName = externalEntity.FirstName + " " + externalEntity.LastName
};}
}
class ExternalEntity
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment