Set interface to public by default in C# - c#

Is there any way for Visual Studio to create my interface 'public'? For example, right click on folder -> create new item -> code -> interface.
Whenever the file is created there are no access modifiers.
interface IMyInterface
{
}
Is there any way to default it to make it public?
public interface IMyInterface
{
}
I'm always forgetting to manually change them to public (when they need to be).

Open your project with the public IMyInterface interface shown above.
Go to File > Export Template.
Follow the steps (make sure you choose item template rather than Project template) and save it under a name of your choice.
When you restart visual studio and have a project open it will be available from the add item dialog. You will be able to set the name of the interface from the dialog just like you can with the other items.
Note that this does not overwrite the default interface template installed with Visual Studio so you can still just as easily make a private interface when required.

You can also create a code snippet for public interfaces like this :
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>public interface</Title>
</Header>
<Snippet>
<Code Language="csharp">
<![CDATA[
public interface IMyInterface
{
}
]]>
</Code>
</Snippet>
</CodeSnippet>
and save it as publicinterfacecsharp.snippet.
Then go to Tools -> Code Snippets Manager...
Select language as C# and My Code Snippets folder, then click Import.. and point to location where you saved the snippet. Now in any new project, you can right click in the editor window and select Insert Code Snippet -> My Code Snippets -> publicinterfacecsharp.

Related

How can I configure default VS Code namespaces for C#?

How can I configure default VS Code namespaces for C#?
E.g. I have the following folders structure:
gameplay/input/controllers/foo.cs
I created the foo.cs by right clicking the controllers folder and selecting New C# Class. And here is how the class looks by default without any changes from my side:
namespace play.input.controllers
{
public class foo
{
}
}
While I would expect the namespace in this case to be gameplay.input.controllers.
I am confused why would VS Code change the gameplay to play? How could I fix it?
UPDATE
I can not find the RootNamespace in my project:
So, the provided answer has no value for me.
You can set the default namespace of a project in Project Preferences->Application. The string that is visible in the "Default Namespace" input box is used for new files in the root directory of the project. If you create items in subfolders, these will be included in the namespace by default.
If you want to remove the application name (play in your case), you can define the default namespace as empty.
You can also edit the project file to include the RootNamespace tag, as follows:
<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp2.1</TargetFrameworks>
<EnableDefaultItems>false</EnableDefaultItems>
....
<RootNamespace>MyRootNamespace</RootNamespace>
....
</PropertyGroup>

C# XML documentation '<include>' tag not appearing in intellisense?

I developing a C# class library in Visual Studio, and I have been making use of XML Documentation Comments primarily for their integration with Intellisense. However, the bulk of comments has become quite cluttered, so now I am endeavoring to use the <include> tag, and an external XML document to reduce the clutter.
My issue is that when using the <include> tag Intellisense seems to not update with the information, not show any of the <summary> and <param> tags that I've assigned to some of my classes and methods.
For Example I could have a class 'Test' documented as shown:
/// <include file="docs.xml" path='extradoc/class[#name="Test"]/*' />
class Test { string foo = "bar"; }
And have docs.xml:
<?xml version="1.0" encoding="utf-8" ?>
<extradoc>
<class name="Test">
<summary>
Contains some Foo.
</summary>
</class>
</extradoc>
And upon build the output XML populates correctly:
<?xml version="1.0"?>
<doc>
<assembly>
<name>Example Program</name>
</assembly>
<members>
<member name="T:Example_Program.Program.Test">
<summary>
Contains some Foo.
</summary>
</member>
</members>
</doc>
The only issue is that, try as I might, this documentation will not appear in the intellisense boxes while appending my code. Is there some Visual Studio configuration setting I'm missing? I've scoured the msn documentation to no avail.
My issue is that when using the tag Intellisense seems to
not update with the information, not show any of the and
tags that I've assigned to some of my classes and methods.
1.Avoid that your issue is being not able to see summary in Intellisense in current project A.
You can get help from this document, this technology is used to provide better reading experience. So assuming you have the Test class in current priject A, when you see the content in VS code editor, you'll see something like:
It's expected behavior that you won't see that rich comments in project A any more cause they have been moved to docs.xml.
2.If you mean when you create a new Project B(or share the assembly to other developers), the Intellisense can't recognize your Test class.
Two possible causes:
1.The output xx.dll and xx.xml from project A are not in the same folder, so when you reference that xx.dll in your new project, Intellisense won't display the documentation comments.
2.I guess there's something wrong with your docs.xml file. (I can't find any official document which indicates this technology supports user-defined nodes like extradoc and class in docs.xml, I used these two nodes and the Intellisense did not work, after changing them to normal docs and members, it works now)
Try using docs.xml and include in this way:
<?xml version="1.0" encoding="utf-8" ?>
<docs>
<members name="MyTests">
<Test>
<summary>
This class is public, but do nothing
</summary>
<remarks>
Just write something here to indicate this is remarks.
</remarks>
</Test>
</members>
</docs>
and
/// <include file="docs.xml" path='docs/members[#name="MyTests"]/Test/*' />
public class Test { }
I suggest you use a public class to test... After that create a new project and reference that xx.dll, when calling Test class you can see the summary:
And if we F12 we can see detailed comments:
Hope it helps :)

