Refactoring "Implement Interface" using auto properties VS2015 - c#

I am trying to get Visual Studio 2015 (14.0) to use auto properties when implementing an interface using refactoring for C#.
I.e. I want this;
public object SomeProperty { get; set; }
as opposed to this;
public object SomeProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
I have accomplished this in past versions of Visual Studio by editing the code snippet file (instructions here) but I cannot get this to work using Visual Studio 2015.

Ok, so I stumbled upon the answer during my testing of VS2019 Preview (16.0).
In the main menu bar Tools --> Options --> Text Editor --> C# --> Advanced look for the option Implement Interface or Abstract Class under When generating properties choose prefer auto properties.
This results in the same outcome that snippets used to take care of pre VS2015.

You can solve by editing the PropertyStub.snippet
Just go to C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Refactoring open PropertyStub.snippet and edit:
$GetterAccessibility$ get
{
$end$throw new $Exception$();
}
$SetterAccessibility$ set
{
throw new $Exception$();
}
to
$GetterAccessibility$ get;
$SetterAccessibility$ set;

Related

No C# Code Analysis in Visual Studio Code

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)

Create a helplink attribute for a .net library to display in visual studio where my library is added as a reference

I have created a small library in c# and want to display a help url like
class HelpURL : Attribute
{
string url;
public HelpURL(string url)
{
this.url = url;
}
}
[HelpURL("www.example.com")]
public func() {
...
}
It works inside my library project but i dont know how to display it in another project where my library is added as a reference? How can i do that?
Edit I tried using XML comments but dont see any changes.
I recommend you use Visual Studio's built-in intellisense features instead. All you need to do is add XML documentation comments to your code, and it will automatically be visible in your other projects.

How to bind the "publish version" property in visual studio WPF

When I'm using the publish option in Visual Studio to create an .*exe package of my application, I can change the Publish Version and it starts from 1.0.0.0.
Every time I'm changing something and re-publish it, the version number is increment automatically, and I want to bind that property to a TextBlock in my WPF.
How can I do that?
Thank you.
First of all you have to reference in your project to System.Deployment.dll than you can try this code:
public string PublishVersion
{
get
{
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
Version ver = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
return string.Format("{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision);
}
else
{
return "Not Published";
}
}
}
Remember that will be work only when you have installed your app.

Visual Studio Architecture Code Generation - Create List<type> rather than IEnumerable<type>

Is it possible to configure an Association in a Visual Studio (2013) class diagram so that when the code is generated from it that it creates a property with type List<MyClass> or even ICollection<MyClass> rather than it's default of IEnumerable<MyClass>?
Yes, it is possible to change the output. Visual Studio uses T4 templates to generate code from the Architecture tools.
You can find the templates at C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Architecture Tools\Extensibility\Templates\Text (remove (x86) if you have a 32-bit machine).
Use the following steps to change the generated code to IList<T> instead of the default IEnumerable<T>:
Back up all templates to a different directory on your machine (better be safe than sorry)
Open CSharpHelper.t4 from the above directory
Locate the method named ElementType(IType type, bool isEnumerable = false)
private static string ElementType(IType type, bool isEnumerable = false)
{
string text = string.Empty;
if (type == null)
{
text = "object";
}
else
{
text = TypeName(type);
}
if(!string.IsNullOrWhiteSpace(text) && isEnumerable)
{
//SO Change IEnumerable to IList here
text = "IEnumerable<" + text + ">";
}
return text;
}
Change the string IEnumerable to whatever you want (see my comment starting with SO)
Save the T4 file and generate your code from visual studio
You can even write your own T4 Templates and instruct visual studio to use them when generating code, more details on MSDN.

Receive Test Run start/finish with DTE2 interface in Visual Studio extension

is there a way to subscribe to Test Explorer events in visual studio extension?
I didn't find anything like that in DTE2 interface. My goal is to trigger some function from extension when Test run completed (for the test that were ran from Test Explorer)
Thank you!
Thanks 280Z28 for your answer. Working code by using application object DTE:
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.TestWindow.Extensibility;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TestTools.Execution;
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
Microsoft.VisualStudio.OLE.Interop.IServiceProvider InteropServiceProvider = application as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
_ServiceProvider = new ServiceProvider(InteropServiceProvider);
_ComponentModel = (IComponentModel)_ServiceProvider.GetService(typeof(SComponentModel));
_OperationState = _ComponentModel.GetService<IOperationState>();
_OperationState.StateChanged += _OperationState_StateChanged;
}
void _OperationState_StateChanged(object sender, OperationStateChangedEventArgs e)
{
}
It is also possible to access currently discovered test by ITestsService.
_TestsService = _ComponentModel.GetService<Microsoft.VisualStudio.TestWindow.Extensibility.ITestsService>();
var GetTestTask = _TestsService.GetTests();
GetTestTask.ContinueWith(Task =>
{
var DiscoveredTests = Task.Results.ToList();
});
The interfaces you need are available through MEF in the Microsoft.VisualStudio.TestWindow.Interfaces.dll assembly.
You need to expose your extension through MEF and [Import] an instance of IOperationState, or use the IComponentModel interface (returned for the SComponentModel service) to access the IOperationState. From there, you want to add an event handler to the IOperationState.StateChanged event, and look for the State property to include the TestOperationStates.TestExecutionFinished flag.
I'm terribly sorry for the lack of links, but I couldn't find any information about this in MSDN.
Edit: Two remarks about compatibility.
This is only available in Visual Studio 2012 and newer.
The necessary assembly (mentioned above) has a different strong name in the two versions of Visual Studio, and there is no bindingRedirect in Visual Studio 2013. What this means is you will be forced to deploy separate extensions for Visual Studio 2012 and Visual Studio 2013, or get "clever" about the way you dynamically load your extension code (the latter is way beyond the scope of this answer, but I've used it for some cases like Inheritance Margin extension that requires access to version-specific IntelliSense resources).
Sample VS 2017
A sample using MEF and the ITestContainerDiscoverer exported type. But be aware this may be gone in VS 2019!
[Export(typeof(ITestContainerDiscoverer))]
[Export(typeof(Testything))]
internal class Testything : ITestContainerDiscoverer
{
[ImportingConstructor]
internal Testything([Import(typeof(IOperationState))]IOperationState operationState)
{
operationState.StateChanged += OperationState_StateChanged;
}
public Uri ExecutorUri => new Uri("executor://PrestoCoverageExecutor/v1");
public IEnumerable<ITestContainer> TestContainers
{
get
{
return new ITestContainer[0].AsEnumerable();
}
}
public event EventHandler TestContainersUpdated;
private void OperationState_StateChanged(object sender, OperationStateChangedEventArgs e)
{
if (e.State == TestOperationStates.TestExecutionFinished)
{
var s = e.Operation;
}
}
}
Some more things could be found here
https://www.fuget.org/packages/Microsoft.VisualStudio.TestWindow.Interfaces/

Categories

Resources