C#中的循环引用 [英] Circular Referencing in C#

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

问题描述




我有一种情况,我想使用循环引用。为了演示目的,我在示例中剪了




假设我们有一个可执行文件(main.exe)和两个DLLS(A1.dll和A2。 dll)。

Main可以访问A1和A2中的类; A1可以访问A2中的类。


但是,我现在想要从A2访问A1中的类。我不能简单地

创建第三个DLL,因为A1 / A2组件使用

各自组件中的其他类。


我相信我可以使用接口类来解决这种情况。

但是,说实话,我不知道怎么做?


一个例子就是非常实用....

例如在main.exe中

--------------

使用A1;

使用A2;


A1C1 a1c1 =新A1C1();

A2C1 a2c1 =新A2C1();

A1.DLL

------

使用A2;

名称空间A1

{

///< summary>

/// Class1的摘要说明。

///< / summary> ;

公共舱A1C1

{

公共A1C1()

{

A2C1 c =新A2C1();

}

}

公共类A1C2

{

public A1C2()

{

//我想从A2获取课程

}

}

}


A2.DLL

------


命名空间A2

{

///< summary>

/// Class1的摘要说明。

// /< / summary>

公共类A2C1

{

public A2C1()

{

//可以从A1访问此课程

}

}

公共类A2C2

{

public A2C2()

{

//我现在想要访问A1C2 ??????

}

}

}

Hi

I have a situation where I want to use circular referencing. I have cut
down the example for demonstration purposes.

Say we have one executable (main.exe) and two DLLS (A1.dll and A2.dll).
Main can access classes in A1 and A2; A1 can access classes in A2.

However, I now want to access classes in A1 from A2. I cannot simply
create a third DLL as the A1/A2 assemblies use other classes within
their respective assemblies.

I believe that I can use interface classes to solve this situation.
However, to be honest, I''m not sure how?

An example would be very useful....
eg in main.exe
--------------
using A1;
using A2;

A1C1 a1c1 = new A1C1();
A2C1 a2c1 = new A2C1();
A1.DLL
------
using A2;
namespace A1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class A1C1
{
public A1C1()
{
A2C1 c = new A2C1();
}
}
public class A1C2
{
public A1C2()
{
// I want access thi class from A2
}
}
}

A2.DLL
------

namespace A2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class A2C1
{
public A2C1()
{
// Can access this class from A1
}
}
public class A2C2
{
public A2C2()
{
// I now want to access A1C2??????
}
}
}

推荐答案




在单独的程序集中声明A1C2的接口。请参阅

A2。当您传递A1C2的实例时,将其作为接口传递。


问候

Senthil


PromisedOyster写道:
Hi,

Declare an interface for A1C2 in a separate assembly. Refer it from
A2. And when you pass an instance of A1C2, pass it as an interface.

Regards
Senthil

PromisedOyster wrote:


我有一种情况,我想使用循环引用。为了演示目的,我有
减少了示例。

说我们有一个可执行文件(main.exe)和两个DLLS(A1.dll和
A2.dll)。 Main可以访问A1和A2中的类; A1可以访问A2中的类。

但是,我现在想要从A2访问A1中的类。我不能简单地创建第三个DLL,因为A1 / A2程序集使用其各自程序集中的其他类。

我相信我可以使用接口类来解决这种情况。
但是,说实话,我不知道怎么做?

一个例子非常实用....

例如在main.exe中使用A1;
使用A2;

A1C1 a1c1 = new A1C1();
A2C1 a2c1 =新A2C1();

A1.DLL
------
使用A2;
名称空间A1
{
///< summary>
/// Class1的摘要说明。
///< / summary>
公共类A1C1
{
公众A1C1()
{
A2C1 c = new A2C1();
}
}
公共类A1C2
公共A1C2()
{
//我希望从A2获取课程
}
}


A2.DLL
- ----

名称空间A2
///< summary>
/// S Class1的ummary描述。
///< / summary>
公共类A2C1
公共A2C1()
{
//可以访问本课程来自A1
}
}
公共课A2C2
{
公共A2C2()
{
//我现在想要访问A1C2 ??????
}
}
}
Hi

I have a situation where I want to use circular referencing. I have cut down the example for demonstration purposes.

Say we have one executable (main.exe) and two DLLS (A1.dll and A2.dll). Main can access classes in A1 and A2; A1 can access classes in A2.

However, I now want to access classes in A1 from A2. I cannot simply
create a third DLL as the A1/A2 assemblies use other classes within
their respective assemblies.

I believe that I can use interface classes to solve this situation.
However, to be honest, I''m not sure how?

An example would be very useful....
eg in main.exe
--------------
using A1;
using A2;

