VS Code: Extract interface in C# - c#

I'm using VS Code and I can't figure out if it's possible to take a class and extract an interface from it. When I google how to do this, I only find extensions for TypeScript, but I want to do this in C#.
Can VS Code extract interfaces? Knowing the shortcut would be nice for others, but I'm using a different keymap, so I'd like to know how to do this from the menu.

You may try this,
Move your cursor on the class_name that you want to extract interface. Then Ctrl + . (period)
Then choose "Extract Interface" this will generate a code above the class that you want to be extracted.
Then move your cursor the interface class_name Ctrl + . (period) again, then choose "Move type to the" auto-generated class name then that's it.
Hope this will help you guys and for future reference.

Not really sure if this feature was already there or got implemented afterwards, but I found this:
First, extract the interface as usual
Then you get to do this on your methods:
Note that it doesn't add necessary usings but uses namespaces instead. You can do the clean up as you like.

Related

C# not detecting and instantiating classes

I started writing a new C# app. I have created a couple of classes that i would like to declare and instantiate in a Form. The thing is, when i write it, the intellisense does not detect it. I will provide a screen shot, as I don't know how to better explain.
DataHelper is defined under the BuildID namespace.
You have 2 options:
The first is to declare it like this:
private BuildID.DataHelper _dataHelper //...;
The second is to add a using BuildID at the top of the page, you can either write it manually or have your cursor on the DataHelper object which is unrecognized (at the moment), press
Ctrl + . and choose the option that the Visual Studio offers you, to add the using statement automatically.
I Think to instantiate it you wanna do
"Private DataHelper data = new DataHelper();

Is there any way to edit all classes in a folder to inherit from certain base class?