Visual Studio 2017 quick actions remove internal

I have a question about VS2017 quick actions. How can i remove the internal tag from the auto generated property with quick actions ?
Close your VS and navigate to
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC#\Snippets\1033\Visual C#
Look for a file named prop.snippet
Open the file with any text editor i.e. notepad
Look for the following line of code:
<Code Language="csharp"><![CDATA[public $type$ $property$ { get; internal set; }$end$]]>
and change it to:
<Code Language="csharp"><![CDATA[public $type$ $property$ { get; set; }$end$]]>
Please note this will effect every instance of VS. A better approach would be to create your own code snippet and import it in.
Please refer to Walkthrough: Create a code snippet for more info on creating custom code snippets.
In another way, you can use code snippets to create another template.

Monogame XML serialization with Content pipeline

I've been trying to implement an XML system for items in my game.. but I just cant get it to work.
I am using Monogame and the content pipeline that comes with it.
I've made an Inventory Class and an Item Class.
Here are the snippets from the Inventory Class (which would have the serialization):
public class Inventory
{
[XmlElement("Item")]
public static List<Item> itemList;
public Inventory (Vector2 _position)
{
itemList = new List<Item>();
}
public void LoadContent()
{
XmlSerializer deserializer = new XmlSerializer(typeof(Item));
TextReader reader = new StreamReader("Content/Items/itemEntities.xml");
object obj = deserializer.Deserialize(reader);
Inventory XmlData = (Inventory)obj;
reader.Close();
}
}
And then I creaded an XML file:
<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:ns="Microsoft.Xna.Framework">
<Asset Type="Game.Item[]">
<Item>
<itemType>Weapon</itemType>
<itemRarity>Rare</itemRarity>
<itemID>0001</itemID>
<positionID>
<X>1</X>
<Y>1</Y>
</positionID>
<name>The sword</name>
<description>Description</description>
</Item>
<Item>
<itemType>Equipment</itemType>
<itemRarity>Uncommon</itemRarity>
<itemID>0002</itemID>
<positionID>
<X>1</X>
<Y>1</Y>
</positionID>
<name>The Item</name>
<description>Description</description>
</Item>
<Item>
<itemType>Drone</itemType>
<itemRarity>Common</itemRarity>
<itemID>0003</itemID>
<positionID>
<X>1</X>
<Y>1</Y>
</positionID>
<name>The Drone</name>
<description>Description</description>
</Item>
</Asset>
</XnaContent>
The problem now is that I get the following error:
error: Importer 'XmlImporter' had unexpected failure!
Microsoft.Xna.Framework.Content.Pipeline.InvalidContentException: Could not resolve type 'Game.Item[]'.
I have read on some other questions here that I would have to make a reference, but I just can't find a way to make that. In the solution explorer I can see references, but when I click add, I dont see anything in Projects, only standard stuff like Frameworks etc. are there.
Oh and I have a constuctor with no arguments in Item class, so that should be fine.
One more thing. I have tried to write the list of items into an XML file and it worked perfectly..
Thanks in advance!
You need to add a reference to a compiled dll lib, which means, your types need to be defined in an external project.
Create another project that compiles into dll, define all your structs and classes there (at least those that need serialization), and add reference to the dll from the content manager by doing the following:
open content pipeline manager.
select root node ("Content").
On the properties tab click on "References".
Click on 'Add' and select the path to the dll file you generated from the other project.
I recently asked a similar question on the monogame community and currently its impossible to serialize types from your own project into / from xml without using another project that builds into a dll.

