Visual Studio snippet - How to convert first letter to lowercase - visual-studio

In Visual Studio, I would like to update the fullprop snipped to automatically convert the field name according to property name: (and so only enter "property name" & "type")
public string MyTest
{
get => _myTest;
set
{
if (_myTest == value) return;
_myTest = value;
RaisePropertyChanged();
}
}
private string _myTest;
I can't find a way to convert MyProperty in myProperty.
I found this topic which seems to work with VSCode but it doesn't work in Visual Studio. Snippet in VSCode seems to be different from Visual Studio as $1$ works in visual studio but ${1} doesn't work. So is there an equivalent way in Visual Studio ?
Thanks for your help !

Related

How to activate C# 10 features Visual Studio 2022

I try to use the newest C# 10 features in Visual Studio 2022 Preview 3. The compiler does not recognize the new keywords required or field. global using seems to work.
public required string Name { get; init; }
public DateTime HiredDate{ get; init => field = value.Date(); }
Null parameter checking doesn't compile:
public void NullParameterCheck(string arg!!) { ... }
I also tried to set the language version to preview in the .csproj:
<LangVersion>preview</LangVersion>
Is there any setting I missed?
Finally I found part of the solution. I have to add
<LangVersion>preview</LangVersion>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
to the .csproj file. Null parameter checks work, but not required and field.
this is what I use in .csproj file:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>preview</LangVersion>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<GenerateRequiresPreviewFeaturesAttribute>true</GenerateRequiresPreviewFeaturesAttribute>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Typewriter relative directory

I'm using the Typewriter extension for Visual Studio and Visual Studio 2017. The following is the Typescript Template file.
${
using System.IO;
Template(Settings settings)
{
settings.IncludeProject("ProjectName");
}
string Test(Class c)
{
//return Path.GetDirectoryName(Path.GetDirectoryName( System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ))); // > file:\C:\Users\[user]\AppData\Local
//return Environment.CurrentDirectory; // > C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE
return String.Join(", ", Directory.GetFiles("relative\path")); // > System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\relative\path'.
}
}
$Classes(ProjectName.Models.*)[
export class $Name {
$Properties[
public $Name: $Type;]
// $Test
}]
When using Directory.GetFiles the path is relative to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE not relative to the template file or the project. How can I get relative files?
P.S. Not sure if this is specific to Typewriter or Visual Studio Extensions in general.
Edit
I have since tried DTE:
using EnvDTE;
DTE dte = (DTE)GetService(typeof(DTE));
string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);
But I get The type or namespace name 'EnvDTE' could not be found (are you missing a using directive or an assembly reference?) and i don't how to reference an assembly within an existing extension. If I could use DTE that would resolve the initial question.
The $Parent property of the class is a $File and this has a $FullName property which is the absolute file name. I am using this to get the filenames of the interface:
string FileName(Interface #interface)
{
return (#interface.Parent as File).FullName;
}
Maybe from there you can figure out the path to the template file?
Thanks to the comments above I was able to pull this off with the following:
#reference EnvDTE
#reference Microsoft.VisualStudio.Shell.15.0
${
Template(Settings settings)
{
EnvDTE.DTE dte = (EnvDTE.DTE)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE));
var fileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(dte.Solution.FullName), "myfile.txt");
}
}

How to encapsulate User Setting (Options Page) in Visual Studio 2010 AddIn

I'm currently developping a Visual Studio Extension and I have a question about Options Page. Options Page allows user to save setting about your Extension. Visual Studio handle a lot of work for us.
I created the Options Page.
public class VisualStudioParameter : DialogPage
{
private string _tfsServerUrl = DefaultParameter.TfsServerUrl;
[Category("TFS Parameters")]
[DisplayName(#"Server Name")]
[Description("The URL of your TFS Server")]
public string TfsServerUrl
{
get { return _tfsServerUrl; }
set { _tfsServerUrl = value; }
}
}
First, I created a method in the Visual Studio Package to acces to the Options Page.
Okay so now, from my Package, I can easily acces to the settings.
partial class SpecFlowTfsLinkerExtensionPackage : Package : IParameter
{
....
....
public string GetTfsServerUrl()
{
return ((VisualStudioParameter) GetDialogPage(typeof (VisualStudioParameter))).TfsServerUrl;
}
}
Now, I want to be able, in another library (Another project, included in the VSIX Package), to get easily these values. I don't want to reference the Visual Studio AddIn Package in my library.
I also have Unit Test so I'm going to create an Interface. During Unit Test, I going to Mock the object.
public interface IParameter
{
string GetTfsServerUrl();
}
Do you have any idea about how I can develop a clean solution to get these parameters from another assembly ?
Do you think the better solution is to inject the AddIn dependency in my library ?
If you already developed a Visual Studio Extension, How did you encapsulated the user setting from your core assembly ?
Thanks a lot.
You can try something like that:
// Access DTE infrastructure
EnvDTE.DTE dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
// Access options page
var props = dte.get_Properties(#"Your Extension", "General");
var pathProperty = props.Item("TfsServerUrl");
path = pathProperty.Value as string;

creating and using DLLs in visual studio c#

I have created my first dll using c# and visual studio 2010. I am trying to use it in a program. The dll is in the program directory but visual studio will not allow using myDLL stating that it could not be found. I have also tried adding it as a reference in the solution explorer. What more do I need to do?
Here is one of the files from my class.
namespace nbt
{
class TAG_Long
{
private string name;
private long payload;
public TAG_Long(FileStream f)
{
name = NameTag.SetTagName(f);
byte[] buffer = new byte[(int)dataBytes.TYPE_LONG];
f.Read(buffer, 0, (int)dataBytes.TYPE_LONG);
Array.Reverse(buffer);
payload = BitConverter.ToInt64(buffer, 0);
}
}
}
Try putting your class in a namespace

Setting a registry value based on dialog in a visual studio setup project

I have visual studio setup project with a custom RadioButtons dialog.
How do I get it to write the value of the ButtonProperty in the registry after it is selected in the UI?
If using a .Net Installer class do the following:
Pipe the data through to your Custom Action using CustomActionData eg: If your property is called MYPROP: /MyVar=[MYPROP]
You can now access the data from your installer class:
protected override void OnAfterInstall(IDictionary savedState) {
string myVar = Context.Parameters["MyVar"];
RegistryKey key = Registry.LocalMachine;
using (key = key.CreateSubKey(#"SOFTWARE\MyCompany\MyApp")) {
key.SetValue("MyVar", myvar);
key.Close();
}
}

Resources