How to add "System" as reference in my file csproj - c#

I have this error:
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference)
And my .csproj file contains:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>
I created the project with the command "dotnet new console"
Would like to know if it is possible, how to generate this (not manually) but with the help of "dotnet"

Related

How to use nuget library SimpleHelpers.FileEncoding on c# on VScode

dotnet add package SimpleHelpers.FileEncoding
SimpleHelpers.FileEncoding
dotnet run
it shows:
C:\Users\user\Downloads\c2\Program.cs(1,7): Error CS0246: The type or namespace name 'SimpleHelpers' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\user\Downloads\c2\c2.csproj]
Program.cs:
using SimpleHelpers.FileEncoding;
namespace c2;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
var encoding = FileEncoding.DetectFileEncoding (#"D:\b\big5\LanTingJiXu_big5.txt");
}
}
c2.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SimpleHelpers.FileEncoding" Version="1.4.0" />
</ItemGroup>
</Project>
update the error message.
It looks like this package was authored to use the content folder with a .cs.pp file transform that were previously rendered and copied to the project folder for projects using packages.config to reference NuGet packages.
This is no longer supported for SDK-style projects which use PackageReference. The package author would need to switch to (or add) contentFiles to support PackageReference or create a pre-built binary (.dll file).

MSTest unit test project added to C# solution causes existing project build to fail VS2022

Im using VS2022 to create a .NET Core 3.1 C# class library (DeletemeClassLib.csproj) with no added content. Solution and Project are in the same folder. This builds as expected.
When I add a MSTest project to the existing solution using .NET Core 3.1, the MSTest project will build without errors, but the DeletemeClassLib.csproj build fails with errors like:
Error CS0246 The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?) DeletemeClassLib C:\Users...\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs 5 Active
The build errors appear to point to my UnitTest1.cs file (in the MSUnitTest.csproj project), but they only occur when building the separate DeletemClassLib.cs project. I also tried the same thing using nunit instead of MSTest, but I found similar results.
My DeletemeClassLib.csproj has no packages listed under the project. My MSUnitTest1 project lists coverlet.collector 1.2.0, Microsoft.NET.Test.Sdk 16.5.0, MSTest.TestAdapter 2.1.0, and MSTest.TestFramework 2.1.0.
DeletemeClassLib.csproj
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
</Project>
MSUnitTests1.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>
</Project>
Class1.cs (main project file in DeletemeClassLib.cs project):
using System;
namespace DeletemeClassLib
{
public class Class1
{
}
}
MSUnitTest1.cs (Main MSTest unit test file):
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MSUnitTests1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}
Build Output with username hidden:
1>------ Build started: Project: DeletemeClassLib, Configuration: Debug Any CPU ------
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(14,12,14,54): error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(15,12,15,60): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(16,12,16,58): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(17,12,17,67): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(18,12,18,54): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(19,12,19,52): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(20,12,20,54): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(1,17,1,29): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(5,6,5,15): error CS0246: The type or namespace name 'TestClassAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(5,6,5,15): error CS0246: The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(8,10,8,20): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(8,10,8,20): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?)
1>Done building project "DeletemeClassLib.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
Any ideas on what might cause the DeletemeClassLib.csproj build to fail when the MSUnitTest1.csproj is present?
If I move MSUnitTest1.csproj to a completely separate folder, everything builds correctly. However, I am trying to retrofit an older Github solution (this example is a simple representation of the other project) to add unit tests, and I can't add a unit test folder above the git parent to separate the unit tests and main project into separate folders as I would like to.
Thanks

How to reference a csproj using csc?

Using csc how can I reference another project's source?
If I have my own .csproj for a hefty project that depends on another, and am compiling the dependent with dotnet build rather than csc I can add
<ItemGroup>
<ProjectReference Include="..\MyAPI\MyAPI.csproj" />
</ItemGroup>
To the csproj file, but what about in the case of writing a single .cs file?
csc test.cs /reference:../MyAPI/MyAPI.csproj
doesn't work because
error CS0009: Metadata file '/Users/theonlygusti/MyAPI/MyAPI.csproj' could not be opened -- PE image doesn't contain managed metadata.
test.cs(2,7): error CS0246: The type or namespace name 'MyAPI' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'Specflow_xxx_XUnitAssemblyFixture' could not be found (are you missing a using directive or an assembly reference?)

After I installed this nuget package in my specflow demo C# .NET Core 3.1 project, I got this error:
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name
'Specflow_Demo_XUnitAssemblyFixture' could not be found (are you
missing a using directive or an assembly
reference?) Specflow.Demo ...\Specflow.Demo\Features\LoggedInDiscount.feature.cs\LoggedInDiscount.feature 3 Active
This is my csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.9.22" />
<PackageReference Include="SpecFlow.xUnit" Version="3.9.22" />
<PackageReference Include="SpecRun.SpecFlow" Version="3.9.7" />
</ItemGroup>
</Project>
After I close all the files in Visual Studio and rebuild, I got another error:
There is an open issue with SpecFlow related to this: Type or namespace [TestNamespace]_XunitAssemblyFixture could not be found. Are you still able to run your test cases? The above link says its an intellisense only issue.
From a comment on the GitHub issue discussion:
The generated assemblyFixture file (e.g.: [TestNamespace]_XunitAssemblyFixture) can be found in the obj folder (obj\Debug\targetframework\xUnit.AssemblyHooks.cs).
We realized that this is IntelliSense related "issue" (IntelliSense of VisualStudio itself, not R#).
As you all noticed, the build was successful, but the error was still there. This is because the generated code behind file (*.feature.cs) is opened in your editor. If you close them, and build again, the error will disappear.
...
BTW there is an ongoing PR which will move the generated files to the obj folder, so this problem will disappear when it'll be merged.
I had a quick look through the types in the referenced nuget packages. None of them uses the naming style that you have there 'Specflow_Demo_XUnitAssemblyFixture' - and none of them has a type with a similar name.
So, as the error says, the type doesnt exist.

Why do .Net5 projects have a reference to Microsoft.VisualBasic?

The following code can be compiled and run, without having an explicit reference to a Microsoft.VisualBasic dll or package:
using Microsoft.VisualBasic;
Interaction.Beep(); // from Microsoft.VisualBasic
System.Console.WriteLine("Done");
Here the corresponding project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
Look under Dependencies > Frameworks in the Solution Explorer and you'll see Microsoft.NETCore.App listed. Expand that node and you'll see all the assemblies included in that framework, which includes Microsoft.CSharp, Microsoft.VisualBasic and Microsoft.VisualBasic.Core. That means that VB projects have a reference to Microsoft.CSharp as well.

Categories

Resources