I have 60 or so classes in a folder and I created a base class for those classes to inherit from.
Is there any automatic way to do this other than copy&paste method in Visual Studio?
I hoped resharper has some functionality for that but can't find one.
You can automate the cut/paste process by using the search and replace feature of Visual Studio to find all declarations, and replace them with declarations that inherit from your base class.
Press [Ctrl+H] to open Quick Replace
Enter public class {:i} in the "Find what" box
Enter public class \1 : MyBaseClass in the "Replace with:" box
Choose "Current Project" in the "Look in:" box
Check the "Use:" checkbox, and pick "Regular expressions" in the dropdown
Keep clicking [Find Next] to find the next occurrence of a class declaration. If the search highlights one of the sixty declarations that you want to modify, click [Replace], otherwise keep clicking [Find Next].
This regular expression should do the trick:
(.*)?(class .*)?(.*)?{
it match any number of characters, followed by the word 'class', followed by any number of characters, followed by a curly bracket.
Perform a replace inside all your code files, using this as replacement text:
$1 : yourBaseClassName {
This has been tested with this online regexp validator, which handles js regexps, and assumes your files do not already implement subclasses / interfaces. Raw, but can be a starting point.
You can write a tool and use it as pre build event.
That tool could be a simple silent command line executable, that writes : MyBaseClass to the right of anything that looks like: class MyClassName.
And it would take a path as argument and do it for every *.cs file it finds in there.
This is, as far as I know, the closest to what you want to achieve.
You could also try to leverage AOP (attribute oriented programming), with a tool like PostSharp (not free), but then you have to tag every class with an attribute, which defeats your purpose.

Most efficient way to move an inline class, interface, or enum to it's own file in Visual Studio

Sometimes when I'm developing I may prefer to quickly inline classes, interfaces and/or enums when I'm building a fresh design or from within a test fixture. However, I find it inconvenient to interrupt my thought process to create new code files, copy and paste the class/interface/enum written inline to the new file, and all the time it takes to navigate between them.
I'm looking for an extension, macro, or hidden shortcut combo that will automatically create a file for the highlighted or selected inline class/interface/enum, and, if possible, copy the using list so that it can be built (but remove & sort will clean it up later).
I'm open to extensions, macros, or hidden shortcut keys. Suggestions?
Edit #1: ReSharper looks awesome, yes, and it appears to have exactly what I need, but I would like to find a free solution, if it exists, that didn't push me back $200.
Edit #2: After your helpful input, I'm pushing for my dev team to all run the ReSharper trial, and re-evaluate in a few weeks if the value-add is worth it. We want keep our extension use consistent, so I'm hoping we all find it equally worthwhile.
Example: I want to turn this (IExample.cs):
using System.Linq;
public interface IExample
{
}
public class Example : IExample
{
}
public enum ExampleType
{
}
...into these:
IExample.cs:
using System.Linq;
public interface IExample
{
}
Example.cs
using System.Linq;
public class Example : IExample
{
}
ExampleType.cs
using System.Linq;
public enum ExampleType
{
}
The built in refactoring tools do not have a simple way to extract a class to its own file, so the simplest thing to do if you do not have a refactoring tool like Resharper or Refactor! Pro that do have it is to:
Copy the class to memory.
Add a new file with the class name.
Paste the class to the new file.
ReSharper offers the feature Move to another file to match type name, the ALT+Enter shortcut makes your work faster.
Devexpress Refactor!pro can be your solution....in example you posted, refactor! Pro shows a quick action menu at the bottom of the class name that you want to move.
In this Actionmenu there s the "Move to file" option that' s all you need....
EDIT:
Apparently, this method does move the type to its own file but still keeps the relationship with the previous class, so it won't actually change anything other than generate a seperate file. The type will still be nested in the same way.
This is a pretty old question and I just encountered the same issue.
In Visual Studio 2019 (and probably in 2017 as well, though I did not check),
you can select the entire class -> right click -> Quick Actions and Refactoring -> Move Type to its own file.
It correct all references to said type, too.
Very handy!

C#/.NET How do I find the containing namespace from a class name

I often find myself remembering the name of a class that I want to use, but not remembering the containing namespace.
Apart from searching the web, i wonder if a good method exists for looking this up.
I think if you press ALT, SHIFT and F10 in Visual Studio - intellisense will drop down an option for you to add the name space of the class you have just typed.
CTRL + '.' will bring up a menu where you can either add a 'using' or fully qualify the class.
You can always hang a big poster on your cube wall like me.
3.5 NameSpace
If you know the name of a class in .Net but have no idea what namespace it is in, it can be hard finding it, especially if you dont have a reference/using to the assembly containing it.
This is where the Object Browser (Ctrl+W,J) comes in handy.
Open it up, type in the name, it will give you all matches, either within your project/solution, or all of the .Net framework.
Edit:
As S.C. Madsen's comment points out, this also helps if you only remember PART of a class name, also if you only remember a method name but not the class.
Use the search function in .NET Reflector by Red Gate Software.
I generally use the offline MSDN reader, with the left panel set to the Index tab.
Another option in Visual Studio is to type the name of the type as if you were declaring a variable, and then see what it suggests. If the name goes to a light blue colour (by default) then it's in one of the namespaces you're already importing - just hover over it to find out which. Otherwise, see what namespaces it offers to add using directives for.
You can right click and select "Go To Definition" in VS and this will either load the class definiftion in your solution or it will show a metadata view of the class definition using reflection. Either of those should have the namespace defined near the top of the page.
If you need to add the namespace with a using decliration right click the unresolved class and mouse over to resolve. It will show you a list of namespaces that contain that class and selecting one will generate the using statement.
Two ways that work in Visual Studio 2013:
Right-click and select "Resolve".
Hover over the class and a 'Options to help bind the selected item' box will appear (same as Ctrl + '.' or Alt+Shift+F10)
Select the namespace and it will insert it for you.

How to exclude certain class/packages/public members from javadoc

I have created java api -ported from C# to be more specific- which aside from public interface, contains a lot of internal stuff that I don't want a user to know about. In C#, I have used doxygen to generate documentation. I presume javadoc has similar features to exclude certain public members, classes, even packages.
Would someone suggest how do that, perhaps via eclipse?
Thanks
I believe that in Eclipse, the only kind of exclusions you can specify are things like "exclude all protected members" and package-based exclusions (not class-based exclusions.)
If you're using Ant to generate them, you can use the nested "package" element, and you can use a fileset nested element and add exclusions to that fileset.
For example:
<javadoc >
<sourcefiles>
<fileset dir="${src}">
<include name="**/*.java"/>
<exclude name="**/ClassToExclude.java"/>
</fileset>
</sourcefiles>
<packageset>
<dirset dir="${src}">
<include name="com.mydomain.*"/>
<exclude name="com.mydomain.excludePackage"/>
</dirset>
</packageset>
</javadoc>
P.S. - I've used the <sourcefiles> element alot, but never the <packageset> element. The latter might not be spot-on syntactically.
Sure, when you "Generate Javadoc", you can select the package concerned by this process and exclude the others
alt text http://www.filigris.com/products/docflex_javadoc/images/eclipse_javadoc_1.png
(here the picture shows the javadoc generated with another tool, but that does not change the general idea)
You can use doxygen with Java. I am not aware of any tools that do what you want with Javadoc.
Superpackages should be coming in JDK 7 which I beleive could address this: http://blogs.oracle.com/andreas/entry/superpackages_in_jsr_294

Categories

Resources