C
C#9mo ago
hugeman

return Object<byte> after checking typeof(T) == typeof(byte)

Is this possible or is there a way to sort of do this? I'm trying to do something like this:
if (typeof(TValue) == typeof(sbyte)) {
return new MyObject<sbyte>(defaultValue, getValue, setValue, (x) => sbyte.Parse(x));
}
if (typeof(TValue) == typeof(sbyte)) {
return new MyObject<sbyte>(defaultValue, getValue, setValue, (x) => sbyte.Parse(x));
}
6 Replies
hugeman
hugemanOP9mo ago
Yeah The constructor takes Func<string, TValue> fromString That what I think i'll do, just use object instead of TValue
jcotton42
jcotton429mo ago
You can do (TValue)(object)val if (TValue)val doesn’t work. The JIT recognizes this pattern and will throw away the cast.
hugeman
hugemanOP9mo ago
It's for a serialisable property I'm wondering if constrainting TValue to INumberBase<TValue> would work
jcotton42
jcotton429mo ago
Do you actually care if it’s a number, or just if it’s parseable?
hugeman
hugemanOP9mo ago
Just parsable really where TValue : IParsable<TValue> seems to work though Though not sure if it works at runtime, i have way too many errors at the moment
PaperClip
PaperClip9mo ago
yet another generic bridging problem smh

Did you find this page helpful?