A1C1 a1c1 = new A1C1();
A2C1 a2c1 = new A2C1();
A1.DLL
------
using A2;
namespace A1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class A1C1
{
public A1C1()
{
A2C1 c = new A2C1();
}
}
public class A1C2
{
public A1C2()
{
// I want access thi class from A2
}
}
}

A2.DLL
------

namespace A2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class A2C1
{
public A2C1()
{
// Can access this class from A1
}
}
public class A2C2
{
public A2C2()
{
// I now want to access A1C2??????
}
}
}






嗨sadhu

能否请您提供一个您的意思的例子。


这将是非常有用的。我不确定为什么需要单独组装




sadhu写道:
Hi sadhu

Could you please provide me with an example of what you mean.

This would be very useful. I am not sure why there is a need for the
separate assembly?

sadhu wrote:

在单独的程序集中声明A1C2的接口。请参阅
A2。当你传递一个A1C2的实例时,把它作为一个接口传递。

问候
Senthil

PromisedOyster写道:
Hi,

Declare an interface for A1C2 in a separate assembly. Refer it from
A2. And when you pass an instance of A1C2, pass it as an interface.

Regards
Senthil

PromisedOyster wrote:
你好

我有一种情况,我想使用循环引用。为了演示目的,我有
Hi

I have a situation where I want to use circular referencing. I have


剪切

示例。

说我们有一个可执行文件(main.exe)和两个DLLS(A1。 dll和
down the example for demonstration purposes.

Say we have one executable (main.exe) and two DLLS (A1.dll and


A2.dll)。


A2.dll).

Main可以访问A1和A2中的类; A1可以访问A2中的类。

但是,我现在想要从A2访问A1中的类。我不能
只需创建第三个DLL,因为A1 / A2程序集在各自的程序集中使用其他类。

我相信我可以使用接口类来解决这种情况。
但是,说实话,我不知道怎么做?

一个例子非常实用....

例如在main.exe中使用A1;
使用A2;

A1C1 a1c1 = new A1C1();
A2C1 a2c1 =新A2C1();

A1.DLL
------
使用A2;
名称空间A1
{
///< summary>
/// Class1的摘要说明。
///< / summary>
公共类A1C1
{
公众A1C1()
{
A2C1 c = new A2C1();
}
}
公共类A1C2
公共A1C2()
{
//我希望从A2获取课程
}
}


A2.DLL
- ----

名称空间A2
///< summary>
///苏Class1的mmary描述。
///< / summary>
公共类A2C1
公共A2C1()
{
//可以访问本课程来自A1
}
}
公共课A2C2
{
公共A2C2()
{
//我现在想要访问A1C2 ??????
}
}
}
Main can access classes in A1 and A2; A1 can access classes in A2.

However, I now want to access classes in A1 from A2. I cannot simply create a third DLL as the A1/A2 assemblies use other classes within
their respective assemblies.

I believe that I can use interface classes to solve this situation.
However, to be honest, I''m not sure how?

An example would be very useful....
eg in main.exe
--------------
using A1;
using A2;

A1C1 a1c1 = new A1C1();
A2C1 a2c1 = new A2C1();
A1.DLL
------
using A2;
namespace A1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class A1C1
{
public A1C1()
{
A2C1 c = new A2C1();
}
}
public class A1C2
{
public A1C2()
{
// I want access thi class from A2
}
}
}

A2.DLL
------

namespace A2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class A2C1
{
public A2C1()
{
// Can access this class from A1
}
}
public class A2C2
{
public A2C2()
{
// I now want to access A1C2??????
}
}
}






它通过访问来帮助你知道你的意思来自A2C2的A1C2 ...做

你的意思是接受作为参数,创建一个新的实例......你用什么来b $ b需要用它做什么?


Senthil的解决方案是适用于所有情况的解决方案:创建一个包含接口的新组件。这些接口是合同

,其他两个集合中的类同意遵守。然后每个

汇编在接口方面处理另一个。


然而,在有限的情况下,你可以逃脱

将接口放在A2中,这将为您提供从A1到A2的单向

引用,但绝不会从另一个方向引用。


你能详细说明你需要做什么吗?

It would help to know what you mean by "access" A1C2 from A2C2... do
you mean accept as a parameter, create a new instance... what do you
need to do with it?

Senthil''s solution is the one that applies to all situations: create a
new assembly that contains interfaces. These interfaces are contracts
that the classes in the other two assemblies agree to honour. Then each
assembly deals with the other in terms of interfaces.

However, in limited situations, you may be able to get away with
putting the interfaces in A2, which would leave you with a one-way
references from A1 to A2, but never the other direction.

Could you give more details on what you need to do?


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

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