How to change the default namespace for Embedded Resources with MSBuild? - visual-studio-2010

I am attempting to embed an unmanaged dll in my console project. The default namespace of the project is Company.Project1Exe. The Assembly Name (output exe ) is named project1.exe
The dlls are added to the project using the Add as Link option and are located in a Libs\x86 subfolder
Company.Project1Exe
|
|--Program.cs
|--Libs
|--x86
|-My1st.dll
|-My2nd.dll
They have been added to the project using the Add as Link option, thus are not physically locate in the Libs subfolder.
I have set the Build Action of both these dlls to 'Embedded Resource'.
By default, MSBuild will embed these dlls using the DefaultNamspace.ExtendedNamespace.FileName where the ExtendedNamespace represents the directory structure of the project.
This results in resource being embedded as Company.Project1.Libs.x86.My1st.dll and Company.Project1.Libs.x86.My2nd.dll respectively.
I want these resources to embedded using the Assembly Name so that they are embedded as Project1.Libs.x86.My1st.dll and Project1.Libs.x86.My2nd.dll respectively.
How can I do this?

The one method that can address this issue is to set the LogicalName of the embedded resource. By default when you embed a resource, you will find an entry in your csproj file similar to
<EmbeddedResource Include="path to embdedded resource"/>
In the case of resources that are added using Add as Link, you will find an additional Link attribute. In this case, the Link attribute is the path of the resource relative to your project structure and the Include attribute is the pointing to file's location on your machine (relative to your project).
<EmbeddedResource Include="path to embdedded resource"/>
<Link>Libs\x86\My1st.dll</Link>
</EmbeddedResource>
In order to get the assemblies embedded using a different namespace the LogicalName attribute can be added to the above which allows one to override the default msbuild behaviour.
<EmbeddedResource Include="path to embdedded resource"/>
<Link>Libs\x86\My1st.dll</Link>
<LogicalName>$(TargetName).Libs.x86.My1st.dll</LogicalName>
</EmbeddedResource>
The downside it would seem, is that one will need to do this for every resource added. I would however have preferred that this convention be set in some way such that this can be the default way to embed any resource in my project i.e. use the $(TargetName) as a replacement for the default namespace

Related

Adding a custom property to the VS Project Designer

I am creating a Visual Studio extension and have added a custom configuration-specific property to an existing Microsoft Visual C++ (vcxproj) project using the IVsBuildPropertyStorage interface.
I would like to have that custom property show up in the Project Designer's property pages, as part of a new property page. There is some information on how to achieve that for a custom project type but the approach relies on reimplementing the project system which is not appropriate for all situations. Is there a way to extend the Project Designer for an existing project type?
One way to customise the Project Designer pages for a vcxproj is to include an additional property schema by adding the following:
<ImportGroup Label="PropertySheets" />
<ItemGroup>
<PropertyPageSchema Include="proppage.xml">
<Context>Project</Context>
</PropertyPageSchema>
</ItemGroup>
to a project which can be done by either copying this snippet into the project file directly or by putting it into a custom props file and including that into the project. Details on the structure of a property page schema are available here.
The main issue with this approach is that it requires manual intervention or an explicit step for setting up a project to use the custom property page, making it unsuitable for my use case.
It is also bit cumbersome as it requires shipping one-two additional files with the extension and it leaves a footprint in the project file. I would like to come across a more minimalistic solution that automatically applies to all MS VC++ projects but does not modify the project files unless the user inputs a property into the custom property page.

Is there an extension to link a file to another like the .designer files

Is there an extension to easily link a file to another in a VS project? The same way a designer file is linked to another file.
I know i can open include the file in the project then open the project in notepad and change
the include element to depend on another file like
<None Include="Features\ContactTestData.json">
<DependentUpon>ContactInfo.feature</DependentUpon>
</None>
but is there a way to do this through the UI?
It sounds as if partial classes may solve your issue. Which is how the form designers work.
It allows you to have multiple class files with the same name in the same namespace accessible as the same class.

Resources files issues in ASP.NET MVC 3

I´m having a strange problem with resources files. Strange because it was working great.
Well, I have some resources files. I created a Resources folder on my asp.net mvc 3 project structure and I am mirroring the views's folder structure. For now I have:
Views
Shared
_Layout.cshtml
Resources
Shared
Layout.pt-BR.resx
Layout.en-US.resx
Layout.ko-KR.resx
All resources files are configured as Embedded Resource, PublicResXFileCodeGenerator and with Custom Tool Namespace "ViewRes". All ".Designer.cs" files are empty. I remember to see some code in them when it was working.
On my .csproj file I have these data:
<ItemGroup>
<EmbeddedResource Include="Resources\Shared\Layout.en-US.resx">
<SubType>Designer</SubType>
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Layout.en-US.Designer.cs</LastGenOutput>
<CustomToolNamespace>ViewRes</CustomToolNamespace>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Shared\Layout.ko-KR.resx">
<SubType>Designer</SubType>
<Generator>PublicResXFileCodeGenerator</Generator>
<CustomToolNamespace>ViewRes</CustomToolNamespace>
<LastGenOutput>Layout.ko-KR.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Shared\Layout.pt-BR.resx">
<SubType>Designer</SubType>
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Layout.pt-BR.Designer.cs</LastGenOutput>
<CustomToolNamespace>ViewRes</CustomToolNamespace>
</EmbeddedResource>
</ItemGroup>
The problem is: When I try to access the #ViewRes on my views, It is not recognized. I tried already to remove the custom tool namespace but the #Resource is not recognized too. In both situations I got this compilation error:
Compiler Error Message: CS0103: The name 'ViewRes' does not exist in the current context
Does anybody have any idea?
Thanks,
Paulo
EDIT: NEW INFORMATIONS
I created a global Resources.resx file by project's properties menu in VS2010 and it's working with the custom tool namespaces I have entered. "Resources" namespace still not working. I think the problem is with the local resources files. Unfortunatly I need them because I can't rename the global resources.resx file including the culture code.
EDIT 2
I created new .resx file on Resource folder and what I can see is that the filename can not have 2 ".". Is the name is Resources.resx, it works. If the name is Resource-en-US.resx, it works but, If the name is Resources.en-US.resx it fails!
I think you can also write a custom helper, that will return by reflection the string.

