简体   繁体   中英

How are classes shared between projects in the same solution?

I'm totally new to the Microsoft dev scene so please go easy :) Currently, I have a solution that contains two projects (ASP.net websites, FWIW). If I create a class in Project1, how do I expose it to Project2? Is it automatically available to Project2 because they are in the same solution? Or do I need to do something special to make the connection?

you need to Add Reference of Class to the project

Like this:

在此输入图像描述

Then Go to Projects Tab (if your project is in Same Soln ) .Select the class library need to refer and click ok.

在此输入图像描述

When you expand your Reference you can see the class1.dll like this

在此输入图像描述

Hope this may help you :)

Go to the properties of Project2 , and add Project1 as a "Project Reference".

Then your class will be available via Project1.ClassName

You can then use the Imports Project1 statement to shorten the reference, and just reference ClassName

Add a project reference. Go to solution explorer and right click on Project 2's 'References' node. Select 'Add refrence'. From the 'Projects' tab select Project 1. That's it. Now you can use classes from Project 1 in Project 2.

您需要添加对项目的引用(项目的上下文菜单),并在类文件中添加Imports语句以使用其他项目中的对象。

Normally you would just create a reference from Project 1 to Project 2, but as they are both ASP.NET websites i don't know if that would be possible (i've never tried that particular combo).

What you can do instead if that fails is share the actual class files between the two projects - in the Solution Explorer right click, Add existing file , then browse to the file containing the wanted class in project 2, and then just before you click Add, click the little down arrow on the button and select Add as link .

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