Visual Studio 2022 - Nesting partial classes (files) - c#

I noticed that, in my solution, nesting partial classes is only working for Microsoft.NET.Sdk.Web project.
For the rest of the projects (Microsoft.NET.Sdk) it is not working.
Does anyone know anything about this?
Edit: I posted the SDK because I think that is the reason. I created a new solution, Console App (Microsoft.NET.Sdk) and tried creating partial classes, still doesn't nest the files.
Edit 2
After adding the following to the .csproj file, it is now nested.
<ItemGroup>
<Compile Update=".\Services\CatalogService.*.cs">
<DependentUpon>.\Services\CatalogService.cs</DependentUpon>
</Compile>
</ItemGroup>
Though, the question still remains...

Based on the documentation mentioned by Jimmy in comment, I tested in my VS2022 and it worked well.Firstly, nesting partial classes work for both Microsoft.NET.Sdk.Web and the rest of the projects (Microsoft.NET.Sdk) .You should check File nesting options in solution explorer.
Here’re several methods to nest files.
1 when you select Default, if no settings exist for a given project type, then no files in the project are nested. You should create project-specific settings through the right-click menu (context menu) of the project console App (Microsoft.NET.Sdk).And use pathSegment to set the rules:
 For example
"pathSegment": {
"add": {
".*": [
".cs",
]
}
}
RESULT
pathSegment: Use this type of rule to nest Catelog.Paged.cs under Catelog.cs
2 when you select Web: This option applies the Web file nesting behavior to all the projects in the current solution.
3 customize file nesting for a solution. Select Add Custom Setting in solution explorer. You can add as many custom file nesting settings as you like. It’s similar to method 1.

Related

How do I prevent nested C# classes with .resx files using the same named .resources file?

When generating a .resources file for a C# class which has a .resx file, Visual Studio 2022 only uses the namespace and outer class name to generate the name of the .resources file. If there is more than one class (with a .resx file) nested in the same outer class, the names of the .resources file will be same, resulting in error MSB3577.
Is there anyway to resolve this? For example, is there a parameter in the entry in the .csproj file to set the output name? Or do I just avoid using nested classes when .resx files are involved?
Background
Why do I have nested Form classes?
There is a C# control with the class name SpecialTrackBar. This class accepts a collection of DataItem objects, so there are DataItem and DataItemCollection classes. The dialog for editing the collection is DataItemCollectionEditorDialog.
I didn't want to put these classes at the same namespace as SpecialTrackBar as they may conflict with other control classes,
and I didn't want to make a super long class name like SpecialTrackBarDataItemCollectionEditorDialog.
Using nested classes and having SpecialTrackBar.DataItemCollectionEditorDialog seemed like a good compromise. I'm hoping there is an alternative naming solution.
Steps to replicate:
In Visual Studio 2022, create a new Windows Forms Control Library using .NET Framework 4.6.2 named "NestedResTest".
Add two Windows Forms, Dialog1 and Dialog2. The files Dialog1.cs, Dialog1.Designer.cs, and Dialog1.resx and the corresponding files for Dialog2 will be automatically created. The Dialog1 and Dialog2 classes will both be in the namespace NestedResTest.
Set the MSBuild project build output verbosity to "Diagnostic" (Tools -> Options -> Projects and Solutions -> Build And Run).
Build NestedResTest. The build should succeed. Search the Build output for the line with "OutputResources=" and you should see two .resources files:
NestedResTest.Dialog1.resources
NestedResTest.Dialog2.resources
Edit Dialog1.cs and Dialog1.Designer.cs and put the Dialog1 class inside the partial class InnerClass.
Build NestedResTest. The build should succeed. Search the Build output for the line with "OutputResources=" and you should see two .resources files:
NestedResTest.InnerClass.resources
NestedResTest.Dialog2.resources
Edit Dialog2.cs and Dialog2.Designer.cs and put the Dialog2 class inside the partial class InnerClass.
Build NestedResTest. The build should FAIL with error MSB3577. Search the Build output for the line with "OutputResources=" and you should see two .resources files, both with the same name:
NestedResTest.InnerClass.resources
I assume you know main consequence of nesting the Form classes is loosing designer support for designing the form.
That said, if you want to fix the resource name issues, you can control how the resource manifest files named using either of the following options:
Specify <ManifestResourceName>
Or specify <LogicalName>
Or remove <DependentUpon>
Example - Control how manifest resource names generate
Right click one the project and Unload the project
Find the <EmbeddedResource> that you are interested in and do one of the following:
Specify <ManifestResourceName>
<EmbeddedResource Include="Dialog1.resx">
<ManifestResourceName>RealProxyExample.Dialog1</ManifestResourceName>
<DependentUpon>Dialog1.cs</DependentUpon>
</EmbeddedResource>
OR Specify <LogicalName>
<EmbeddedResource Include="Dialog1.resx">
<LogicalName>RealProxyExample.Dialog1.resources</LogicalName>
<DependentUpon>Dialog1.cs</DependentUpon>
</EmbeddedResource>
OR Remove <DependentUpon>
<EmbeddedResource Include="Dialog1.resx">
</EmbeddedResource>
Right click on the project and Reload.
Build and run the project. You will see the resources have been applied and are working as expected.

