Background: I have a large application that imports entities from one system into another. The source system has a primary key, which I'll call SourceId
, and the destination system has its own key, which I'll call DestinationId
, but the destination record also records the SourceId
property, so a destination record can be used to find the source record.
Suppose I have code like this:
Class Destination
Public DestinationId As String
Public SourceId As String
End Class
Dim src = GetSource()
Dim dst = New Destination();
dst.DestinationId = RandomGuidString()
dst.SourceId = src.Id
Because both Id
properties are strings with similar names, it's easy to mix them up:
dst.DestinationId = src.Id <---BUG! I meant to set dst.SourceId instead
What I'd really like is the ability to do this:
TypeAlias SourceId = System.String
TypeAlias DestinationId = System.String
Class Destination
Public DestinationId As DestinationId
Public SourceId As SourceId
End Class
dst.DestinationId = src.Id <---Now this causes a compiler error because the types are different, even though they're really both strings
So is there any way in .NET to define a type alias for primitive types that the compiler can enforce in this way.
This is VB.NET, but I'm curious about c# as well.