How to use a class from one C# project with another C# project

后端 未结 9 937
花落未央
花落未央 2020-11-27 15:12

In the same solution, I have two projects: P1 and P2. How can I make use of a class of P1 in P2?

相关标签:
9条回答
  • 2020-11-27 15:52
    1. In the 'Solution Explorer' tree, expand the P2 project and then right-click the project and select 'Add Reference' from the menu.
    2. On the 'Add Reference' dialog, select the 'Projects' tab and select your P1 project.
    3. If you are using namespaces then you will need to import the namespaces for your P1 types by adding 'using' statements to your files in P2.

    Note that the types in P1 that you wish to access directly must have a sufficient access level: typically this means they must be made public.

    0 讨论(0)
  • 2020-11-27 15:54

    Simply add reference to P1 from P2

    0 讨论(0)
  • 2020-11-27 15:54

    In project P1 make the class public (if it isn't already). Then add a project reference (rather than a file reference, a mistake I've come across occasionally) to P2. Add a using statement in P2 at the correct place and start using the class from P1.

    (To mention this: The alternative to making the class public would be to make P2 a friend to P1. This is, however, unlikely to be the answer you are after as it would have some consequences. So stick with the above suggestion.)

    0 讨论(0)
提交回复
热议问题