How can build independent dll's that depend on the same source?

I am building a few different C# libraries that both depend on a single C# file we'll call Dep.cs, and these dll's need to be used together in a Unity project. I'd like to set up these projects in the following way:
The C# libraries can be built independently of one another using Visual Studio
C# libraries (i.e. dll's) can be imported into a Unity project without conflicting symbols
The C# library projects (i.e. the source code for each library via git submodule for example) can be imported into a Unity project without conflicting sources.
I've solved (1) by including Dep.cs in each library project that requires it, though this causes issue with (2). And I've solved (3) by putting the dependency in a folder like Dependencies~ so that Unity ignores the file (this way no duplicate classes are found).
I'm having trouble solving (2) however. I thought I'd be able to add Dep.cs as reference in the VS solution but This doesn't seem to work. I've heard of Assembly References but I am not sure if they do what I need.
You can use "Add File as Link" from Visual Studio "Add Existing File" screen. It also works well with git submodule, just place Dep.cs anywhere in a parent folder or in the solution's root directory.
To get the same result you can also directly edit the .csproj file and add a compile instruction:
<ItemGroup>
<Compile Include="..\..\Path\To\YourFile.cs" Link="YourFile.cs" />
</ItemGroup>
This method solves all the issues you mentioned.
To solve my problem I decided to modify the external scripts to be internal this way both dll's can compile with that source and not conflict with one another. The rest of the setup in my question remained the same so this solved (2) for me without compromising (1) and (3).

Writing a library in a framework [duplicate]

I'm writing a class library for a simple parser in C#. When I first created it, I used .NET standard 2.0, but now I need to migrate it to .NET 4.6 both to conform to the other projects in my solution and in order to use NUnit.
I tried to follow the instructions in the Microsoft documentation, but when I try to select another framework in the properties, I can only find other .NET standard versions.
How can I migrate it? Will I need to manually edit the .csproj file?
Open up the project file (.csproj) and change the TargetFramework to net462
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
</PropertyGroup>
My personal experience in Visual Studio 2017 is that recreating project and adding existent sources is the simplest, safest and most effective way - because .Net Framework based csproj file has extra xml elements (comparing with Standard based), it seems changing "TargetFramework" is not enough.
Below is portion of diffs appeared by default:
If you are publishing your class library as a Nuget package then there is a better way to set this up. Check out this article:
https://weblog.west-wind.com/posts/2017/Jun/22/MultiTargeting-and-Porting-a-NET-Library-to-NET-Core-20
Basically you can setup your class library for multi targeting, allowing it to be imported into .net core projects as well as different versions of .net frameworks.
There are a few steps that I did and worked for me:
git push your code, so you have a back up :)
Unload the project in VS (right click on the project and unload)
Edit the project in VS (right click and edit)
Replace the TargetFramework OR/AND TargetFrameworkVersion with
<TargetFramework>netcoreapp2.0</TargetFramework>
Change the project line, that's usually the first line (after xml root) to:
<Project Sdk="Microsoft.NET.Sdk">
Remove the import that's usually the second line (after the xml root)
Keep your PropertyGroups that describe the build options, if you want (I want mine as are custom)
Remove the references and the file references, they are not needed.
Close the file and reload (right click reload).
Delete the assemblyinfo file (from properties folder) as it is not needed, the assembly version comes now from the proj
Right click on the project and go to properties to see that you don't have any error in proj file. Ensure that you don't have typos or tags that are not close.
Build. If you have dependencies that you are missing then right click and on the project and add them. - I suppose that you don't want to edit them in the proj. Or you can do a dotnet restore or dotnetbulid to add them, as you would like.
Hope this works for you. They seem a lot of steps but they are not that complicated, and this is one time effort.

How to add Razor file to Xamarin project

