[Range(-3, 3)]
public float range;
I want later in the code to do something like :
range.min = 2
range.max = 20;
Or get
int min = range.min;
int max = range.max;
[Range(-3, 3)]
public float range;
I want later in the code to do something like :
range.min = 2
range.max = 20;
Or get
int min = range.min;
int max = range.max;
You can't change an attribute's state at runtime, but you can read it using reflection:
class YourClass
{
[Range(-3, 3)]
public float range;
}
var range = typeof(YourClass)
.GetField(nameof(YourClass.range))
.GetCustomAttribute<RangeAttribute>();
float min = range.min;
float max = range.max;
Can you change it to an object like below?
public class range
{
private float _value;
public range(int min, int max)
{
Min = min;
Max = max;
}
public float Value
{
get
{
return _value;
}
set
{
if (value > Max || value < Min) throw new Exception("value out of
range.");
_value = value;
}
}
public int Min { get; }
public int Max { get; }
}
use it like;
rangeObj = new range(-3,3);
rangeObj.Min
rangeObj.Max
rangeObj.Value