Include code file into C#? Create library for others?
I wo开发者_开发问答uld like to know how can I embedd a code source file (if possible), something like that:
class X
{
include "ClassXsource"
}
My second question - Can I build DLL or something like that for my colleagues to use? I need them to be able to call methods from my "part" but do not modify or view their content. Basically just use namespace "MyNamespace" and call its methods, but I have never done anything like that. Thanks
The C# compiler does not support include files. You can of course use a pre-compiler, that would read your source code files, and insert the included files into them, before passing the new files to the compiler.
As for producing a class library, sure. You can do it in two ways:
- You can make the project source code available, so that they would add the project with all its files into their own project, and thus compile your class library as part of their solution.
- You can compile it yourself, which produces an assembly file on disk (YourClassLibrary.dll), and then give that file to your co-workers. They would then add a reference to the file and can start using it.
You can't, except by using some external preprocessor. You could use T4, which is already used by Microsoft as a text templating tool, e.g. for code generation with the Entity Framework; see this MSDN documentation page.
With T4, you would include another source file like this:
<#@ include file="c:\test.txt" #>
Now to the more fundamental point: Including source files is not how you usually create libraries, especially not in .NET. What you would do is define one or more types containing the functionality—there are no global functions in C#, the closest you can get are static methods. You would then compile this and give the resulting assembly (.dll
file) to your colleagues. (Or you create a NuGet package and publish it to a source that your colleagues can access.) They will be able to load it as a reference into their .NET project and use your class.
public static class MyFunctions
{
public static void MyMethod1(...) { ... }
public static int MyMethod2(...) { ... }
...
}
(Of course such code is more procedural than good object-oriented design, but this may or may not be an issue for you.)
No, not in that way. But you can create a class library and put all classes they need to use there. They simply add a reference to that dll from their project and import the namespace, voila, they can use the classes.
精彩评论