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
bighugemassive3
bighugemassive3OP2mo ago
Yeah The constructor takes Func<string, TValue> fromString That what I think i'll do, just use object instead of TValue
jcotton42
jcotton422mo ago
You can do (TValue)(object)val if (TValue)val doesn’t work. The JIT recognizes this pattern and will throw away the cast.
bighugemassive3
bighugemassive3OP2mo ago
It's for a serialisable property I'm wondering if constrainting TValue to INumberBase<TValue> would work
jcotton42
jcotton422mo ago
Do you actually care if it’s a number, or just if it’s parseable?
bighugemassive3
bighugemassive3OP2mo 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
paperClip2mo ago
yet another generic bridging problem smh

Did you find this page helpful?