ReSharper 9 Adding menu item action not working

Trying to update my resharper extension to work for 9.0, before I was just moving the dll into the plugins directory but now I need to figure out how to get nuget to work... I've been able to package the files, dll gets included in the nupkg but I think I have some namespace\id something something issues(not very familiar with .net) and it doesn't seem as if my actions.xml is even being read by resharper when I import the nuget package. The menu item isn't being added. Anwyays if anyone can give me any sort of advice on how to debug a nuget package or what might be going wrong would really really appreciate as I've been stuck on this for a few days now.
Actions.xml
<?xml version="1.0" encoding="utf-8" ?>
<actions>
<action id="yuval" text="L10N"></action>
<insert group-id="ReSharper" position="last">
<action-ref id="yuval" text="About Localization Helper"/>
</insert>
</actions>
AboutAction.cs
namespace JetBrains.Resharper.L10N
{
[Action(Id)]
public class AboutAction : IExecutableAction
{
public const string Id = "yuval";
public bool Update(IDataContext context, ActionPresentation presentation, DelegateUpdate nextUpdate)
{
return true;
}
public void Execute(IDataContext context, DelegateExecute nextExecute)
{
MessageBox.ShowMessageBox(
"Localization Helper\nYuval\n\nHelps Localize",
"About Localization Helper",
MbButton.MB_OK,
MbIcon.MB_ICONASTERISK);
}
}
}
nuget spec
<?xml version="1.0"?>
<package >
<metadata>
<id>JetBrains.Resharper.L10N</id>
<version>1.0.0.7</version>
<title>L10N</title>
<authors>Yuval</authors>
<owners>UW</owners>
<licenseUrl>https://myurl.com</licenseUrl>
<projectUrl>https://myurl.com</projectUrl>
<iconUrl>https://myurl.com/logo.png</iconUrl>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<description>Tool to help localize</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2015</copyright>
<tags></tags>
<dependencies>
<dependency id="Wave" version="[1.0]" />
</dependencies>
</metadata>
<files>
<file src="..\bin\Debug\JetBrains.Resharper.L10N.dll"
target="dotFiles\"/>
</files>
</package>
The way actions are registered has changed in ReSharper 9. It's no longer done with actions.xml, but with interfaces on your action class. For example, to add an action to the ReSharper → Tools menu, you would do:
[Action(ActionId, Id = 1)]
public class AboutAction : IExecutableAction, IInsertLast<ToolsMenu>
{
public const string ActionId = "yuval";
// …
}
You also need to specify a unique value for Id. As of 9.1, this needs to be unique within your own extension (9.0 required it to be unique across the whole installation, including ReSharper itself and any other extensions).
Whenever you change the attributes or interfaces of an action, the extension needs to be reinstalled via nupkg (the actions are statically registered with Visual Studio, in the same way as a standard VS extension), but if just the implementation has changed, you can copy the dlls to the install folder, either manually, or via a small change to the .csproj.
You also need to make sure you've defined a ZoneMarker class. This declares that your action belongs to a zone, which is used to enable/disable functionality based on installed features and the current host (e.g. so Visual Studio specific extensions only work in VS and don't get loaded into dotPeek, etc.). You can find out more about Zones in the devguide, with this page providing useful info for defining a zone marker.
This thread should help, too.
Also, it's probably a good idea to name you dll and nupkg something other than JetBrains.ReSharper.(Whatever) to prevent any potential clashes with official dlls, and to prevent confusion as to where the dll comes from. The first part of the name is supposed to be your company's name (or personal name).

Categories

Resources