Is Resharper confused by Aliases used for Assemblies in Using directives? - office-interop

I noticed this in my .cs file, which looks like I might be "mixing metaphors" or at least being redundant by having both "Microsoft.Office.Interop" and "Microsoft.Office.Interop.Excel" in my list of usings directives:
using System.Runtime.InteropServices;
using MSExcel = Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
Resharper, when Inspecting for Code Issues in Solution, normally flags unused using directives, but in this case it peeped not a peep regarding "using MSExcel = Microsoft.Office.Interop;" even though when I commented it out to see what would happen, the solution compiles fine - so it has fooled Resharper, I guess. So does this mean this is a known "hole in Resharper's swing" and I should suspect the necessity of any aliased using directives?

By default, this option in R# Options > C# > Namespace Imports is enabled:
Do not remove using alias directives if alias name differs from the imported type name
Because if the alias name is non-trivial you may have a reason outside of R#'s knowledge to have it.
Disabling the option will show the directive as redundant.

Related

Is there a way in visual studio 2022 to add a new line before and after namespace?

I am using Visual Stuido 2022 to code my C# project.
Is there a way to configure VS using (.editorconfig file) where a new line is added before and after the namespace?
So my class will look like this
using System;
namespace ProjectName.Tests;
public class Test
{
}
instead of
using System;
namespace ProjectName.Tests;
public class Test
{
}
I'm not sure there is a Visual Studio native way of doing this.
There is definitely not a way to do this in .editorconfig with Visual Studio alone (meaning no plugins). About halfway down Namespace declaration preferences, it talks about csharp_style_namespace_declarations, and the code formatting sample when that value is file_scoped looks like
// csharp_style_namespace_declarations = file_scoped
using System;
namespace Convention;
class C
{
}
which appears to get you part of the way there (blank line after using). When you look at the supported formatting rules, the list is pretty brief.
If you have ReSharper there is a way. These settings in .editorconfig will do what you want:
resharper_blank_lines_after_file_scoped_namespace_directive = 1
resharper_blank_lines_after_imports = 1
If ReSharper is not an option, here are 3 possible paths to take, none all that great. They certainly aren't simple solutions.
try to find something in the Visual Studio Marketplace
write a .NET Analyzer that is configured via .editorconfig (ref. this page)
raise an issue on Developer Community, and hope they get to it.
Like the other guy said,
No, not with editor config (out of the box).
Resharper can do this; you can build a custom format for your code and tell it, for example, where you want certain sections like imports below the class for some weird reason ;) ...of course, with that spacing around those sections.
But now you have to get ReSharper licenses for all your devs. The Resharper devs are very cool, and Jetbrains has been building quality software for a few decades. Maybe I'm sentimental.
Well, there is another option if you don't want to pay...
In DevOps roles of the past, I've used Roslyn to build extensions for custom formatting.
Build a custom Rosyln analyzer.
https://learn.microsoft.com/en-us/visualstudio/code-quality/roslyn-analyzers-overview?view=vs-2022#severity-levels-of-analyzers
You can build a custom code suggestion with this of configurable severity.
It's pretty easy, too, thanks to Rosyln, but you must think a little backward. Let me know if I can help more.
StyleCop.Analyzers has a bunch of formatting related rules that you can enforce and, in some cases, automatically fix across your codebase.
Unfortunately it looks as though there is not yet support for file-scoped namespace formatting in the way you want. There's a PR to add it here, so if you really wanted to have this you could get that PR across the finish line, or make your own build:
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512
This issue might also be worth a look:
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3578

How can I find all usages of a namespace and its members?

I am trying to search a large solution for any usages of a given namespace or its members, say, System.IO.
Resharper's Find Usages allows me to find references to System.IO, but only when used explicitly. If a file declares using System.IO; then uses any of the namespace's members without the full name, only the using reference is found. I need to find any usage of any member of the namespace in each file as well.
Can this be accomplished in Visual Studio, Resharper, or any other plugin do this search for me?
Place cursor on keyword using of line using System.IO. Then you can use the normal Find Usages (Shift+F12).
This works with ReSharper only.
As of ReSharper 2019.2.2 (2019/08/28), a solution wide Show Usages of Symbol feature still does not exist.
Workaround:
Search Everywhere for namespace: Ctrl+T, then enter using System.IO
Save results in new windows: Alt+Enter
You may wish to pin the window so that it stays open.
For each result, Show Usages of Symbol:
Place cursor on the using keyword.
Ctrl+Shift+Alt+F12
This is definitely not ideal, but it is a systematic way to get the job done.
Update: October 2, 2019
JetBrains has confirmed that this feature is not currently supported by ReSharper, but they are considering adding it to the road map.
Feature Request: solution wide "Show Usages of Symbol"
Update: October 17, 2019
JetBrains has just confirmed that they have added this feature to ReSharper, and it will be available as of 2019.3 EAP1.
See: Feature Request: solution wide "Show Usages of Symbol"
To Show Usages of Symbol, use the Find Dependent Code option which will enable you to:
look for project references
look for NuGet packages
search at project level
search at solution level

ASP.Net Library name conflict between Ektron app_code and library

