I'm trying to add custom properties to CodeDOM output, such as file version, author, etc. I'm unsure how.
For the file version, you have to use AssemblyFileVersion attribute.
See the example.
CodeCompileUnit unit = CreateMyUnit();
var attribute = new CodeAttributeDeclaration(
new CodeTypeReference(typeof(AssemblyFileVersionAttribute)));
attribute.Arguments.Add(
new CodeAttributeArgument(
new CodePrimitiveExpression("1.1.1.1")));
unit.AssemblyCustomAttributes.Add(attribute);
As for the author, you may do the similar thing. See the MSDN assembly attribute.
EDIT:
You need add the references.
using System.CodeDom;
using System.CodeDom.Compiler;
Related
i tried using "System.Application' "System.Windows.Application"
Code:
pastebin . com LK5F8HpA
i could not say it here
The way to resolve ambiguities like these is by understanding what you are using and why you are using it.
Then use the proper using/importing declaration and or use fully qualified name so that your program can compile and you do not have ambiguity problems.
When you ask a question, Please provide more information about what Framework you are using and what version and what kind of applications you are building...
Form? WPF? Core or full version? etc
In DotNet Namespace: System.Windows.Forms.Application, Assembly System.Windows.Forms.dll
While in core: Namespace:System.Windows, Assembly:PresentationFramework.dll
Use fully qualified name to solve this problem quickly.
According to Msdn link you can use namespace alias and '::' operator to fix your issue.
Quick fix
using forwinforms = System.Drawing;
using forwpf = System.Windows;
public class Converters
{
public static forwpf::Point Convert(forwinforms::Point point) => new forwpf::Point(point.X, point.Y);
}
If there is need for both assemblies in one class, simply preface the specific types with the entire namespace.
So:
System.Windows.WhatEverIsNeededHere foo = new System.Windows.WhatEverIsNeededHere();
And:
System.Windows.Forms bar = new System.Windows.Forms();
I am basically doing the same work as described in extern alias with same assembly file name: I am writing a converter for classes between different versions of a software, so that the XML settings of these classes can be converted.
The conversion is ready and working ("extern alias" are set where needed).
Now I want to change my small test project into a full program. The conversion uses the DLLs of V22, V23 and V24, each with respective alias, and the program should use the latest version of the DLLs (currently V24, without alias, thus global) for its own operation. The problem is, that the program does not find any types from the referenced global DLLs.
Is Visual Studio (I'm using v.2015 U3) maybe not able to distinguish between the DLLs if the same DLL is used with and without alias?
The project references:
basics, path=..\v24.., no alias (#1)
basics v=22, path=..\v22.., alias=V22
basics v=23, path=..\v23.., alias=V23
basics v=24, path=..\v24.., alias=V24 (#1)
imaging v=22, path=..\v22.., alias=V22
imaging v=23, path=..\v23.., alias=V23
imaging v=24, path=..\v24.., alias=V24
...
I supspect that the marked (#1) assemblies collide somehow.
Is this correct?
Any solution or workaround?
I could add "extern alias V24" in every file of the general part of the program, but then I'd have to change that to "extern alias V25" when the next version of the DLLs is released. I'd like to avoid that extra work.
I found an acceptable workaround.
Instead of adding the alias to every "using" when I switch to another version, e.g. replace "/* v24 */" by "swcore_v_0_22"
using CImageObject_V_0_22 = swcore_v_0_22.SwCore.CImageObject;
using CImageObject_V_0_24 = /* v24 */ SwCore.CImageObject;
using CImageObjectStandard_V_0_22 = swcore_v_0_22.SwCore.CImageObjectStandard;
using CImageObjectStandard_V_0_24 = /* v24 */ SwCore.CImageObjectStandard;
using CImageObjectCombi_V_0_22 = swcore_v_0_22.SwCore.CImageObjectCombination;
using CImageObjectCombi_V_0_24 = /* v24 */ SwCore.CImageObjectCombination;
I can simply add a nested using, which is not possible by default but possible when placed inside a different namespace, see https://stackoverflow.com/a/35921944/2505186. The namespace exists anyway.
using SwCore_v_0_22 = swcore_v_0_22.SwCore;
using SwCore_v_0_24 = global::SwCore;
namespace ConfigEditor
{
using CImageObject_V_0_22 /**/= SwCore_v_0_22.CImageObject;
using CImageObject_V_0_24 /**/= SwCore_v_0_24.CImageObject;
using CImageObjectStandard_V_0_22 /**/= SwCore_v_0_22.CImageObjectStandard;
using CImageObjectStandard_V_0_24 /**/= SwCore_v_0_24.CImageObjectStandard;
using CImageObjectCombi_V_0_22 /**/= SwCore_v_0_22.CImageObjectCombination;
using CImageObjectCombi_V_0_24 /**/= SwCore_v_0_24.CImageObjectCombination;
Later I will replace "global" by "swcore_v_0_24" in all files, which still is some work, but much less than before. And since it is replacing instead of adding, it can do it automatically.
I could theoretically also replace "/* v24 */", but that could break the nice vertical alignment depencing of the length of the replacement. ;-)
I am sort of new to config files. I am aware that I can add key value pairs and that it's possible to access them and change them on the fly. I am attempting to implement the ChangeConfiguration method on https://blogs.msdn.microsoft.com/youssefm/2010/01/21/how-to-change-net-configuration-files-at-runtime-including-for-wcf/
However, I am getting:
"'ConfigurationManager' does not contain a definition for 'OpenExeConfiguration'"
...and I get the same for trying to use ConfigurationManager.RefreshSection()
I am aware that the instructions date back to 2010 so by the looks of it, these instructions seem to no longer be the correct procedure to do this...?
Context
Web UI tests using Specflow, Selenium WebDriver, NUnit
Class Library targeting .NET Framework 4.6.1
Trying to add key value pairs at runtime in App.config
using System.Configuration;
using System.Reflection;
namespace CoreSeleniumFramework.Managers
{
public class ConfigurationManager
{
static void ChangeConfiguration()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
appSettings.Settings.Clear();
appSettings.Settings.Add("name", "bar");
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
}
}
Answered by #Nastaran Hakimi
Ok so this is a thing... need to use...
System.Configuration.ConfigurationManager.OpenExeConfiguration
...when it seems I should just need...
using System.Configuration;
Edit: if you get "object reference not set to an instance of an object" (or in other words, GetEntryAssembly() returns null), use GetCallingAssembly()
I am a student studying Computer Engineering in University, and I am trying to develop an application that will read an rss feed from a certain url, then display the titles and links of each item in the feed as a notification whenever a the feed on the url is updated.
Well, I am actually at the very beginning, and I am working on this project for learning purposes, following some tutorials etc.
My plan was to use System.ServiceModel.Syndication library to read the rss feed from the url using the SyndicationFeed object and its methods. But whenever I try to use that I get a strange error. The error is as follows
--- CS0012: The type 'XmlReader' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=5.0.5.0',Culture=neutral, PublicKeyToken='7cec85d7bea7798e'.
Here is the part of code that this error is shown:
public void GetFeed()
{
// Create an xml reader that will read rss data from the given url
var xmlReader = XmlReader.Create(rssUrl);
syndicationFeed = SyndicationFeed.Load(xmlReader);
}
The part where I create the xmlReader has no errors, I also have the following assembly referenced, 'System.Xml'.
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel.Syndication;
using System.Xml; // Here is the System.Xml
Also, trying to add a refenrence to the said library (System.Xml) by right clicking and selecting 'Add Reference' just gives me another error, telling me that I cannot refenrence 'System.Xml' as it is already being referenced by the build system.
I tried using other classes from the System.ServiceModel.Syndication namespace to ensure that the problem is not with the assembly, and every other class, method, etc. worked without errors. For example, I am able to write this and get no error:
SyndicationItem item = new SyndicationItem();
item.Title = new TextSyndicationContent("Me");
item.Links.Add(new SyndicationLink() { Uri = new Uri("http://somesite.con") });
item.PublishDate = DateTime.Now;
I get no errors on the above piece of code. I don't get errors when I use XmlReader like this for example:
var reader = XmlReader.Create(rssUrl);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Attribute:
// Some code here
break;
// Some more cases here......
}
}
I get no errors here about the XmlReader either. I only get the error when passing an instance of XmlReader to a SyndicationFeed.Load(XmlReader instance) method.
// This always gives me error!!!
syndicationFeed = SyndicationFeed.Load(xmlReader);
I have been trying to solve this problem for quite a while now, nearly 6 hours, I searched on the web, referenced different versions of System.ServiceModel.Syndication.dll, trying to find Syndication packages on Nuget package manager. Nothing worked. I am asking this question here as a last resort, and any help would be greatly appreciated.
UWP apps use the Windows Runtime class Windows.Web.Syndication.SyndicationFeed rather than .Net's System.ServiceModel.Syndication.
Windows.Web.Syndication.SyndicationFeed doesn't have an XmlReader constructor. Generally you'll create a SyndicationClient and then call RetrieveFeedAsync(url) to get the SyndicationFeed.
See How to access a web feed (XAML) for a full walkthrough.
I'm looking for ANY means of setting the file version for an exe file generated using codeDOM. Mine always comes out as 0.0.0.0. Programatically would obviously be preferred, but at this point anything would be better than nothing.
The version of the compiled assembly is controlled by the AssemblyFileVersion attribute. You just need to make sure this is included as part of your CodeDom tree when you compile.
You can set this by adding the attribute into the CodeCompileUnit AssemblyCustomAttributes member.
CodeCompileUnit unit = CreateMyUnit();
var attribute = new CodeAttributeDeclaration(
new CodeTypeReference(typeof(AssemblyFileVersionAttribute)));
attribute.Arguments.Add(
new CodeAttributeArgument(
new CodePrimitiveExpression("1.1.1.1")));
unit.AssemblyCustomAttributes.Add(attribute);