How do I add Same Solution Project References when exporting Templates in Visual Studio?

I have a solution that contains several projects, lets call them ProjectA and ProjectB. I need ProjectA to have a reference to ProjectB, however when I "Export Template" I can't seem to keep the reference. What I am trying to do is to have it create the reference for me when I create a new project and then automatically name it to what I entered.
This is slightly out of scope for pure Visual Studio templates.
Visual Studio templates supports replacements parameters for templates, so what you could do is:
Inside your ProjectA.zip template file you will find ProjectA.csproj, which is the template for the project
In ProjectA.csproj you will find an <ItemGroup> containing <Reference> entries
Among them should be an entry like <Reference Include="ProjectB">
When you create a project from your ProjectB-template, it's name will be user-specified, so you want to replace ProjectB with a replacement parameter like $Foo$.
This is where it gets out of scope for pure templates: How do you tell visual studio to replace $Foo$? And what would the replacement be?
Your only choice here it to use a custom wizard, which either looks for the ProjectB-type project or asks the user for it.
Here's an outline for how to get the template and custom wizard to get along:
Create a VSIX package project.
Be sure to add the [ProvideBindingPath] attribute to your package. This is required to get the custom wizard registered (otherwise you would need to install it to the GAC, so VS can find it)
Create your wizard, it should implement Microsoft.VisualStudio.TemplateWizard.IWizard
Add your template to the solution and add it as a ProjectTemplate asset in your package's vsixmanifest
In the .vstemplate file of your template, add the following snippet below the <TemplateContent> block:
<WizardExtension>
<Assembly>Your.Packages.Assembly</Assembly>
<FullClassName>Your.Wizard</FullClassName>
</WizardExtension>
That will bring up your wizard when the project is created. You could, in the RunStarted method bring up a dialog which asks for the reference or you could use the automationObject parameter (which actually is a EnvDTE.DTE instance) to try and find the ProjectB project and supply it's name in the replacementsDictionary.

Best way to deploy and reference an XSLT file

In a visual studio project I have three layers, Data Layer, Business Layer and Presentation Layer.
In the Data Layer I have a few XSLT's that transform some objects into an email, all works fine but I have discovered that the XSLTs do not get built/copied when building.
I have currently, created a folder in the deploy location and placed the XSLT's there but I am concerned about relying on a manual process to update these.
Has anyone encountered a similar issue and if so how did they get around it.
It smacks of changing the MSBuild script to copy the build artifacts to the required location, does anyone have examples of this?
Thaks
If you are using Visual Studio 2005/2008, the easiest way to do this is by including your XSLT files as project resources.
Open the Properties for your project.
Select the Resources tab. You will probably see a link that says "This project does not contain a default resources file. Click here to create one." Go ahead and click on that.
Click the Add Resource drop-down near the top and select Add Existing File.
Browse to your XSLT files and select them.
After you have done this, you can easily access the resources in the following manner:
// To get the contents of the resource as a string:
string xslt = global::MyNamespace.Properties.Resources.MyXsltFile;
// To get a Stream containing the resource:
Stream xsltStream = global::MyNamespace.Properties.Resources.ResourceManager.GetStream("MyXsltFile");
If you are using Visual Studio 2003, your best bet is to include those XSLT files as embedded resources for the DLL. In Visual Studio, select the file(s) in Solution Explorer, open the Properties pane, and change the Build Type to "Embedded Resource". You can then use the GetManifestResourceStream method to get a Stream containing the XSLT(s). The name to pass will be based on the default namespace of your assembly, the folder containing the file, and the name of the file.
For example, say your data layer assembly has a default namespace of My.DataLayer. Within your data layer project you have a folder named Templates which contains a file called Transform.xslt. The code to get your XSLT would look like this:
// There are numerous ways to get a reference to the Assembly ... this way works
// when called from a class that is in your data layer. Have a look also at the
// static methods available on the Assembly class.
System.Reflection.Assembly assembly = (GetType()).Assembly;
System.IO.Stream xsltStream = assembly.GetManifestResourceStream("My.DataLayer.Templates.Transform.xslt");
For more information check out this article on CodeProject.
Obvious question maybe, but still has to be asked, did you include the folder containing the XSLT's in the project itself? Is this a web or forms app?
In VS, it is easy to set the properties of the XSLT files in the project to copy on build, by default they do not.
I may have explained myself poorly.
THe Data layer is a class library that a the presentation layer references.
On building the DataLayer I can get the XSLTs to output to the Bin directory of the DataLayer. However when I build and publish the presentation layer, it correctly grabs the DLL but not the XSLTs

Resources