I've got an Ektron 8.2 site, and I was trying to integrate Quartz.NET into it, in order to run some scheduling. Quartz.NET requires a library Common.Logging. This library introduces a conflict and breaks the Ektron code in App_Code/VBCode.
E.g. the following code from Utilities.vb
Case Is = Common.EkEnumeration.FolderType.Community
imageURL &= "images/ui/icons/folderCommunity.png"
Case Common.EkEnumeration.FolderType.Catalog
imageURL &= "images/ui/icons/folderGreen.png"
now gives a compile time error-
App_Code\VBCode\Utilities.vb(703,0): error BC30456: 'EkEnumeration' is not a member of 'Common'.
It appears that Common.Logging is conflicting with Ektron.Cms.Common (the Ektron files have a Imports Ektron.Cms statement). Is it possible to specify the priority on libraries? Or namespace an imported library?
Update
The Utilities.vb code is written by Ektron. I'd like to either make no changes to this code, or minimal changes, as any changes would need to be re-done upon Ektron upgrades. This is really a clash between 2 libraries - Ektron and Quartz.Net. Can I resolve this clash without changing the code of either library? Is there a configuration setting for aliasing libraries?
A simple solution is to use the full namespace, Ektron.Cms.Common.EkEnumeration, rather than relying on the include to sort things out automatically.
I.e.
Case Is = Ektron.Cms.Common.EkEnumeration...
Not elegant, but should get you working again.
Another alternative is to use a namespace alias:
using EkCommon = Ektron.Cms.Common;
So your code would instead look like:
EkCommon.EkEnumeration.FolderType.Community

Intellisense error / including additional libraries with environment variables

I'm working on a C++ project (VS 2010) using CPLEX.
I have included the required cplex libraries in the project settings as follows:
added the "additional include directories" under C/C++ > general
added the "additional library directories" under linker > general
added the .lib files as "additional dependecies" under linker > input
Everything compiles fine, however my problem is that intellisense still reports errors (red squiggly underlines) such as "cannot open source file" in the #include line, and "identifier undefined" when using variable types defined in the CPLEX library.
The only difference with other projects that don't have this behaviour is that this time I have used windows environment variables in setting the include path, i.e. the library directories and include directories are defined similar to: %CPLEX_STUDIO_DIR%\cplex\include
I have set it like this so that I can build this project on different machines without messing around in the project properties. Since CPLEX is installed separately, using relative paths to specify additional directories is not really an option.
I'd like to repeat that the project compiles, it's only the intellisense errors that are bothering me.
I know I can turn off the intellisense error reporting, but if someone has a workaround for this I'd love to hear about it.
Kind regards,
This problem is known to Microsoft:
http://connect.microsoft.com/VisualStudio/feedback/details/779874/intellisense-cant-handle-using-environemt-variable-in-include-path
The only way I know of to avoid it is to manually enter the full literal path. The bug is marked "deferred" which I think means Microsoft has regarded it not serious enough to be fixed soon.
The solution is to replace %CPLEX_STUDIO_DIR%\cplex\include with $(CPLEX_STUDIO_DIR)\cplex\include. This is the syntax Visual Studio uses for its built-in variables such as $(VCInstallDir), but it also works for environment variables (so long as they don't have the same name as a built-in variable) and is recognised by intellisense as well as the compiler.
(I realise this is an old question, but it ranks highly in search results so it could help others even if not the original poster.)

IntelliSense in .fsx and dynamically loaded assemblies

When dynamically referencing assemblies in .fsx using #I and #r, VS highlights the following usages of imported types and writes "The namespace or module 'XXX' is not defined". Is it ok?
For example, in the following code
#I #".\Tools\FAKE"
#r "FakeLib.dll"
open Fake
Target "Hello" (fun _ -> trace "hello!")
Run "Hello"
VS highlights Fake and says "The namespace or module 'Fake' is not defined", it also highlights Target and Run. I have this problem in VS 2010 SP1 and in VS 11 CTP. This issue makes writing F# scripts a bit harder task than it could be.
By the way, IntelliSence for "common" types works well. FakeLib.dll and FakeLib.xml are present in the .\Tools\FAKE directory. The code runs well.
Update 1
Here's the screenshot of the situation. You can see that VS resolves assembly FakeLib correctly (in a tooltip), and that at the same time Intellisense "see" standard types (tooltip over Console) class.
Update 2
I think there's something wrong with the IntelliSense on my work workstation, because it (IntelliSense) behaves itself quite strange. When I start VS and open .fsx file, IntelliSense refuses to resolve even standard types/classes, and it doesn't underlines FAKE classes, it does nothing. But after some period of time and some manipulation over code IntelliSense starts working for standard types and underlines FAKE types.
Moreover, when I've created the similar code on my home workstation there were no problems, IntelliSense works as it should.
If you put the full path into the #r directive, you'll get full IntelliSense. The #I directive, while convenient, prevents IntelliSense from working properly though the code will run just fine. This is true for both loose scripts and those found in projects. Have a look at Tomas Petricek's [FSharp.AsyncExtensions](http://github.com/tpetricek/FSharp.AsyncExtensions] project.
No, that's not how it works. Does your code run? Intellisense is provided for assemblies referenced using #r.

Resources