I'm trying to get Razor working inside my Xamarin project and I can't seem to get Visual Studio 2017 to recognize that the files need to be processed by the Razor preprocessor.
When I go to add a new file, there's no template for Razor or .cshtml, so I'm just adding a text file and change the extension to .cshtml. Then, in the file properties, I'm setting the Custom Tool to RazorTemplatePreprocessor.
However, despite doing this, Visual Studio does not generate the .cs file that I expect it to.
What am I missing?
UPDATE AND WORKAROUND: As a result of helpful conversation with #SushiHangover (see answer and comment chain below), it appears that .NET Standard library projects do not currently allow Razor files to be added through the Add Item wizard, and if you add them manually, they don't compile correctly. this is true as of Visual Studio 15.5.2. I've opened a bug with Microsoft, but in the meantime, I have super clunky workaround, which is as follows.
At the end of the day, Razor files as you might use them in a Xamarin project are only there to generate .cs files, which compile into classes containing a GenerateString method, which code can call to generate HTML that can then be fed into a WebView. So, I figured, why not just create a regular .NET Framework library (not .NET Standard) and put all my Razor files there. In that project, you can add them normally and they work properly.
But since the rest of my project is .NET Standard and I didn't want to mix and match, I don't actually have any of the other projects reference this library. Instead, I just include the generated .cs files as links (click the little arrow next to the Add button and choose Add as Link instead) in my "real" project (the one where I wanted to add them originally). So the library with Razor files has only one purpose, and that is to generate .cs files, period. Those files are actually compiled into a different library.
I hope this helps someone! When Microsoft fixes the bug, I'll update this thread again (and move my Razor files back to where they ought to be).
EDIT 2
Microsoft has marked my bug "Under Consideration." If this issue affects you, please upvote it to encourage them to fix it sooner rather than later.
https://developercommunity.visualstudio.com/content/problem/172997/razor-templates-not-working-for-net-standard-proje.html
The Razor file templates are available on Visual Studio for Mac (VS4M) under the "Text Templating" group, but the recent versions of Visual Studio for Windows (VS4W) they have gone missing.
On VS4W, you can just edit the .csproj that you are trying to add a Razor file:
A Compile item for the generated .cs file that includes a DependentUpon tag for the .cshtml file.
A None item for the .cshtml that includes the Generator and LastGenOutput tags
Create those two files (.cshtml & .cs) (they can be empty to start)
Example (Xamarin.iOS, Xamarin.Android, & PCL projects)
<ItemGroup>
<Compile Include="RazorTemplate.cs">
<DependentUpon>RazorTemplate.cshtml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="RazorTemplate.cshtml">
<Generator>RazorTemplatePreprocessor</Generator>
<LastGenOutput>RazorTemplate.cs</LastGenOutput>
</None>
</ItemGroup>
Example (NetStandard 2.0 project)
<ItemGroup>
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="RazorTemplate.cs">
<DependentUpon>RazorTemplate.cshtml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="RazorTemplate.cshtml">
<Generator>RazorTemplatePreprocessor</Generator>
<LastGenOutput>RazorTemplate.cs</LastGenOutput>
</None>
</ItemGroup>
This is not the answer as I misunderstood the question so the answer is actually what #SushiHangover put above...
To get the Xamarin Razor pages the project has to be a WebViewApp and then you can add New PreProcessed Razor View to the project.
From the Xamarin Blog on the subject.
See our Building Hybrid apps with Xamarin documentation to get started building Razor Hybrid applications. Simply use Xamarin Studio to create a new WebView Application for iOS or Android, or add a WebView to any existing iOS or Android layout, and use Add New…Preprocessed Razor Template to incorporate Razor-powered web views into your Xamarin apps. For a slightly more complex sample of a data-driven Razor hybrid app, check out our RazorTodo app.
The documentation can be found here
More detail here as this details how to add the Razor view to each project.
For Visual Studio 2022 I refer you to this answer that worked for me:
https://stackoverflow.com/a/41116061/812013
When I entered 'RazorTemplatePreprocessor' for the file's custom tool as the answer suggests, I initially didn't notice that Visual Studio opens a dialog asking you to install a ASP .NET extension first. Once I installed this extension, I was then getting a cs file generating from a cshtml file.

how to use same name class libraries on a solution

Need to add two same name .csproj class libraries in my solution.Have two project but unfortunately those project class libraries names are same,like: Hello.csproj.I try to add existing project on solution then show me error
http://blogs.msdn.com/b/ansonh/archive/2006/09/27/774692.aspx
from above url I learned how to use same namespace dll on same project ,but I need help how to use same classlibraries on a solution
if have any query please ask,thanks in advanced.
Note:ok people want to know the reason,i have two project on Autocat 2005 and 2010,now want to merge those project on one solution,2010 update base on 2005 so class libraries are same,but i need to use both of them.So problem arise and seeking help.
You can have projects with the same name as long as they are already created in different folders and they are in different solution folders. If the projects are already created, do this to add them to your solution:
Add your first project to the solution.
In Solution Explorer window, right click your solution and select Add->New Solution Folder
Give a name to the newly created folder.
Right click the folder and select Add->Existing Project
Navigate to your second project and double-click the .csproj file.
You're done.
If you really must do this, then ensure the second project has a different name, and then change the namespaces of the classes in the second project (normally the project name comprises the first part of the namespace - just change that part). The classes will still be identical internally, but because they have a different namespace they will be distinct entities. This will lead to very smelly code though when you start mixing them up in the ClientApp - to avoid confusion make sure you always refer to them by their full namespace (i.e. do not have a using xyz.myclassname; statement at the top of the class file that uses them).
Maybe you want to run two (almost identical) instances of the same service or something, but as mentioned it is hard to think of a genuine reason why you would need to do this. If you are looking to have two identical looking instances but different implementation then you will want to use interfaces instead.
Edit: Visual Studio will not allow you to have two identically named projects, and you are playing crazy games if you change a project name but don't change its project GUID (in the .proj file and the .sln file).
The simplest thing for you to do here is to create a new empty project in the solution explorer, right click on it and Open folder in Explorer, then copy the class files from the original project to the new one, then back in the solution explorer choose Show all files (little button at the top of the solution explorer), then select the newly added files under the new project, right click, Add to project. (These menu options are from memory, they should be roughly right).

Categories

Resources