I have a generic class (C#),
class MyClass<T> where T : struct, IComparable<T>
{
public T filelocation;
}
T can be either UInt32 or UInt64 (nothing else).
I need to convert filelocation to a long to seek in a file...
I have tried the following
long loc = (T)myclass.filelocation;
long loc = (T)(object)myclass.filelocation;
But nothing seems to work...
Any ideas?
Call Convert.ToInt64
.
Writing (object)fileLocation
creates a boxed UInt32
.
Boxed value types can only be unboxed to their original value types, so you cannot cast it in one step to long
.
You could write (long)(ulong)fileLocation
, but that will fail for a uint
for the same reason.
Try Convert.ToInt64.
long loc = Convert.ToInt64(myclass.filelocation);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With