我的项目对象的 OnBuy 和 OnSell 有一个委托,问题是我要复制粘贴一些项目,而不是修改每个 OnBuy 和 OnSell 的关键字名称,并尝试使用“this”关键字,我已经添加我对项目类的函数,但在复制粘贴后如果不修改对象名称仍然无法访问它。这是我的代码:
public static Item item = new Item
{
Name = "Item",
ID = 1,
Price = 50,
Info = "Awesome!",
OnBuy = delegate(Client cli)
{
// Invalid
this.BuyTitle(cli);
// Still can't change
this.Name = "AAA";
return true;
},
OnSell = delegate(Client cli)
{
// Invalid
this.SellTitle(cli);
// Still can't change
this.Name = "AAA";
return true;
}
}
这是项目类:
public class Item
{
public string Name { get; set; }
public int ID { get; set; }
public int Price { get; set; }
public string Info { get; set; }
public Func<Client, bool> OnBuy { get; set; }
public Func<Client, bool> OnSell { get; set; }
public bool BuyTitle(Client cli)
{
...
}
public bool SellTitle(Client cli)
{
...
}
}