简体   繁体   English

在两个c#程序集之间共享代码

[英]Sharing code between two c# assemblies

1) I have a project called SocketServer in which there is a Class Room , this project is a complied exe and can also be complied as a DLL. 1)我有一个名为SocketServer的项目,其中有一个Class Room ,这个项目是一个编译的exe,也可以编译为DLL。

2) In Another project called, lets say, MyGame, there is a class called MyAwesomeGame, in which I need to do something like: 2)在另一个名为MyGame的项目中,有一个名为MyAwesomeGame的类,我需要在其中执行以下操作:

public class MyAwesomeGame
{
    public Init(Room room)
    {
        //I can get data from Room
    }
}

Now MyGame Project is compiled into a MyGame.DLL, and is placed somewhere relative to the SocketServer.exe, which at runtime loads MyGame.DLL and Instantiates MyAwesomeGame class and calls the Method Init and passes room as its parameter. 现在,MyGame Project被编译成MyGame.DLL,并放置在相对于SocketServer.exe的某个地方,SocketServer.exe在运行时加载MyGame.DLL并实例化MyAwesomeGame类并调用Method Init并将room作为参数传递。 Code is SocketServer project is something like: 代码是SocketServer项目是这样的:

public class Room
{
     private InstantiateGameRoom()
     {
          //Load External MyGame.DLL Assembly 
          Task<MyAwesomeGame> task = Task<MyAwesomeGame>.Factory.StartNew(() => (MyAwesomeGame)Activator.CreateInstance(classType, new object[] { this }));
          MyAwesomeGame instance = task.result;
          instance.init(this);
     }
}

So my question is how can I get reference of the Class room in MyGame Project? 所以我的问题是我怎么能获得MyGame项目在教室的参考? Should I add reference of my SocketServer Project? 我应该添加我的SocketServer项目的引用吗? and if I do, wont it get complied into my MyGame.dll? 如果我这样做,它会不会被我的MyGame.dll编译?

ps: I also intend to distribute the socketserver.dll as an API to thrid-party users. ps:我还打算将socketserver.dll作为API分发给第三方用户。

You should add the SocketServer's generated dll to the references of the MyGame project. 您应该将SocketServer生成的dll添加到MyGame项目的引用中。 Right click on MyGame, click Add, click References..., in the dialog select Projects and select the SocketServer project. 右键单击MyGame,单击Add,单击References ...,在对话框中选择Projects并选择SocketServer项目。

I believe it is also wise to make sure that SocketServer is a depedency for the MyGame project to make sure SocketServer is built before MyGame. 我相信确保SocketServer是MyGame项目的依赖性以确保在MyGame之前构建SocketServer也是明智之举。

--edit: please read the comment below that I made after Sushant's clarification. - 编辑:请阅读我在Sushant澄清之后做出的评论。 Or go straight to Sid's answer. 或直接去Sid的回答。 :-) :-)

I think the proper way to share Model s among two or more projects is to extract all the models and relative logics to a separate project . 我认为在两个或更多项目中共享Model的正确方法是将所有模型和相对逻辑提取到一个单独的项目中

Then you can reference that project from every project that needs to use that data. 然后,您可以从需要使用该数据的每个项目中引用该项目。

The scenario I am talking about is the following: 我正在讨论的场景如下:

  1. ModelsProject (Contains models and business logic) ModelsProject (包含模型和业务逻辑)
  2. ProjectA (has a dependency on ModelsProject) ProjectA (依赖于ModelsProject)
  3. ProjectB (has a dependency on ModelsProject) ProjectB (依赖于ModelsProject)


Scheme 方案

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM