C# compiler - getting verbose output or diagnostic info - c#

Is there an option to the C# compiler CSC.exe which generates more verbose output, specifically details on its parsing logic?
I've got a C# source file in a project which essentially contains this:
namespace lib.data.files
{
public class group
{
public file[] file;
}
public class file
{
}
}
And a compile error of
files.cs(5,15): error CS1031: Type expected [lib.data.csproj::TargetFramework=netstandard2.0]
The problem can be fixed by changing line 5 to
public lib.data.files.file[] file;
But I'm curious to find out what the CSC compiler thinks file[] represents (if it isn't picking up the class referrence in the same namespace).
(p.s. adding <ErrorLog>compiler-diagnostics.sarif</ErrorLog> to the project file only gives the same level of detail.)
(p.p.s. this compile error only showed up after upgrading from .NET 6.0 to .NET 7.0)

Related

Error CS0840 in MSBuild but not in VS2015

I have the following code:
namespace NS{
public class ClassName{
public PropertyName{get;}
}
}
I get the following error:
TestFile.cs(11,32): error CS0840: 'NS.ClassName.PropertyName.get' must
declare a body because it is not marked abstract or extern.
Automatically implemented properties must define both get and set
accessors.
When compiled in VS2015 everything is working good. When trying to build using MSBuild the error happens.
I am compiling against .NET 4.6.2 with C# 6.0 and ToolsVersion 14.0.
What am I missing?
What is almost certainly happening here is that your version of MSBuild is old and is compiling against version 5 of C#. Consider this code:
public class Foo
{
public Foo()
{
Bar = 1;
}
public int Bar { get; }
}
This will compile happily in C# 6 (i.e. VS2015) but will throw the error you experience from MSBuild (and also if you used VS2013.)
If you want to compile with MSBuild then you need to download and install the updated build tools: https://www.microsoft.com/en-us/download/details.aspx?id=48159

How can I get an auto-generated file to recognize a method it created itself?

