In my application i am using the following code to update my .csproj.
var project = new Project();
project.Load(path);
var itemGroup = project.AddNewItemGroup();
var buildItem = itemGroup.AddNewItem("ProjectReference",#"..\Basics\Test.csproj");
buildItem.SetMetadata("Project","sf34-34hg-jgjk3-3434-99345345hh345");
buildItem.SetMetadata("Name","Test");
project.Save(path,Encoding.UTF8);
The code is working fine. But during the time of compilation. It shows a warning like Microsoft.Build.BuildEngine.Project is Obsolete which is advising to use another assembly Microsoft.Build.Evaluation.Project.
I searched for changing the code with new assembly, but didn't get much information on the same. Anyone please help me to change this code to new method.
Related
I started a new code refactoring project from the template that reverses Type names. After the renaming of some test type in a simple winforms application I want to add a file to the project so it shows under file Form1.cs like the file Form1.Designer.cs does so it is grouped like a folder. My code adds the file but does not associate it like I want, it shows up at the same level as Form1.cs. Here is the part I added after the renaming code:
// my code
var docid = DocumentId.CreateNewId(document.Project.Id);
var folders = new List<string>() { "Form1.cs" };
var sln = newSolution.AddAdditionalDocument(docid, "comment.cs",
SourceText.From("//help find parent!!"),
ImmutableArray.CreateRange(folders));
return sln;
Can anyone see what I am doing wrong? Thanks!
I'm trying to build C# projects using .csproj files.
For this I'm using the following code:
Microsoft.Build.Evaluation.Project project = new Microsoft.Build.EvaluationProject(projectFile);
bool success = project.Build();
But not all projects are build and for some I get false as the result of project.Build().
Any ideas how to understand what is going wrong?
Or maybe anyone can suggest an alternative way to compile projects using .csproj files?
You need to add an ILogger to the Build method as a parameter. I suggest implementing one as the MSDN article suggests. Just copy paste their code, add any missing references and you'll be fine.
Then you can call Build as follows:
Microsoft.Build.Evaluation.Project project = new Microsoft.Build.Evaluation.Project(projectFile);
BasicFileLogger logger = new BasicFileLogger();
logger.Parameters = logFilePath;
logger.Verbosity = LoggerVerbosity.Normal; //Increase it if you don't get enough data
bool success = project.Build(logger);
The example logger will write all data that you would see during a normal build to the file at logFilePath. Based on that you should be able to discern the issue.
I'm compiling a project using Roslyn with code resembling:
var workspace = MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync("SomeProject.csproj");
var compilation = await project.GetCompilationAsync();
I need to set a compilation symbol (such as DEBUG or TRACE, but in my case something altogether custom). How can I do this with the API?
I saw that project has a CompilationOptions property, but I didn't see anything relevant there.
EDIT Thanks to #JoshVarty who pointed towards adding code like this prior to compilation:
project = project
.WithParseOptions(((CSharpParseOptions)project.ParseOptions)
.WithPreprocessorSymbols("SOME_SYMBOL"));
I think you're looking for Project.WithParseOptions() and WithPreprocessorSymbols()
I'm trying to retrieve the setting TreatWarningsAsErrors, but I'm unable to find it for a project of my loaded solution.
What I'm trying to accomplish, is to get the setting from the project files, and set it to true, if it's not already that. Next, I want to let Roslyn do a compilation with the new setting, so I can check if this will break the project.
I've looked at various places, among others, the Project.CompilationOptions. Most options to a project build are there, except this one.
The CompilationOptions contains all the build settings, such as warning level, etc. But TreatWarningsAsErrors is not there. Am I looking at the wrong place?
The way I'm opening the solution is similar to the FormatSolution sample:
var solutionFile = #"C:\ties\src\playground\EnforceTreatAllWarningsAsErrors\EnforceTreatAllWarningsAsErrors.sln";
var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(solutionFile).Result;
var project = solution.Projects.Single();
// warning level is there
var warningLevel = project.CompilationOptions.WarningLevel;
// treat warnings as errors is not there... The following doesn't compile :(
bool treatWarningsAsErrors = project.CompilationOptions.TreatWarningsAsErrors;
You're looking for
compilationOptions.WithGeneralDiagnosticOption(ReportDiagnostic.Error)
Source
I am trying to create a Solution from a single source file and tested different solutions.
One of them is the following:
var info = ProjectInfo.Create(
projectId,
version: VersionStamp.Default,
name: "TestProject",
assemblyName: "TestProject.dll",
language: LanguageNames.CSharp);
using (var ws = new CustomWorkspace())
{
var project = ws.AddProject(info);
}
But when running this code, I just get an exception saying that "language is not supported".
Any hint about what is happening?
You need to make sure Microsoft.CodeAnalysis.Workspaces.CSharp.dll is copied alongside your project. We detect that it's there and load it to provide C# support.