C# compiler does not find dlls ; solution compiled

2019-08-13 20:45发布

问题:

I have a C# solution and some referenced dll-s. Even though when compiling in visual studio(vs2010) it appears as it succeeded, when using the C# compiler it fails: missing dll apparently..

csc /t:library /out:test.dll test.cs


test.cs(22,10): error CS0246: The type or namespace name
    'Attribute' could not be found (are you missing a using directive
    or an assembly reference?)

Does anyone know why is this happening?

回答1:

As you haven't given the code, it's not clear what type Attribute is meant to be. If it's System.Attribute, I'd expect that to be found automatically via the default assembly references. If it's a type in another assembly, you need to explicitly reference it from the command line:

csc /t:library /out:test.dll /r:OtherAssembly.dll test.cs


回答2:

CSC knows nothing about the project containing test.cs, nor any libraries which that project is referencing.

You have to use the /r switch in order to reference other assemblies. Note that there is a file called csc.rsp in the folder containing csc.exe, which specifies default command line switches. This contains most of the usual .NET framework assemblies, which is why you do not have to explicitly reference mscorlib.dll, for example.