No C# Code Analysis in Visual Studio Code - c#

I'm currently using VS Code for C# development because I find it to be much more lightweight than Visual Studio itself, but there doesn't seem to be any code analysis tools available. This is a problem as I am not getting any warnings about unused functions (I am able to get warnings about unused local variables, however).
Some sample code:
namespace MyProject
{
// No warning for this unused class.
public class Dog
{
private int _age;
private string _name;
// No warning for this unused field.
private string _furColor;
public Dog(int age, string name)
{
// This unused variable generates a warning as expected.
int unusedVariable = 2;
_age = age;
_name = name;
}
// No warning for this unused private function.
private int Age()
{
return _age;
}
// No warning for this unused public function.
public string Name()
{
return _name;
}
}
}
I already have the official C# extension by Microsoft installed, but it doesn't seem to help. I can't find any code analysis extensions in the VS Code Marketplace either, and searching online only points me to extensions for Visual Studio. I am also aware that FxCop can be installed via NuGet, but it seems to only be for Visual Studio as well.
Are there any C# code analysis tools that can be used with VS Code? Even external tools or dotnet commands would help.

Special thanks to Martheen for suggesting this link.
All I had to do was add the following to my VS Code settings to enable better code analysis:
"omnisharp.enableRoslynAnalyzers": true // Enable C# code analysis.

This might be obvious, but I actually screwed it up:
You have to "enable" VS Code inside of your Unity Project
https://vionixstudio.com/2021/11/02/unity-visual-studio-code/
Open Unity Project
Edit --> Preferences --> (Analysis) External Tools
Change "External Script Editor" to VS Code
Click on "Regenerate project files"
If you click on a script this should open VS Code and the code completion should work (at least as long as everything else is set up, worked for me)

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

No entry sign in visual studio

Does anybody know what this sign means?
You're looking at a Debug Watch or Quick Watch Window similar to this one:
The stop sign is added when the member you're watching is marked as internal. Each access modifier has its own indicator. As you can see from the picture and the code that belongs to it:
static void Main(string[] args)
{
var test = new Test();
// put breakpoint here
}
public class Test : TestBase
{
internal int SomeNumber;
protected int FooNumber;
}
public abstract class TestBase
{
internal int AbstractInternalSomeInt;
public int OtherInt;
private byte SomeByte;
}
Notice that in the class view and in the Solution Explorer the symbol for internal members is different, it shows a heart instead:
I came across this post when searching for "visual studio solution explorer no entry symbol". In my case the symbol was presented when using the file view of solution explorer rather than class view. It turned out this was because my gitignore file had an exclusion to all files "*.userprefs" and I'd added project called "userprefs" - what are the odds!
Easy way to fix - right click the files in solution explorer and choose "Add ignored file to source control". Would be worth checking that this makes the desired change to the .gitignore file
In files view, I think it indicates the file is excluded from the build.
Maybe (as in my case) the file is directly included into something that is part of the build.
Look at properties > Excluded from build

Adding Windows Azure Caching crashes Visual Studio 2012 with datacachefactory argument null exception

