We created Id32
and Id64
struct to wrap integers and long values coming from DB, so they can be explicitly processed as IDs by a Json converter (with dedicated custom converters).
The problem is that we read this data from a Dictionary<string, object>
that actually is a DataRow
-like object where the string
part is the name of the column and the object
part is the value.
So before we had this code to read the value:
int myVal = (int)row["COLUMN"]
We want this code to continue working also after these changes.
But since row["COLUMN"]
is an object
(@ compile-time) the implicit cast fails, even though it is actually an Id32
(@ run-time).
The following obviously works:
int myVal = (Id32)row["COLUMN"]
But is there some to way to fix this without modifying the code that reads the value?
This is the struct code:
public struct Id32
{
public readonly int Value;
public Id32(int id) { this.Value = id; }
public static implicit operator int(Id32 id) { return id.Value; }
public static implicit operator Id32(int id) { return new Id32(id); }
}