I have a *.tt file (two, actually, but they are acting similarly, so I'll just talk about one).
I have them set just like another project, where they work fine. Their properties are set the same, such as:
CustomTool = TetxtTemplatingFilePreprocessor
When I select "Run Custom Tool", a corresponding *.cs file is created:
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 10.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
But the project won't compile, because this code in the auto-generated file (FormTemplate.cs):
FormTemplate formTemplate = new FormTemplate(POST, this);
output.Write(formTemplate.TransformText());
...won't compile. The err is, "'QuizModule.QuizModuleWebPart.Templates.FormTemplate' does not contain a definition for 'TransformText' and no extension method 'TransformText' accepting a first argument of type 'QuizModule.QuizModuleWebPart.Templates.FormTemplate' could be found"
How can it generate the code, and then not be able to find a method it references? Actually, it did generate the method, and it is right there in FormTemplate.cs:
#line 1
"C:\Projects\QuizModule_Test\QuizModule_Test\QuizModuleWebPart\Templates\FormTem
plate.tt"
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "10.0.0.0")]
public partial class FormTemplate : FormTemplateBase
{
{
public virtual string TransformText()
{
Why would it be that it cannot see its own method?
UPDATE
This turned out to be one of those "my bads" that produces seemingly in[s]ane err msgs. What happened was, some of my namespaces were wrong - the code had been copied over verbatim from another project, and the namespaces had not been updated. Once those were rectified, the project compiled just fine.
CAUSE
This turned out to be one of those "my bads" that produces seemingly in[s]ane err msgs.
SOLUTION
What happened was, some of my namespaces were wrong - the code had been copied over verbatim from another project, and the namespaces had not been updated. Once those were rectified, the project compiled just fine.

Compiler error when trying to access public const field in C# assembly from C++/CLI

I have a C# project that declares the following class :
public static class CPConstants
{
public const double FAR = double.PositiveInfinity;
...
}
I have a separate C++/CLI project that references the above C# assembly and tries to do this in a method of a class:
double farValue = CPConstants::FAR;
When I compile this (I'm using MS Build with Visual Studio 2008), I get the following compiler errors:
error C2589: ';' : illegal token on right side of '::'
error C2059: syntax error : '::'
Can anyone explain what I am doing wrong here? How can I access the 'FAR' field from my C++/CLI project?
There is a #define FAR in the Windows headers for legacy reasons. Try using this line between the header inclusions and your actual code:
#undef FAR

Unable to compile code output because of some reference assembly goofups

My issue goes like this:
There is a project called myframework. It has some extension methods defined in it as follows:
namespace myframework
{
public static class Helpers
{
public static bool ContainsAll(this string obj, string[])
{
return true;
}
}
}
It also has some other stuff like interfaces, etc, etc.
There is a second class I generate via System.CodeDom classes. The generated output is somewhat like this:
using myframework;
public class A: IMyFrameworkInterface
{
public void foo()
{
string s ="HELLO";
if(s.ContainsAll(some_arr))
return;
}
//More methods defined...
}
The compiler options I pass which is created prior to the actual compile call references the correct assemblies
var cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("myframework.dll");
The code compilation modules are written in a different project. The particular class responsible for this also nicely gives us access to a list of CompilerError object via which we can learn the result of compilation.
Issue1: When I tried this in an asp.net project the compiler threw error saying it could not find metadata file myframework.dll (despite it being referenced in the project).
Issue2: When I tried it with a windows forms project. It gave a different error. This time saying that string does not contain definition for ContainsAll()
How to solve these two specific problems?
Found out the answer to this after a bit digging up. I was using .net framework 3.5. The codedom compiler apis targets v2.0 of the framework by default. Hence, you have to manually specify the correct framework:
var cp = new CompilerParameters(
new Dictionary<string,string>() { {"CompilerVersion", "v3.5"} });
For the compilation to work within an asp.net environment you'd have to actually point the references to the correct location. Hence you'd have to do something like follows:
cp.ReferencedAssemblies.Add(
HttpContext.Current.Server.MapPath(
"bin\\myframework.dll"));
My references:
http://blogs.msdn.com/b/lukeh/archive/2007/07/11/c-3-0-and-codedom.aspx
.Net 3.5 CodeDom Compiler generating odd errors
And comments in the question's post. :)

F# declared namespace is not available in the c# project or visible through the object browser

F# declared namespace is not available in the c# project or visible through the object browser.
I have built a normal F# library project, but even after i build the project and reference it to my C# project, I am unable to access the desired namespace.
I am also unable to see it in the object browser, i get an error telling me that it has not been built. I am running on the september release can someone point out my error ?
F# Version 1.9.6.0
(6) Edit : Referencing the dll directly has fixed my problem, referencing the project allows me to compile but the intellisence does not work. When the dll is directly referenced the intellisence works perfectly.
This is the code found in the .fs file
#light
namespace Soilsiu.Core
module public Process =
open System.Xml.Linq
let private xname (tag:string) = XName.Get(tag)
let private tagUrl (tag:XElement) = let attribute = tag.Attribute(xname "href")
attribute.Value
let Bookmarks(xmlFile:string) =
let xml = XDocument.Load(xmlFile)
xml.Elements <| xname "A" |> Seq.map(tagUrl)
let PrintBookmarks (xmlFile:string) =
let list = Bookmarks(xmlFile)
list |> Seq.iter(fun u -> printfn "%s" u)
(5) Edit : Could ReSharper 4.0 be the problem?
(4) Edit : When i say the Object browser is unable to read the resulting assembly, i mean that when I try to open the assembly in the object browser i get an error telling me the project has not yet been built. yet again i can read the assembly using reflector.
(3) Edit : Reflector can Disassemble the dll but the Object Browser is unable to read it.
(2) Edit : I have Upgraded my F# version to 1.9.6.2 and still the same consequence
(1) Edit : I was able to Disassemble the dll to C# I get : (Everything seems to be fine here)
namespace Soilsiu.Core
{
[CompilationMapping(7)]
public static class Crawler
[CompilationMapping(7)]
public static class Process
}
[CompilationMapping(7)]
public static class Process
{
// Methods
static Process();
public static IEnumerable<string> Bookmarks(string xmlFile);
public static void PrintBookmarks(string xmlFile);
internal static string tagUrl(XElement tag);
internal static XName xname(string tag);
// Nested Types
[Serializable]
internal class clo#13 : FastFunc<XElement, string>
{
// Methods
public clo#13();
public override string Invoke(XElement tag#9);
}
[Serializable]
internal class clo#17 : FastFunc<string, Unit>
{
// Methods
public clo#17();
public override Unit Invoke(string u);
}
}
What if you reference the produced DLL directly (i.e., not via a project reference, but via a file reference)?
Maybe IntelliSense is just messed up? What compiler error do you get when you try to use it in C#? When you say "the object browser is unable to read it" what does that mean?
For what it's worth, I added this to a F# library project, referenced it (project) from a C# console app, and was able to use it. IntelliSense did not work at first though. (Had to rebuild.)
If you can make a solid repro, I'd suggest emailing it to F# bugs alias (fsbugs).
I tried the same thing. It looks as if Visual Studio and Resharper 4.0 doesn't understand F# for some reason. If you ignore the sea of red text and the lack of intellisense, it will compile fine.
Try
Make sure that C# project is targeted FULL .NET (NOT Client Profile).
Add references to assemblies into C# project which are used by F# project.

Categories

Resources