I am unable to view the debug information when using a Task of Tuple. E.G. When a breakpoint his hit, I cannot view any variables on hover, in the local window, or in the watch window.
The repro is just to create a new WPF app, add System.ValueTuple, add this code to MainWindow.xaml.cs, and then set breakpoints at both lines with "return".
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var task1 = TaskWithLocalDebugInfo();
var task2 = TaskWithoutLocalDebugInfo();
}
private async Task<bool> TaskWithLocalDebugInfo()
{
var viewableInLocalWindowAndHover = true;
return viewableInLocalWindowAndHover;
}
private async Task<(bool, bool)> TaskWithoutLocalDebugInfo()
{
var notViewableInLocalWindowAndHover = true;
return (notViewableInLocalWindowAndHover, notViewableInLocalWindowAndHover);
}
}
}
Edit: If I add the un-viewable local variable to watch, I get: error CS8182: Predefined type 'ValueTuple`2' must be a struct.
It's a bug in the current version of Visual Studio 2017. It has been fixed and will be out in the next quarterly release.
See the GitHub issue and the comment from the MS employee saying it's fixed.
In the meantime, from the GitHub comment on Apr 13, 2017:
i can confirm that the bug repros with ValueTuple 4.3.0, but not with 4.3.0-preview1-24530-04.
You can install the "preview" version via the NuGet Package Manager/Manage NuGet Packages for Solution interface. Just select 4.3.0-preview1-24530-04 from the "Version:" dropdown and click "Install".
One hopes that, after the next update for Visual Studio, using the "preview" version of the package won't be necessary. As noted by the previously mentioned comment, it's not clear why using the "preview" version of the package avoids triggering the bug. But obviously it's preferable to be able to use the latest "stable" version of the package if one can; who knows what changes happened since the "preview" version that would lead to some other hard-to-diagnose bug, in the debugger or otherwise.
After seeing the error in watch window, I remembered that there is a new ValueTask in C# 7. So I added package system.threading.tasks.extensions and changed the broken method to use ValueTask instead of Task
private static async ValueTask<(bool, bool)> TaskWithoutLocalDebugInfo()
{
var notViewableInLocalWindowAndHover = true;
return (notViewableInLocalWindowAndHover, notViewableInLocalWindowAndHover);
}
I can now see the debug information.
Related
I have my library with one file that contains:
public class SomeClassWithAwait
{
public async Task<int> MethodAsync()
{
return await Task.Run(() => 1 + 1);
}
}
I turn on the feature "Show compiler-generated Code".
I see changes in "Assembly Explorer", for example - state machine for await. But I cannot see changes in the file. I see only async\await as before when I turn on feature from step 2.
What I tried: close\open file, close\open dotPeek, rebuild library in release\debug mode.
Version dotPeek: 2020.2.2 build on 2020-09-09 (latest for today).
Any ideas?
UPDATED
About "Use sources from symbol files when available" feature:
Still doesn't work for me, I got this:
public async Task<int> MethodAsync()
{
int num = await Task.Run<int>((Func<int>) (() => 2));
return num;
}
So, I still have async/await.
Uncheck "Use sources from symbol files when available". Then close the window with the code and reopen it.
Try to open the decompiled source:
Right click on a class -> Decompiled Sources
dotPeek will show the await/async state machine instead of the "async/await" statements.
dotPeek Screenshot
See also the official documentation: https://www.jetbrains.com/help/decompiler/Navigation_and_Search__Navigate_from_Here__Decompiled_Code.html
I'm trying to create simple extension for Visual Studio for Mac which will handle the moment when user saves the document (sample project is on GitHub, right here).
Here's how looks my implementation of ICommandHandler<SaveCommandArgs>:
[Export(typeof(ICommandHandler))]
[Name(nameof(SaveCommandHandler))]
[ContentType(StandardContentTypeNames.Code)]
[TextViewRole(PredefinedTextViewRoles.PrimaryDocument)]
public class SaveCommandHandler : ICommandHandler<SaveCommandArgs>
{
public string DisplayName => nameof(SaveCommandHandler);
private readonly IEditorCommandHandlerServiceFactory _editorCommandHandlerServiceFactory;
[ImportingConstructor]
public SaveCommandHandler(IEditorCommandHandlerServiceFactory editorCommandHandlerServiceFactory)
{
_editorCommandHandlerServiceFactory = editorCommandHandlerServiceFactory;
}
public bool ExecuteCommand(SaveCommandArgs args, CommandExecutionContext executionContext)
{
try
{
var service = _editorCommandHandlerServiceFactory.GetService(args.TextView);
Debug.WriteLine($"I am executing something on save with {service.GetType()}");
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
return true;
}
public CommandState GetCommandState(SaveCommandArgs args)
{
return CommandState.Available;
}
}
Good point: system call the constructor of this handler, when you start editing the first file
Bad point: ExecuteCommand method never called, as well as GetCommandState method
Any ideas why it doesn't work?
I was trying to do all the things according to the documentation on official wiki in github project for visual studio api
Unfortunately, there're only samples for quick info and autocomplete features. No samples for ICommandHandler-s, haven't found any similar projects for Visual Studio for Mac as well
I agree the examples since Microsoft Visual Studio SDK documentation is horrible with no good examples of ICommandHandler.
I tried your exact code in visual studio 2022 on Windows 10 and the ExecuteCommand function is called.
So your code is fine.
I think the core problem here is that the ICommandHandler interface structure is somewhat newer and Microsoft did a poor job of properly checking for custom command handlers and adding calls.
I am trying to do a similar thing where I add an ICommandHandler to intercept the GoToDefinition command using an ICommandHandler, using this very similar code I can't get the ExecuteCommand function to fire ever.
I have been able to intercept commands using Microsofts older, yuckier DTE or DTE2 interface.
//provider constructor code
var dte2 = (DTE2)Package.GetGlobalService(typeof(DTE));
dte2.Events.CommandEvents.BeforeExecute += CommandEvents_BeforeExecute;
private static void CommandEvents_BeforeExecute(string guid, int id, object customIn, object customOut, ref bool cancelDefault)
{ //All events fired here use, the guid you want is likely
// Microsoft.VisualStudio.VSConstants.CMDSETID.StandardCommandSet97_string
// With an ID defined in
// Microsoft.VisualStudio.VSConstants.VSStd97CmdID
Debug.WriteLine("CommandEvents_BeforeExecute1 " + String.Format(
"dte2 GUID: {0}\nID: {1}\nIn: {2}\nOut: {3}",
guid, id, customIn, customOut));
}
I'd like to create a solution that controls the project references in C# projects. Ideally, this solution is IDE-agnostic so that it can be used with Visual Studio, Jetbrains Rider, or even VS Code. The reason for this is that I've seen solutions that are completely messed up due to people creating almost arbitrary project references. It's super hard to get them straight after a project has grown to a certain size.
I know that Visual Studio Enterprise offers this out-of-the-box. Unfortunately, in my current company we do not have VS Enterprise. Thus, I want to create that on my own.
So what would be the best way to do it? After doing some research I think leveraging the .NET Compiler Platform ("Roslyn") with its Workspace API might be a good idea? Seems like I could deploy it as a NuGet package which can then be used in any IDE or build-automation. But maybe there's an easier or better way, I'd like to hear your opinion on that before I start digging into it.
Also: if the "Roslyn"-way is the right one is there some good resources on how to create an analyzer that works with the Workspace APIs?
Thanks in advance.
In your analyser, register a compilation start action:
public override void Initialize(AnalysisContext context)
{
context.RegisterCompilationStartAction(Initialize);
}
private void Initialize(CompilationStartAnalysisContext context)
{
var compilation = context.Compilation;
}
From that compilation object, you have various options:
var referencedAssemblyNames = compilation.ReferencedAssemblyNames;
or
var references = compilation.References;
Then do your analysis. To report diagnostics, register an action on the CompilationStartAnalysisContext using context.RegisterCompilationEndAction.
If you don't need to look at actual project content for your analysis, you can simply use RegisterCompilationAction as follows:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class TooManyReferencesAnalyzer : DiagnosticAnalyzer
{
private static DiagnosticDescriptor TooManyReferences { get; } =
new DiagnosticDescriptor(
"DEMO",
"Don't use too many references",
"The project '{0}' has {1} references",
category: "Maintainability",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(TooManyReferences);
public override void Initialize(AnalysisContext context)
{
context.RegisterCompilationAction(AnalyzeCompilation);
}
private void AnalyzeCompilation(CompilationAnalysisContext context)
{
var compilation = context.Compilation;
int referenceCount = compilation.References.Count();
if (referenceCount > 5)
{
context.ReportDiagnostic(
Diagnostic.Create(
TooManyReferences,
null,
compilation.AssemblyName,
referenceCount));
}
}
}
I want to create simple toast notification to action center in windows 10 from this example. But I got problem on Step 2:
using Windows.UI.Notifications;
It`s missing. But I have spent a lot of time to find it and got no result. I really have no idea where I can find or at least download it.
What I tried:
After long search I found Windows.UI.dll in C:\Windows\System32 but when I try to add it as reference into project I got this error. Even after I tried to copy it and made this fully accessible nothing changed
I tried to reinstall .Net (I`m using 4.5.2)
Installed Windows 10 SDK
Tried to import with global
Added
<PropertyGroup>
<TargetPlatformVersion>10.0</TargetPlatformVersion>
</PropertyGroup>
Added System.Runtime.dll reference
Example code which probably is useless for you:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.QueryStringDotNET;
using Windows.UI.Notifications;
namespace MessagerClient.Notifications {
class DefaultWindowsNotification {
public static void notificationTest() {
string title = "Andrew sent you a picture";
string content = "Check this out, Happy Canyon in Utah!";
string image = "http://blogs.msdn.com/something.jpg";
string logo = "ms-appdata:///local/Andrew.jpg";
ToastVisual visual = new ToastVisual() {
BindingGeneric = new ToastBindingGeneric() {
Children =
{
new AdaptiveText()
{
Text = title
},
new AdaptiveText()
{
Text = content
},
new AdaptiveImage()
{
Source = image
}
},
AppLogoOverride = new ToastGenericAppLogo() {
Source = logo,
HintCrop = ToastGenericAppLogoCrop.Circle
}
}
};
Console.WriteLine("NOTIFICATION");
//Can`t use because of Windows.UI library
ToastNotificationManager.CreateToastNotifier().Show(visual);
}
}
}
You have to fight Visual Studio pretty hard to use these UWP contracts in a Winforms app. You got off on the wrong foot right away with the wrong TargetPlatformVersion, pretty hard to recover from that. Full steps to take:
Edit the .csproj file with a text editor, Notepad will do. Insert this:
<PropertyGroup>
<TargetPlatformVersion>10.0.10586</TargetPlatformVersion>
</PropertyGroup>
Which assumes you have the 10586 SDK version installed on your machine. Current right now, these versions change quickly. Double-check by looking in the C:\Program Files (x86)\Windows Kits\10\Include with Explorer, you see the installed versions listed in that directory.
Open the Winforms project, use Project > Add Reference > Windows tab > tick the Windows.Data and the Windows.UI contract. Add Reference again and use the Browse tab to select System.Runtime. I picked the one in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\ .NETFramework\v4.6.1\Facades. This reference displays with a warning icon, not sure what it is trying to say but it doesn't appear to have any side-effects.
Test it by dropping a button on the form, double-click to add the Click event handler. The most basic code:
using Windows.UI.Notifications;
...
private void button1_Click(object sender, EventArgs e) {
var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var text = xml.GetElementsByTagName("text");
text[0].AppendChild(xml.CreateTextNode("Hello world"));
var toast = new ToastNotification(xml);
ToastNotificationManager.CreateToastNotifier("anythinggoeshere").Show(toast);
}
Embellish by using a different ToastTemplateType to add an image or more lines of text. Do keep in mind that your program can only work on a Win10 machine.
If anyone should happen to stumble on this, see this similar but newer post -
Toast Notifications in Win Forms .NET 4.5
Read Stepan Hakobyan's comment at the bottom.
Essentially, I'm seeing the same thing. This code runs, I can step through it line by line with no exceptions but the toast notification is never shown within a Form app.
Im writing a policy plugin for VS which checks several issues with the code. If an issue occurs it will be displayed in the policy warnings tab. Now I want to jump to the line where the issue occurs in the editor when I double click it in the policy warning tab. How can I do that?
namespace PolicyPlugin
{
[Serializable]
public class MyPolicyPlugin : PolicyBase
{
//...
//called if the user clicks on a policy warning
public override void Activate(PolicyFailure failure)
{
// make jump to line x
}
}
}
Thanks!
You could try to get DTE automation object first:
EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
or use alternative ways to get it.
An then execute standard command (that's what happens when you press CTRL+G in Visual Studio)
DTE.ExecuteCommand("Edit.Goto", "1234")
Note: I'm not sure about exact ExecuteCommand method signature. Also you can manipulate IDE the same way for other commands.