public class Ticket
{
private int _distance;
public int Distance
{
set{this._distance = value;}//进行赋值设置
get{return this._distance;}
}
}
当然对值进行一些限定时可以直接在属性里进行,根据不同环境取值
public class Ticket
{
private int _distance;
public int Distance
{
if(value<0)
{
return;
}
set{this._distance = value;}//进行赋值设置
get{return this._distance;}
}
}
public class Car
{
public readonly int Height;
}
static void Main(string[] args)
{
Car car = new Car();
FieldInfo fi = car.GetType().GetField("Height");
fi.SetValue(obj, 100); //ok
Console.WriteLine(car.Height); //100
Console.Read();
}