I've added the Windows Azure Cache 1.8.0 nuget package to my solution, but it ends up crashing Visual studio when I load the project. I've found that I can "prevent" the crashing by removing the dlls from the bin folder, and then again when visual studio adds them back to the bin while the project loads.
The dlls I'm removing are:
Microsoft.ApplicationServer.Caching.AzureClientHelper.dll
Microsoft.ApplicationServer.Caching.AzureCommon.dll
Microsoft.ApplicationServer.Caching.Client.dll
Microsoft.ApplicationServer.Caching.Core.dll
When I look at the event viewer for the visual studio crash I get this:
Application: devenv.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentNullException
Stack:
at System.Threading.Monitor.Enter(System.Object)
at Microsoft.ApplicationServer.Caching.DataCacheFactory.Close()
at Microsoft.ApplicationServer.Caching.DataCacheFactory.Finalize()
I'm uncertain why VS is doing things with the dlls while the project is loading, but I admit I'm not an expert on that.
I've basically followed the process described on this page to add a dedicated cache worker role for caching:
http://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/
I've tried removing and reinstalling the package, removing and reinstalling the Visual Studio SDK (Oct 2012), but the problem comes back.
Also, I don't have the App Fabric Server installed.
Thanks in advance for your help!
In case anyone else runs into this problem, I'm providing what seemed to work for me here.
In order to get visual studio to load the project, I removed the DLLs from the project. I also removed them as the project was loading and VS put the dlls back in the bin folder.
I removed the references to the dlls. Then I removed my code that was using the datacachefactory.
In the end I believe that it was caused by an improper use of the cache in my code that I had performed a build on. I was able to correct the usage of it, build the solution and get all the dlls back into the project.
Previously by datacache object had not be static.
here's my correct usage of the datacache factory:
using System;
using Microsoft.ApplicationServer.Caching;
namespace WebRole1.Classes
{
public class AzureCache
{
public static DataCache cache { get; set; }
public static DataCacheFactory dataCacheFactory { get; set; }
public AzureCache()
{
if (cache == null){
DataCacheFactoryConfiguration cfg = new DataCacheFactoryConfiguration();
cfg.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, "CacheWorkerRole1");
dataCacheFactory = new DataCacheFactory(cfg);
cache = dataCacheFactory.GetDefaultCache();
}
}
public void Add(string item, object value)
{
cache.Add(item, value);
}
public void Add(string item, object value, TimeSpan timeout)
{
cache.Put(item, value, timeout);
}
public object Get(string item)
{
return cache.Get(item);
}
public TimeSpan TimeRemaining (string item)
{
DataCacheItem DCitem = cache.GetCacheItem(item);
return DCitem.Timeout;
}
public void Flush()
{
cache.Clear();
DataCacheFactory cacheFactory = new DataCacheFactory();
cache = cacheFactory.GetDefaultCache();
}
}
}
We may need to capture a dump of the visual studio process(devenv.exe) to see what is causing this exception. You can use debugdiag("http://www.microsoft.com/en-us/download/details.aspx?id=26798") to capture the dump.
You may need to involve Microsoft support services("http://www.windowsazure.com/en-us/support/contact/") to further investigate this since the exception is coming from the cache code itself.
It's not an issue exclusively related to Visual Studio. I get it while running my webrole.
Check out this other article.

ScriptSharp ClockLabel example with 0.6.2

I'm developing in Visual Studio 2010 and I've just downloaded and installed Script# 0.6.2 for VS 2010. I'm trying to follow the clock example in the Read Me pdf but can't get it to compile.
I've created a new Script# Class Library project inside my solution called Clock, renamed the .cs file to ClockBehaviour and added the following code as per the example:
using System;
using System.DHTML;
using ScriptFX;
using ScriptFX.UI;
namespace Clock {
public class ClockBehavior : Behavior {
private int _intervalCookie;
public ClockBehavior(DOMElement domElement, string id) : base(domElement, id) {
_intervalCookie = Window.SetInterval(OnTimer, 1000);
}
public override void Dispose() {
if (_intervalCookie != 0) {
Window.ClearInterval(_intervalCookie);
} base.Dispose();
} private void OnTimer() { DateTime dateTime = new DateTime(); DOMElement.InnerHTML = dateTime.Format("T"); }
}
}
When I try and compile the project I get errors saying that the System.DHMTL, ScriptFX and ScriptFX.UI namespaces could not be found (and some others, but I guess by fixing these errors the others will fall out).
It feels like I'm not referencing the correct projects/dlls. In the References for the project I have mscorlib and Script.Web. I've tried using the object browser find the classes (such as Behavior) in other namespaces but with no luck. I've added all of the .dlls from the ScriptSharp folder in Program Files but the namespaces still can't be found.
Any help would be very much appreciated,
Thanks,
Hugh
the sample docs are a bit out of date - look at the phot sample in the samples download : http://projects.nikhilk.net/Content/Projects/ScriptSharp/Sample.zip
See http://projects.nikhilk.net/ScriptSharp/Conceptual-What
You need to reference ssfx.Core.dll which should be installed with Script#
(Alternatively, see pp 23-24 of the pdf you linked...)

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