简体   繁体   中英

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.

2) In Another project called, lets say, MyGame, there is a class called MyAwesomeGame, in which I need to do something like:

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. Code is SocketServer project is something like:

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? Should I add reference of my SocketServer Project? and if I do, wont it get complied into my MyGame.dll?

ps: I also intend to distribute the socketserver.dll as an API to thrid-party users.

You should add the SocketServer's generated dll to the references of the MyGame project. Right click on MyGame, click Add, click References..., in the dialog select Projects and select the SocketServer project.

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.

--edit: please read the comment below that I made after Sushant's clarification. Or go straight to Sid's answer. :-)

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 .

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)
  2. ProjectA (has a dependency on ModelsProject)
  3. ProjectB (has a dependency on ModelsProject)


Scheme

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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