循环参考问题 [英] Circular Reference Problem

查看:109
本文介绍了循环参考问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,我需要在这两个类之间具有父级-子级的实现.
假设在项目1中有一个A类,在项目2中有另一个B类.
现在我希望A将B类型的列表作为其子代,而B将A类型的变量作为其父代.
请注意它们存在于不同的项目中,这些项目存在于Visual Studio的同一解决方案中.这是否是引起循环引用的正确方法.
还有其他方法可以在没有循环引用的情况下处理类似这样的需求吗?

i have two classes between which i need to have parent -child realtion.
Say there is a class A in project 1 and another class B in project 2.
Now i want A has list of type B as its child,and B has a variable of type A as its parent.
NOTE THAT THEY ARE IN DIFFERENT PROJECTS,which exists in the same solution of visual studio.Is this the correct appproach as its causing CIRCULAR REFERENCE.
Is there some other way in which a requirement like this can be handled without having circular reference ???
Circular reference is causing problems during build.

推荐答案

在C#中,原则上不存在此类问题;在C ++中,转发声明"会有所帮助.

In C#, such problem does not exist in principle; in C++, "forward declaration" helps.

class Second; // this is forward declaration;
// without it, the class First will not compile

class First {
   Second* second;
};

class Second {
   First* first;
};



—SA



—SA


解决方案1将解决编译器问题,但是如果我正确理解,您的问题就是链接器问题:您不能链接任何一个项目,因为实现引用其他库中该类的API,因此至少需要.lib文件.

解决此问题的一种方法是:

1.通过删除引用库2的代码来临时更改库1中的实现.

2.还要在​​链接器设置中删除#include和对库2的引用

3.构建库1(现在不再需要库2)

4.将生成的.lib文件复制到库2期望的任何位置

5.构建库2

6.将库2 .lib文件复制到库1期望的任何位置

7.将库1恢复到原始状态

8.重新构建

之后,除非您同时更改两个库的API,否则无需再次重复这些步骤. (所以请避免!)

这样做的基本原理是,链接程序将.lib文件视为链接器,将链接器视为头文件.因此,要链接一个库,您需要另一个库的.lib文件,但是如果链接失败,则不会获得该.lib文件.无论如何,.lib文件仅包含API信息,以后再更改实现都没有关系.

当然,更清洁的解决方案是将这两个类从这些项目中放到一个新的第三个项目中,然后从另外两个中引用一个.
Solution 1 will solve the compiler issue, but if I get you correctly yours is a linker problem: you cannot link either project, because the implementation references the API of the class in the other library, and therefore at the very least needs the .lib file.

One way to fix this would be:

1. Temporarily change the implementation in library 1 by removing the code that references library 2.

2. Also remove the #include and the reference to library 2 in the linker settings

3. build library 1 (it now no longer requires library 2)

4. copy the resulting .lib file to wherever library 2 expects it

5. build library 2

6. copy the library 2 .lib file to wherever library 1 expects it

7. Restore library 1 to its original state

8. Rebuild it

After this you should not need to repeat these steps again, unless you simultaneously change the API of both libraries. (so avoid that!)

The rationale behind this is that the .lib file is to the linker what the header files are to the compiler. So to link one library, you need the .lib file of the other, but you won''t get that .lib if the link fails. Anyhow, the .lib file only contains the API information, and it doesn''t matter if you change the implementation later.

Of course, the cleaner solution would be to put the two classes out of these projects and into a new, third project, then reference that one from the other two.


也许我遗漏了一些东西,但是为什么不仅仅使用来自两个都可以引用的单独库中的公共接口呢?

InterfaceLib.dll
Maybe I''m missing something, but why not just use a common interface from a separate library that both could reference?

InterfaceLib.dll
namespace CircRefProb.Interfaces
{
    public interface IParent
    {
    }
}
namespace CircRefProb.Interfaces
{
    public interface IChild
    {
    }
}



ParentLib.dll



ParentLib.dll

using System;
using System.Collections.Generic;
using CircRefProb.Interfaces;

namespace Parent
{
    public class ParentNode: IParent
    {
        public List<IChild> Children { get; set; }
    }
}



ChildLib.dll



ChildLib.dll

using System.Collections.Generic;
using CircRefProb.Interfaces;

namespace Child
{
    public class ChildNode: IChild
    {
        public IParent Parent {get;set;}
    }
}


这篇关于循环参考问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