将调用方法的对象添加到列表中

时间:2013-10-14 13:28:02

标签: c# list queue

我正在尝试在C#+ XNA中创建一个简单的成就类,并且我想实现一个队列,这样当你在彼此之后快速获得多个成就时,后者将被添加到队列列表中。

以下是为获得某项成就而被调用的方法:

//This is the list that I want to add the achievements to be queued to
static List<Achievement> queueList;

public void GetAchievement()
{
        //If the player hasn't got the achievement yet and another achievement is NOT being drawn, get the achievement
        if (!got && !drawing)
            get = true;
        //If the player hasn't got the achievement yet and another achievement IS being drawn, add the achievement to the queue list
        else if (!got && drawing)
            queueList.Add(); //What do I do here?
 }

所以这将被称为:exampleAchievement.GetAchievement(); exampleAchievement是Achievement类的一个对象。

我无法弄清楚如何知道哪个对象正在调用GetAchievement(),所以我不知道要添加到queueList中的内容。

非常感谢任何帮助!

〜卢卡

编辑: 我用过:

queueList.Add(this);

但我只是愚蠢而没有正确测试,所以看起来没有任何东西被添加到列表中。 无论如何,谢谢你的帮助:3

4 个答案:

答案 0 :(得分:2)

您在寻找exampleAchievement实例吗?

queueList.Add(this);// is this you're looking for?

我错过了什么吗?

答案 1 :(得分:1)

听起来你要求this,它引用了调用实例方法的当前对象。

答案 2 :(得分:0)

如果ExampleAchievement从其基类Achievement获取此方法,则只需执行此操作:

queueList.Add(this);

并将ExampleAchievement的实例添加到队列中。但是,在使用它时,您需要以某种方式检查类型是否存在您需要获得的基本合同中不可见的特殊属性。

如果所有属性都只是在子类中覆盖属性,那么您不必担心这一点,因为在访问子实现时您将获得子实现。

答案 3 :(得分:0)

是否可以选择传递参数,以便将自己标识为正在调用的参数?

所以

public void GetAchievement(string caller)
{

if(caller == "Something")
{
//Do Something
}
else
{
//Do Something else
}
     //If the player hasn't got the achievement yet and another achievement is NOT being drawn, get the achievement
    if (!got && !drawing)
        get = true;
    //If the player hasn't got the achievement yet and another achievement IS being drawn, add the achievement to the queue list
    else if (!got && drawing)
        queueList.Add(); //What do I do here?
}

然后使用exampleAchievement.GetAchievement(this.name)调用它;