Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get MinValue/MaxValue of a certain ValueType via reflection?

I need to this at runtime. I checked using Reflector and value types line like Int16, for example, should contain

<Serializable, StructLayout(LayoutKind.Sequential), ComVisible(True)> _
Public Structure Int16
    Implements IComparable, IFormattable, IConvertible, IComparable(Of Short), IEquatable(Of Short)

Public Const MaxValue As Short = &H7FFF
Public Const MinValue As Short = -32768


End Structure

But the following code is not working

Dim dummyValue = Activator.CreateInstance(GetType(UInt16))
Dim minValue As IComparable =    DirectCast(dummyValue.GetType.GetProperty("MinValue").GetValue(dummyValue,
Nothing), IComparable)

any idea how to solve?

EDIT: only for the example I used directly GetType(UInt16) but in real code this part is substituted by an instance of unknown-at-design-time .NET Type

like image 442
Drake Avatar asked Jan 22 '23 08:01

Drake


1 Answers

Use GetType.GetField("MinValue"). Constants are considered Fields

like image 58
M.A. Hanin Avatar answered May 13 '23 13:05

M.A. Hanin