I am creating a .NET assembly. I want to have 2 different versions of this assembly. The only difference between the 2 versions is a guid string which is embedded in a .cs file.
So for version 1 of the assembly, the guid will be ECABAFD2-7F19-11D2-978E-0000F8757E2A and for version 2 ECABAFD2-7F19-11D2-978E-0000F8757E2B
How do I manage something like this in Visual Studio 2010 ? Is there some kind of automation tool that can change that string for me and compile both versions ?
How would you do it ? I am opened to suggestions
In C#, conditional compilation is typically done using ConditionalAttribute. Place code using the relevant GUID values in your assembly that is Conditional on two different compilation symbols - say VARIANT1, VARIANT2.
Define build configurations for this project in Visual Studio that define VARIANT1 for the first build, VARIANT2 for the second build. This results in two output binaries - one with the first GUID and the other with the second.
If you want a slight change perhaps use the #if directive
#if FirstVersion
_id = "ECABAFD2-7F19-11D2-978E-0000F8757E2A"
#else
_id = "ECABAFD2-7F19-11D2-978E-0000F8757E2B"
#endif
Then you either use the command line to compile setting the directive or setup a new configuartion that you can switch.
As for having VS build both at the same time I would suggest a script or external build tool like Nant to do that.
Related
I am trying to create re-usable extension methods for the Startup class for asp.net core.
One thing i'd like to do is to add all the different usings based on true/false inputs.
One example is services.AddHttpsRedirection inside the ConfigureServices method.
My problem is that this method requires adding the nuget package Microsoft.AspNetCore.HttpsPolicy.
What I'm trying to do is add a preprocessor directive based on the presence of this package/dll.
From what I have researched, it seems I would need to use something like:
#if HTTPS_POLICY
services.AddHttpsRedirection(options =>
{
//difference options
});
#endif
But what I can't figure out is where I will be setting the HTTPS_POLICY symbol.
I've found 2 possible places:
Using the .csproj file i can set DefineConstants elements using a Condition attribute.
I can set conditional compilation symbols inside visual studio.
But I'm not even sure if these 2 things are different, they could be the same.
And for option 1, I don't know how to use the Condition attribute to look for the existence of a package reference or dll.
Also consider, I am creating a class library that will be used by other projects, and ideally I don't want those other projects to have to set conditional symbols and things.
Is this even possible to achieve, if so how?
I have a large WinForms application (C# , .NET 4.5.2) with several own DLLs (plug-ins for the application), all as different Projects in the same Solution. I use Visual Studio 2015 Community.
The main app and all the DLLs have their version number assigned in their respective AssemblyInfo.cs files like this:
[assembly: AssemblyVersion("1.0.*")]
Now I want to up the version of the application to, say, 2.0.. I also want all the DLLs to be 2.0.. The way I currently have it I would need to go into each DLL and manually change the version to 2.0.*.
Is there a way to inherit the "2.0" part from the application so that, in future, I would only have to change major and minor version number in one place?
I did some searching but was not able to find the answer.
Update:
What I was hoping is that I can replace
[assembly: AssemblyVersion("1.0.*")]
with something like:
[assembly: AssemblyVersion(some_string + ".*")]
where "some_string" is a string containing the major and minor version number. But I wouldn't know where I can define that string, or if this is possible at all.
Add a link to the original AssemblyInfo.cs file to the other project via the project solution explorer:
Right click on the project -> Add -> Existing item -> Add as link (from the dropdown menu)
Now, once you change the original AssemblyInfo.cs, any changes will be applied to all the projects to which the file was added as a link.
Edit:
To avoid duplicating attributes that should be unique per assembly (such as the GUID), make two files, one for the shared attributes like version number, and another for assembly specific attributes. No one forces you to put everything into the same file. It does not even have to be named AssemblyInfo.cs
In Visual Studio of you can set Pre build events.
Right click the project and select properties
Go to Build Events
then in the first box for pre-build event you can launch a simple script like this
if $(ConfigurationName) == Release C:\AssemblyChanger.exe $(ProjectDir)
This sample script above will Launch "AssemblyChanger.exe" located on your c drive and it will pass the current Visual Studio project folder as an argument. from there it is very simple to read the Assembly.cs on the specific project that manage the "version" and edit the one in the path that was pass as argument. Plus this will only be called if you are in Release mode (that "if" can be removed without a problem).
You can create a simple console application to do that.
That script can be set in all DLL pre build event so when they compile they call the script and they will get their assembly edited before the compiler create the DLL.
I have the following code (inherited from a contractor):
public partial class StoredProcedures
{
#if NO_THREAD
readonly static String version = "XXXX, Version 1.02, Apr/29/2010";
#else
readonly static String version = "XXXX, Version 0.93, Dec/21/2006";
#endif
I can't seem to find NO_THREAD anywhere else. This is code that compiles and installs as a SQL assembly. Is it something special or am I missing something simple?
Try to check Project Properties->Build->General->Conditional compilation symbols for all Build configurations which you have for the project, It may be there.
Look for a #define statement. See the docs for #if preprocessor conditionals : http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx
If you can't find a
define #NO_THREAD
Anywhere in the code, then it's probably because the contractor was defining the symbol by passing the /define compiler option.
See here for more details (typing from a cell, sorry for the format):
http://msdn.microsoft.com/en-us/library/0feaad6z.aspx
you should probably have a look at the c# pre-processor directives
No_Thread here is a symbol which can be defined by using #define No_Thread and when #define No_Thread is present then #if NO_THREAD will result in true and at compile time readonly static String version = "XXXX, Version 1.02, Apr/29/2010"; this statement will be compiled otherwise the next statement will be compiled.
this is generally used to differentiate between debug and release versions. have you noticed there are 2 versions in VS when you create a new project. if you write something like this somewhere in you code
#if DEBUG
Console.WriteLine("DEBUG VERSION");
#endif
then the string "DEBUG VERSION" would only be printed on the console when the project is in debug mode because the VS inserts a symbol DEBUG if you manually do it using the #define pre-processor then too this line would be compiled
NO_THREAD is a symbol for conditional compilation.
It can come from, #define NO_THREAD, from the project file, or from the nant file (or whatever method you use for building).
If it's defined, the first line of code is counted as part of the C# code. If it isn't, then the second is.
If that's the sole occurence, I'd say it was a hangover from something removed, but if you're uesd to using visual studio to build, then make sure there isn't a build file for nant in case the previous developer used that instead.
This is a conditional compilation symbol. In Visual Studio 2010, these appear on the Build page of your Project Properties in the Conditional compilation symbols text box. Probably one of your Configuration Manager configurations either contains this symbol or has at some point in the past. Presumably, there is another #if somewhere that disables a block of code that uses multiple threads if the NO_THREAD symbol is present.
Is this possible to check in assembly what client (winforms app or asp.net page) is running it?
I want to add some methods but only for specific client.
I know, there is predefined DEBUG (#if DEBUG). Where can I find full list, what can I check using preprocessor?
To expand on m0sa's answer, preprocessor directives are basically just a string passed to the compiler.
If you are so inclined, you can add new build configurations (example: instead of Debug/AnyCPU and Release/AnyCPU, you could make WebDebug/AnyCPU, WinformsDebug/AnyCPU, WebRelease/AnyCPU, etc).
Then in the properties page of your project, for each configuration you could provide a value in the 'Conditional compilation symbols' field. For example, for WebDebug and WebRelease, you could provide the conditional symbol WEB. Then, you would be able to use:
#if WEB
using MyNamespace.WebStuff;
#endif
You will need multiple build configurations for that and define different a preprocessor directive for each one. You can set the conditional preprocessor directives in the Build tab of the project Properties page.There are no other directives defined, just the DEBUG directive which you can turn on and off (together with the TRACE directive) in the same tab. Note that DEBUG it is not defined for the "release" build configuration. This is kind of what you will need to do to enable different versions of the assembly to be built.
References:
MSDN
www.davidarno.org <-- see this one for a more visual description
I have several classes in a project which need to only be in certain builds of the application which are currently not ready for release or debug.
To prevent these classes from being used, I want to set around them this:
#if USE_MYCLASS
// Code here...
#endif
Unfortunately, I don't know how to setup a project-wide #define.
Is there functionality in Visual Studio to set project-wide definitions?
If there is, though I don't need it right now, is there a functionality to set solution-wide definitions?
If there is no functionality for such (seeing as C# does not have include files, I suppose it's possible), is there any method or plugin of doing this functionality without using the command line compiler and /D?
You can do that in the project properties, but not in source code.
Project Properties => Build => Conditional compilation symbols
You can specify whichever symbols you need (space delimited, but IIRC is is quite forgiving). Note that DEBUG and TRACE can also be toggled with a checkbox.
I have some projects with multiple "release" build configurations, with different symbols in each (for building 2.0 vs 3.0 vs 3.5 versions - see <DefineConstants> here)