This question is on behalf of one of my team members: I am a developer in charge of writing the documentation for our product. I have written a tool in C# to output our assembly in markdown style files. In order to facilitate the ease of use for our classes, I wanted to implement a way of linking the class type and property constructs to any MSDN documentation available publicly. For the most part, this was accomplished simply by using the namespace of the class like so:
msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol(v=vs.110).aspx
However, I ran into some problems when looking at classes with generic type arguments and properties. They seem to be generated in a special manner that looks like a hashed string, like so:
msdn.microsoft.com/en-us/library/b682ts2x(v=vs.110).aspx
The “b682ts2x” part of the URL is the part that is different.
I would like to know if there is any way I can get in touch with someone who knows how these links are generated, and if there is a way to generate the same exact URL portion (that is, b682ts2x) for any class property using only reflection.
As an alternative approach you could use the same syntax that the F1 help is using when you highlight a class name for example.
As mentioned in Visual Studio intercepting F1 help command
msdn.microsoft.com/query/dev11.query?
appId=Dev11IDEF1&
l=EN-US&
k=k(width);
k(vs.csseditor);
k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.0);
k(DevLang-CSS)&
rd=true
The "k" parameter above is contains the help context inside visual
studio. Help context contains both "keywords" (text strings) and
"attributes" (name/value pairs) which various windows inside Visual
Studio use to tell the IDE about what the user is doing right now.
For example here is one for System.Net.HttpHttpClient.
https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.Net.Http.HttpClient);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true
Notably when I pressed F1 when highlighting "HttpClient" it assumed I meant ServiceClient.HttpClient so be careful to provide the namespace.
Related
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. :)
As of lately I'm using quite some code generation, usually in combination with partial classes. Basically the setup is as follows:
Partial class containing generated code. Some of this code will call partial methods. The code is re-generated a lot of time. The code generator is in some cases a custom tool.
Partial methods are manually implemented in a separate file.
The problem is that when I'm using Intellisense features like "generate method", they are for some reason generated in the file containing the generated code. Obviously I don't want that.
My question is: Is it possible to generate some hint that tells Intellisense it shouldn't touch certain 'cs' files (but instead the other partial class)?
Update
In retrospect I should have noted that I'm using a custom tool to generate the code. It's not a EF or a simple transformation; there's quite a bit of logic involved in the code generation. Also, it generates a complete namespace and class structure with partial classes. The 'root namespace' is found by extracting it from the csproj file and then using the folder structure to figure out the absolute namespace (it's similar to how Linq2sql does this).
The answer suggested by xanatos (thanks!) works: intellisense sorts its operation on the name, then alphabetically sorts on the name and then picks the first item in the list. This means that you can generate a zzzz.foo.cs which (albeit a bit ugly) will work just fine. I've just ran some experiments and found out that the feature find all references returns the order that VS appears to use. As it turns out, it works like this:
Say you have a custom tool that works on the filename foo.bar and transforms it into foo.cs. The custom tool will generate the content as string and pass it back to Visual studio (that's just how custom tools work...). The result will be in a file called foo.cs.
Now, I was quite surprised to found that Intellisense does not sort it as foo.cs but rather as foo.bar\foo.cs. In other words: regardless of how you name the 'cs' output in your custom tool, you have to rename the base file foo.bar to something like zoo.bar.
While that might be a workaround, I'm hesistant to accept it as the answer, because I would have to give files in my project strange names (names have meaning...). Also, some of my custom tools have dependencies on their filenames, so that will also get broken...
Therefore, I'm still open for suggestions on how to fix this properly.
From a simple test I've done in VS2013, it seems that Visual Studio 2013 adds the method to the "first" file he finds in the Solution Explorer. So you could simply add a .something.cs to your file-name, like MyClass.generated.cs vs MyClass.cs. Be aware that the VS2013 seems to be using the "full path", with path ordering based on name. So:
Z\MyClass.cs
comes after
MyClass.generated.cs
(and Intellisense will put code in MyClass.generated.cs) even while in the Solution Explorer all the folders are ordered first.
Full example:
A\MyClass.gen3.cs
MyClass.gen2.cs
Z\MyClass.gen1.cs
This should be the order as "seen" by the Intellisense, so it will put the new classes in A\MyClass.gen3.cs.
Assuming you're talking about the EF, I always change the template file (.tt) so the filename of the auto-generated file is [classname].model.cs. This means my partial file, which by convention is called [classname].cs is alphabetically first and always seems to get picked for auto-generation.
All you have to do is find/replace all the:
fileManager.StartNewFile(entity.Name + ".cs");
With:
fileManager.StartNewFile(entity.Name + ".model.cs");
There should be 3.
This has other benefits like auto-generated files are clearly marked in the filename.
I still have no idea why they didn't do this in the first place.
If you're not talking about the EF, the same trick of using the filename to order them should work.
I would like to generate some C# code based on existing code.
More precisely I need some mappers for existing enums as well as converters, unit-tests for them.
It would be a longer discussion why I've generate this code rather than go for a generic approach, but considering that I would like to generate some classes based on some enumeration types, what options would I have?
I am just thinking about visual studio extensions, some templates or maybe resharper addins but so far I haven't done anything like this...
I would appreciate any input of those of you who had previous experience with such a task.
ReSharper supports Live Templates as a means of generating code. You can create whatever code you like in there, with editable or linked hotspots to provide customisation points (e.g. current file name, class name, time, new guids, suggested variable names, etc). You can generate code snippets in existing files, surround existing code, or create new files. ReSharper 8 also introduces support for multi-file templates, creating more than one file at a time.
However, ReSharper's templates don't support things like loops - you can't loop over an XML file and generate a class member for each element, for example. T4 would be a better solution for that.
Reegenerator supports generating code from existing code. You can create a code generator and then attach it to the file that contains the enum type. The generator will be executed everytime the file is saved.
The code generator are given access to the Visual Studio DTE Object, which will give you the code structure through CodeElement classes (CodeClass, CodeAttribute, etc). From there you can either generate a separate partial file through a T4-like template, or simply manipulate the code directly using the DTE EditPoint.
I'm working on an ASP.NET web application that uses a lot of JavaScript on the client side to allow the user to do things like drag-drop reordering of lists, looking up items to add to the list (like the suggestions in the Google search bar), deleting items from the list, etc.
I have a JavaScript "class" that I use to store each of the list items on the client side as well as information about what action the user has performed on the item (add, edit, delete, move). The only time the page is posted to the server is when the user is done, right before the page is submitted I serialize all the information about the changes that were made into JSON and store it in hidden fields on the page.
What I'm looking for is some general advice about how to build out my classes in C#. I think it might be nice to have a class in C# that matches the JavaScript one so I can just deserealize the JSON to instances of this class. It seems a bit strange though to have classes on the server side that both directly duplicate the JavaScript classes, and only exist to support the JavaScript UI implementation.
This is kind of an abstract question. I'm just looking for some guidance form others who has done similar things in terms of maintaining matching client and server side object models.
Makes perfect sense. If I were confronting this problem, I would consider using a single definitive description of the data type or class, and then generating code from that description.
The description might be a javascript source file; you could build a parser that generates the apropriate C# code from that JS. Or, it could be a C# source file, and you do the converse.
You might find more utility in describing it in RelaxNG, and then building (or finding) a generator for both C# and Javascript. In this case the RelaxNG schema would be checked into source code control, and the generated artifacts would not.
EDIT: Also there is a nascent spec called WADL, which I think would help in this regard as well. I haven't evaluated WADL. Peripherally, I am aware that it hasn't taken the world by storm, but I don't know why that is the case. There's a question on SO regarding that.
EDIT2: Given the lack of tools (WADL is apparently stillborn), if I were you I might try this tactical approach:
Use the [DataContract] attributes on your c# types and treat those as definitive.
build a tool that slurps in your C# type, from a compiled assembly and instantiates the type, by using the JsonSerializer on a sample XML JSON document, that provides, a sort of defacto "object model definition". The tool should somehow verify that the instantiated type can round-trip into equivalent JSON, maybe with a checksum or CRC on the resulting stuff.
run that tool as part of your build process.
To make this happen, you'd have to check in that "sample JSON document" into source code and you'd also have to make sure that is the form you were using in the various JS code in your app. Since Javascript is dynamic, you might also need a type verifier or something, that would run as part of jslint or some other build-time verification step, that would check your Javascript source to see that it is using your "standard" objbect model definitions.
What tools or techniques do you recommend for discovering C# extension methods in code? They're in the right namespace, but may be in any file in the solution.
Specifically:
How can I find all extension methods (and navigate to them) on the current type in the code window?
I do have Resharper (v4), so if that has a mechanism I'm not aware of - please share!
If you have the source then you can search for this Type identifier using regular expressions. Considering the fact that it has to be the first parameter to the function something like this should do the trick:
\(this:b+:i:b+:i
At least this way you can discover where the extensions methods are defined and add that namespace, then rely on intellisense. Just ran this on a non-trivial project with lots of extensions methods everywhere and it worked a treat. The only false positive was something like this:
if(this is Blah...
Which we can fix by adding static to our search since the extension methods have to be static:
static.*\(this:b+:i:b+:i
This won't work for cases like this:
public
static ExtensionMethod(
this int
iVal) {
}
But that's kind of the limitation of regular expressions. I am sure certain somebodies can tell you all about the pain of using regular expressions to parse a language.
Now, what I think is missing from the IDE is the ability to recognise the extension methods that are in a non-imported namespace. Similar to when you know the classname, if you type it up, the IDE will give you a hint to either use it explicitly or import the namespace. After all, that's how I import all my namespaces and frequently find myself trying to do the same to extension methods.
This is pretty low-tech, but how about Ctrl-F searching for "this:b+MyClassName" ?
If you are using VS which I guess you are intellisense will show all the avialable extensionmethod for a given object for you (marked with a blue thingy added to the usual instance method icon). That list might differ from file to file (a mthod called aMethod might mean two different things in two different files) eventhough the object type is the same (which is based on the way extension methods are found)
If you've got resharper, just hold down the ctrl key and click on the method.
If you have installed the ILSpy extension in Visual Studio (I am using 2022) then you can:
Right click on the class/type and select -> Open code in ILSpy
In ILSpy right click on the type and select -> Analyze
In the Analyze window you will see a node "Extension methods" (if any exists, else no node is shown)