Generate code as Windows Forms do with .designer.cs - c#

I think I have a big problem.
I have a two projects solution.
First one is UI project.
Second one in an Algorithms Service.
I need to generate (or overwrite) a class (fileUI.cs) in UI part just before compiling it.
The main idea is that when you edit a class in de Services part (fileSA.cs) and you build the solution just a moment before the class in the UI part (fileUI.cs) is updated with Services part class information (fileSA.cs), and then, compiled.
I think its similar what VS does with the .designer.cs of every form(user control...)
I dont know if its possible...
Thanks a lot

You need T4 Template Here
Code Generation and T4 Text Templates

Visual Studio has a mechanism called single-file generators / custom tools that targets exactly this use case: generating a file (fileUI.cs in your case) when another file has changed (fileSA.cs).
Refer to the MSDN article Implementing Single-File Generators to learn how such a custom file generator is written.
P.S.: While this would be the appropriate mechanism, I don't think that it will work across the project boundary.

You can add a custom msbuild target in your project (right click on project -> edit project file). See msdn for more info about msbuild targets. You can run custom tasks as part of this target. A way to generate code is using T4 templates. More info here on running t4 templates as part of the build process. Oleg Sych has a lot of detailed explanations on T4 and is kind of the authority on the matter, so his site is a good read to start.

Related

Visual Studio: Writing hints visible in other projects

Using Visual Studio 2013 and .NET I've created set of widgets which I want to use in external projects. Each class and method is well documented with the XML-style comments. For example:
///<summary>...</summary>
When I use already commented code within the same solution, the appearing prompt suggesting how to finish the line contains my remarks. Nevertheless, when I tried to generate a DLL file with the code and use it in the external project the comments were not available anymore. How can I document code to make these hints visible in other projects using the compiled DLL?
If you want to use Code Documentation when referencing dll you have to generate XML documentation. XML documentation has to be in same folder that .dll is in.

Create a general "skeleton" from a c# project

I've a very complex solution in c# containing about 20 projects, each of them control a device since it is a driver.
In many cases those projects use similar structures/code (for example everyone as a connect method, a retrive data method and so on).
Is there a tool to analyze the code and create a general "Skeleton" that can be reused?
You can easily create a project template from an existing project from the File>Export Template menu. The process is described in How To: Create Project Templates.
There is no tool that can decide what to include in a skeleton project, as this depends on knowledge of what each project actually does, which parts that can be generalized and which have to be project specific.
You can use duplicate analysis in Visual Studio or Resharper to find repeated code, but this won't tell you what should be in a template and what shouldn't.
What you can do, is:
Extract common functionality in a separate project that all device projects will reference
Create a template from one of the device projects.
Use template parameters to customize the resulting template.
Step #1 will result in a much simpler template, that is easier to customize

Does documentation for file Microsoft.CSharp.targets with default C# targets exist?

When I create C# project in Visual Studio 2010, file Microsoft.CSharp.targets is included.
Is any documentation available for it?
Which targets in it, which properties are used?
It is especially useful when editing build script manually without VS.
The file with targets could be investigated manually (what I do from time to time).
But in such case it is not clear what is a matter of changes, what is by specification and what is no.
Everytime I need something about Microsoft.CSharp.targets I found it in different places.
I have not found "one place" with all described.
Does complete reference available?
Thanks.
No specific documentation I know of, it is an implementation detail for C# projects. You can find plenty of documentation about MSBuild in the MSDN library, the Microsoft.CSharp.targets file just contains targets that are specific to building a C# project.
The most important targets it implements are Build, Clean and Rebuild. They directly correspond to the commands you find in the VS build menu. The .csproj file merely sets properties that affect the outcome of the general targets. All of this is readily available on your machine, you can look at .targets files with an editor. There's just a whole lot of it and it is isn't exactly that easy to read, the concept of XML as a programming language is a bit, well, flawed. No debugger either.

How do you add folder level documentation to C# assemblies?

Me: I'm a relative new-comer to the .NET platform.
Problem
In Java, you can add package level documentation to your project by creating a package-info.java or package.html file and storing in the package folder. How do I add equivalent documentation to my project in C# using Visual Studio 2010?
Background
I like to write documentation describing my motivations in the package/folder level context of the source code projects that I am working on. I have become very accustomed to this workflow in a variety of languages (specifically Java) and I believe that it is a good way to document my project.
C# will automatically turn the XML-based tripple-slash comments into intellisense documentation.
///<summary>This method does something!</summary>
///<parameter name="p1">The first parameter</parameter>
///<return>true, if the method completed successfully</return>
public bool DoSomething(int p1){
return p1 > 0;
}
When you compile your project into a class library and reference it in another project, the above will automatically be turned into a useful tooltip. In addition, the C# compiler can optionally produce an XML file with all of these comments alongside your DLL. This XML file can be fed into Sandcastle (as mentioned previously) and added to a documentation project as MSDN-style API reference.
The Sandcastle tool has its own project and documentation structure, so you'll want to start up a side project if you're going to add anything more than the XML-generated Intellisense reference.
The focus is a bit different in .NET, it has very good support for generating IntelliSense info. Documentation at your finger tips. I'm sure you're familiar with it when you used VS for a while, look up "xml documentation".
Off-line docs used to be covered by NDoc but the guy that supported it quit his project. The Sandcastle project took up the slack. Seems to be a bit laggy too these days btw.

How to add a dependency to a arbitrary file to a T4 template?

I have a T4 template that generates classes from an xml file.
How can I add a dependency between the xml file and the template file so that when the xml file is modified the template is rerun automatically without choosing "Run custom tool" from the context menu?
I don't believe T4 supports automatic template transformation based on an external dependency. I agree with Marc - if you only have one external file, you could create a custom "custom tool" for your XML file or simply use ttxgen. However, I don't think this approach scales up to a scenario where t4 template depends on more than one file. You may need to create a Visual Studio package to handle that.
How long does the tool take to execute? One lazy option might be to simply edit the csproj such that it always runs the tool during build (presumably via <Exec ... /> or a custom targets file) - of course, this depends on it being quick to execute.
Another way would be to write a shim that works as the "Custom Tool" in VS, and simply calls the existing exe (or whatever) with the right args. Not trivial, but doable (see here) - I believe this then supposedly plays fairly nicely with change detection. It is actually on my list of things to do for a current project, so I'll find out soon enough...
You can use AutoTT Visual Studio Extension.
This extension allows to configure triggers that will run a T4 template.
One of the possible triggers is a file change. In the sample configuration file in AutoTT page, the regular expression for the triggers matches all files in the specified folders (Controllers, Content), but you can change it so that it runs with a specific file only.
Chirpy is another option for doing this. And also T4 Regenerator, which does it in a different way.
Have you tried using <## xsd?

Categories