How do I compile C# code as a library instead of an executable?

后端 未结 5 670
盖世英雄少女心
盖世英雄少女心 2021-01-12 02:56

I have a C# console application in Visual Studio 2010. It has a Main() method as well as a bunch of utility classes. I\'d like those utility classes to be available to other

相关标签:
5条回答
  • 2021-01-12 03:35

    Make sure that the classes in your dll project are public.

    0 讨论(0)
  • 2021-01-12 03:41

    You do not need to build it as a dll. VS 2010 (and IIRC 2008) allow referencing exe assemblies. All you need is for they relevant types to be declared public - top-level classes defualt to internal if you don't add a specifier.

    0 讨论(0)
  • 2021-01-12 03:52

    At first, from the point of view of managed libraries it does not matter what kind of Output type is your managed library. I mean that you can successfully reference ConsoleApplication1.exe from ConsoleApplication2.exe project (so you have no reason to convert ConsoleApplication1.exe to ConsoleApplication1.dll).

    At second, I've tried to reproduce your situation, but... without effect. My VS displays types/methods from ConsoleApplication1.dll. One reason I can suppose is that you have forgotten to set visibility modifier (public keyword) for your utility classes.

    0 讨论(0)
  • 2021-01-12 03:55

    You can switch output type to Class library in project properties as well - then you will have an output as dll instead exe file

    0 讨论(0)
  • 2021-01-12 03:58

    What I've always done (since this is what you do with C++ static libraries, which is what I normally use - though I think it has some advantages for C# too) is add the class library's project to the solution, then add a reference to it in the project (or projects) that uses it. When you go to add a reference, the list of potential references includes items from the solution, so it should be fairly obvious what to do. You should then get intellisense for your library.

    One advantage of doing things this way is that if you need to edit files in the library project, it's very straightforward because they are close to hand, and the project then gets rebuilt automatically when you compile the solution.

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