You can make a generic class like this:
public class MyProp<T>
{
private T _value;
public T Value
{
get
{
// insert desired logic here
return _value;
}
set
{
// insert desired logic here
_value = value;
}
}
public static implicit operator T(MyProp<T> value)
{
return value.Value;
}
public static implicit operator MyProp<T>(T value)
{
return new MyProp<T> { Value = value };
}
}
…then use it in a class like so:
class SomeClass
{
public MyProp<int> SomeProperty { get; set; }
}
The implicit operators means that you do not need to explicitly set or get the Value
property of MyProp
, but can write code to access the value in a more “natural” way:
SomeClass instance = new SomeClass();
instance.SomeProperty = 32;
int someInt = instance.SomeProperty;