learning public methods of a class - c#

I'm learning a bit of C# and I know a bit of c++ right now.
When I want to learn about a class's public methods - I look in the header file in C++- which C# doesn't have. Where do I get a good summary of a class's public methods without having to look in the source file?

In the documentation, in Visual Studio using the object browser or IntelliSense or using reflection (either at runtime or using third-party tools).

There are few ways:
1) Read the documentation - .NET Framework class library or the appropriate one for used library.
2) Take a look at class in Class view
3) Use some metadata reading tool like ILDasm
4) The last in the list, but the most useful and most used is Intellisense

C# doesn't have header files, but byte code-compiled assemblies (i.e. exe and dll files) contain a section with metadata about namespace and namespace members, which is used by the runtime to discover types, methods, properties and so on. Also, Visual Studio uses it to offer its powerful intellisense.
Anyway, if you press F12 when cursor is on a method name, or any member (class, enumeration, ...), or you right-click there, Visual Studio will go to the definition of the whole member, and if such member has no source code in your machine, it will show metadata only:

Related

How to find derived class in Visual Studio 2017?

I don't have Resharper installed.. I want to see all subclasses inherited from IActionResult and there is no things like show derived classes in object browser and class view.
I try to install a extension TypeHierarchyViewer(https://marketplace.visualstudio.com/items?itemName=munyabe.TypeHierarchyViewer) but it is not working (even using the example List, just remains blank) .
So what should I do?
I use this function frequently developing java application using eclipse..It seems there is no out-of-box tool in VS2017 or I just missed some things??
(Go To Implementation says the symbol has no implementations)
(Same in class view)
I have searched many "solutions"(like Visual Studio: How do I show all classes inherited from a base class?) but not work or need other tools(or just see the doc?).
I want to see if there anyway to do it just using VS.
Before I thought VS was a very good IDE but I can't image it lacks so much basic functions...(so there is Resharper...)
And I find there is derived types but in Solution explorer:
But you can't input the class you want And if I input IActionResult in search box it will not find it(not in the my source and I don't implement it).
After trying..I find VS support it in solution explorer...
But it's hard to use...
I need to find some classes or interface in my source code related to the class or interface I want and use derived type and implements to find the it..
It looks like:
(I find a class and navigate to object, it lists all classes .Then I find ActionResult and choose implements find IActionResult, finally I can see all derived classes above... ...)
emmmmm
it seems there is no direct way using VS(although I can get all sub classes using solution explorer but it's too verbose...) to get the result I want.
Finally I choose to use dotpeek(I don't want to buy resharper because I just study ASP.net core not for work)
I open C:\Program Files\dotnet\sdk\NuGetFallbackFolder in dotpeek where the dependencies located.
Then using Hierarchies get the result.
feel a little disappointed about VS. Not so strong before I heared.
Thx for help. :)

How to get various types used in a C# project with Roslyn

I am analyzing about converting an existing C# library to java. To start with I need to know what are the types / built-in keywords used in the existing C# library. I mean, for example
public class CSharpClass
{
int i;
float j;
Console.Writeline(String.Concat("A","B"));
}
In this class the types/Keywords used are,
public
int
float
Console
String
My Questions are,
Is there any way to do this. I hope I can do this with Roslyn. But we can get “LocalDeclarationStatementSyntax” for variables. But how to parse “Console” and “Concat”. Is that “Sytax walker” that parses all the tokens in a class is the only option?
Also how to get all the classes from a project file with Roslyn?
You need to semantics as well -- syntax is just the text you see and that's exactly what you get, nothing more, nothing less. Get a Compilation for your project, then you can call GetSemanticModel where you give it a tree, and then from there you can call GetTypeInfo or GetSymbolInfo (as appropriate, search online for the difference between these two) to get type information.
As far as getting the Compilation, if you're writing a command line tool you probably want to use MSBuildWorkspace to load your project. If you're analyzing the projects open in Visual Studio, use VisualStudioWorkspace, etc.

Visual Studio Express 2013 - Create separate c# code file?

The VS 2013 Express forum doesn't seem to exist at Microsoft so I'd like to ask here..
I am using Microsoft VS Express 2013 to create a C# project. I'd like to be able to add a whatever.cs file to the project so that I can put extra functions there instead of in the default Program.cs file. Back in the old days, we could import code files in C by using a #include but C# in the Visual Studio doesn't seem to do this.
I have been able to successfully add a .cs file, create a class within it, and then instantiate the class and call it's methods from within Program.cs but I'd rather not have to instantiate a variable and have to call functions like something.MyFunction() just to execute some code that exists in another file.
Is this even possible? If not, does anybody know why? I always like the #include in C. You could keep things nice and neat.
Files added to a Visual C# project are automatically "included" in every other file within that namespace. You do not need a using statement unless you change the namespace. Because of this, there is no equivalent of the "#include" directive from C/C++.
Now to handle your use case. C# is inherently object-oriented. It is not expected that you create a million functions and call them individually (like you do in C). So, if you want to use multiple files (and you should!) you have a few options:
Create a normal class (as you have already done) and instantiate it to call its methods. This is the preferred method, and you should be able to come up with plenty of classes for your program that make sense.
Create a static class. These don't have to be instantiated (you access them like MyStaticClass.MyFunc(); ). These are often used as "helper" classes. In general, use sparingly as they are hard to unit test/dependency inject.
Mark your class as partial. This allows you to define the same class over multiple .cs files. Again, this should be used sparingly (see Jon Skeet's answer: https://stackoverflow.com/a/2895068/1783619)

How to load an existing assembly with Roslyn, transform it and generate new .cs files

It seems like the documentation around Roslyn is a bit lacking?
I am not able to find good comprehensive documentation.
What I am trying to do essentially is copy the public surface of an existing API (.dll)
into a new assembly (need to create source code .cs files!) and at the same time make a variety of tranformations to the resulting code (think making wrapper classes).
Would really appreciate any help in how I can use Rolsyn to load the initial SyntaxTree from an existing assembly and how to do those basic tranforms (for example exclude internal classes etc)
In the current Roslyn CTP there is a Roslyn.Services.MetadataAsSource namespace which can be used to convert an type's public interface to source code. This is what we implement the F12 "metadata as source" feature with. Now, it generates only a shell of source code which won't actually compile, so you'd have to use further APIs to munge the syntax tree into what you want. Alternatively, you could use the Roslyn.Services.CodeGeneration namespace to generate source from these symbols automatically. I should warn the MetadataAsSource namespace may go away in future versions of the API.
You can import symbols from metadata by creating an otherwise empty compilation with the metadata references you care about added, and then from that compilation browsing the type hierarchy from GlobalNamespace property, or calling Compilation.GetReferencedAssemblySymbol() and then digging through that. This is actually far better than using reflection, since it'll properly express the symbol model from the "C# perspective" instead of the "CLR perspective" -- reflection won't give you information for uses of dynamic, some default parameter values, etc.
It seems like the documentation around Roslyn is a bit lacking? I am not able to find good comprehensive documentation.
Roslyn is at the Community Technology Preview stage, so it's not surprising that its documentation is lacking. You can find some sources at Roslyn API documentation.
What I am trying to do essentially is copy the public surface of an existing API (.dll) into a new assembly (need to create source code .cs files!) and at the same time make a variety of transformations to the resulting code (think making wrapper classes).
Working with assemblies this way is not something Roslyn can do. But it seems for what you want, reflection for reading the assembly combined with Roslyn for writing the new code would work. But you would need to write all the code to translate from the reflection model to Roslyn's model (e.g. Type → TypeDeclarationSyntax, MethodInfo → MethodDeclarationSyntax, etc.).

Using Visual Studio DTE to analyze source code

There is an interesting coding standard in my shop that says that we don't shortcut type names with a using statement, they have to be fully qualified (so any time you reference a type, you use MyRootNamespace.ANamespace.MaybeAnotherNamespace.MyClassName, instead of just "MyClassName").
Love it or hate it, that's just how we roll, and I can't do anything about that.
Of course, you are swimming upstream with Visual Studio, because all the editing tools that generate code for you (member completion, event completion, etc) use the short type names wherever possible.
What I would like to do is to build some sort of extension or macro for Visual Studio that will correct a partial declaration, and replace it with the fully-qualified typename.
I started out trying to build a macro that would run for the symbol that your cursor is on (though I'd like to be able to scan a whole file, or maybe just intercept the code as you type).
I found that I can get members of a class with FileCodeModel2.GetElementFromPoint(), but this method will only work on class members -- it won't pick up a variable declaration inside of a method, for example.
Is there some other way I can get at that stuff? I'm currently using Macros, but would the new VS Extension model be more appropriate? I'm using Visual Studio 2010 Ultimate edition, and I only need to target c# code.
The object model allows you to do many things that are available within the IDE. But as such a feature is not available in the IDE you are out of luck here, I'm afraid.
The only thing you can do automatically with the using directives is sort them and remove the ones that are not used.
Update
As it seems it is somewhat possible to retrieve a fully qualified type name from a macro. However, it seems problematic with special cases such as generics.

Categories

Resources