以编程方式将文件描述添加到 c# 编译器输出文件
Add file description programmatically to c# compiler output file
这是我的编译器函数,效果很好。但输出文件没有描述,如 Copyright
、ProductVersion
或 FileDescription
:
public static bool CompileFromSource(string source, string output, string icon = null, string[] resources = null)
{
CompilerParameters cParams = new CompilerParameters
{
GenerateExecutable = true,
OutputAssembly = output
};
string options = "/optimize+ /platform:x86 /target:winexe /unsafe";
cParams.CompilerOptions = options;
cParams.TreatWarningsAsErrors = false;
cParams.ReferencedAssemblies.Add("System.dll");
cParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cParams.ReferencedAssemblies.Add("System.Drawing.dll");
cParams.ReferencedAssemblies.Add("System.Data.dll");
cParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");
if (resources != null && resources.Length > 0)
{
foreach (string res in resources)
{
cParams.EmbeddedResources.Add(res);
}
}
Dictionary<string, string> providerOptions = new Dictionary<string, string> { { "CompilerVersion", "v2.0" } };
CompilerResults results = new CSharpCodeProvider(providerOptions).CompileAssemblyFromSource(cParams, source);
}
如何在此编译器中以编程方式添加关于我的文件的描述?而且我不想使用像 resourceHacker
这样的第三方应用程序
你能不能把 prepend/append [assembly: ...]
放到 source
字符串中?
如果没有,可以在此处找到标准的 Roslyn-as-code 语法:https://roslynquoter.azurewebsites.net/
[assembly: MyAttribute]
namespace S {
class C { }
}
结果:
CompilationUnit()
.WithAttributeLists(
SingletonList<AttributeListSyntax>(
AttributeList(
SingletonSeparatedList<AttributeSyntax>(
Attribute(IdentifierName("MyAttribute"))))
.WithTarget(
AttributeTargetSpecifier(
Token(SyntaxKind.AssemblyKeyword)))))
.WithMembers(
SingletonList<MemberDeclarationSyntax>(
NamespaceDeclaration(IdentifierName("S"))
.WithMembers(
SingletonList<MemberDeclarationSyntax>(
ClassDeclaration("C")))))
.NormalizeWhitespace()
这可以通过添加的另一种方式完成:
[assembly: AssemblyTitle("Loader")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Loader")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("f99d6ga6-3b37-43a6-9332-866f8s8g7f9b")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
到要以编程方式编译的源代码的顶部
这是我的编译器函数,效果很好。但输出文件没有描述,如 Copyright
、ProductVersion
或 FileDescription
:
public static bool CompileFromSource(string source, string output, string icon = null, string[] resources = null)
{
CompilerParameters cParams = new CompilerParameters
{
GenerateExecutable = true,
OutputAssembly = output
};
string options = "/optimize+ /platform:x86 /target:winexe /unsafe";
cParams.CompilerOptions = options;
cParams.TreatWarningsAsErrors = false;
cParams.ReferencedAssemblies.Add("System.dll");
cParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cParams.ReferencedAssemblies.Add("System.Drawing.dll");
cParams.ReferencedAssemblies.Add("System.Data.dll");
cParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");
if (resources != null && resources.Length > 0)
{
foreach (string res in resources)
{
cParams.EmbeddedResources.Add(res);
}
}
Dictionary<string, string> providerOptions = new Dictionary<string, string> { { "CompilerVersion", "v2.0" } };
CompilerResults results = new CSharpCodeProvider(providerOptions).CompileAssemblyFromSource(cParams, source);
}
如何在此编译器中以编程方式添加关于我的文件的描述?而且我不想使用像 resourceHacker
你能不能把 prepend/append [assembly: ...]
放到 source
字符串中?
如果没有,可以在此处找到标准的 Roslyn-as-code 语法:https://roslynquoter.azurewebsites.net/
[assembly: MyAttribute]
namespace S {
class C { }
}
结果:
CompilationUnit()
.WithAttributeLists(
SingletonList<AttributeListSyntax>(
AttributeList(
SingletonSeparatedList<AttributeSyntax>(
Attribute(IdentifierName("MyAttribute"))))
.WithTarget(
AttributeTargetSpecifier(
Token(SyntaxKind.AssemblyKeyword)))))
.WithMembers(
SingletonList<MemberDeclarationSyntax>(
NamespaceDeclaration(IdentifierName("S"))
.WithMembers(
SingletonList<MemberDeclarationSyntax>(
ClassDeclaration("C")))))
.NormalizeWhitespace()
这可以通过添加的另一种方式完成:
[assembly: AssemblyTitle("Loader")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Loader")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("f99d6ga6-3b37-43a6-9332-866f8s8g7f9b")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
到要以编程方式编译的源代码的顶部