0

我有这个对象:

public class ObjectDisplay
{
    public int mMasterObjectID { get; set; }
    public ObjectInfo mMasterObject { get; set; }

    public int mChildObjectID { get; set; }
    public ObjectInfo mChildObject { get; set; }

    public ObjectDisplay()
    {
        mMasterObject = new ObjectInfo();

        mChildObject = new ObjectInfo();
    }
}

并且是一个类,在不正常Object Display的情况下将主对象(始终存在的主要对象)和子对象分组。ObjectInfo.ObjectType

我有这种方法可以为我获取这些项目:

public Object GetObjectDisplayByID(int _objectID)
{
    Object objectToReturn = new Object();

    if (objectToReturn.mMasterObject == null)
    {
        return null;
    }

    if (objectToReturn.mMasterObject.ObjectType != "Normal")
    {
        if (objectToReturn.mMasterObject == null && objectToReturn.mChildObject == null)
        {
            mObjectDAL.LinkObjects();

            return GetObjectDisplayByID(objectToReturn.mMasterObject.mObjectID);
        }

        if (objectToReturn.mMasterObject.mMasterObjectID != null)
        {
            // If we are here then the primary object is in fact a child object.
            // We need to swap the objects and fetch the primary object which will become the child object.

            ObjectInfo child = objectToReturn.mMasterObject;

            objectToReturn.mMasterObject = mObjectDAL.GetObjectByID((int)child.mMasterObjectID);

            objectToReturn.mMasterObjectID = objectToReturn.mMasterObject.mObjectID;

            objectToReturn.mChildObject = child;
            objectToReturn.mChildObjectID = child.mObjectID;
        }
        else
        {
            if (objectToReturn.mMasterObject.mChildObjectID == null)
            {
                // The links are not made yet and we would need to redo them.
                mObjectDAL.LinkObjects();

                return mObjectDAL.GetObjectDisplayByID(objectToReturn.mMasterObject.mObjectID);
            }

            objectToReturn.mChildObject = mObjectDAL.GetObjectByID((int)objectToReturn.mMasterObject.mChildObjectID);
            objectToReturn.mChildObjectID = objectToReturn.mChildObject.mObjectID;
        }
    }

    return objectToReturn;
}

我的主要问题是,通过这些行,我已经做到了,如果还没有 ID,则意味着尚未建立链接。我调用一个内部方法来做链接:

if (objectToReturn.mMasterObject.mChildObjectID == null)
{
    // The links are not made yet and we would need to redo them.
    mObjectDAL.LinkObjects();

    return mObjectDAL.GetObjectDisplayByID(objectToReturn.mMasterObject.mObjectID);
}

我想要的是再次实际调用相同的方法,以“重新启动”该方法。我做对了吗?那是我实际上所说的“递归调用”相同的方法吗?

4

1 回答 1

1

是的,您应该 递归调用您的方法如果您的代码中有一个条件,如果满足您想要再次执行您的方法中定义的相同步骤集,除非满足退出条件。

于 2013-07-16T15:44:08.617 回答