命名空间不能直接包含AssemblyInfo.cs中的成员

num*_*ang 3 c# build

我有如下文件AssemblyInfo.cs.我想为AssemblyVersion定义一个字符串:

string version = "1.2.0.0";  
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误说:

命名空间不能直接包含成员

我该怎么做呢?

using System.Reflection;
using System.Runtime.InteropServices;

// Setting ComVisible to false makes the types in this assembly not visible  to COM components
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("24f53cr8-552b-40d3-cds1-13e310ds6c3f")]

[assembly: AssemblyCopyright("1/10/2013")]

#if (DEVELOPMENT)
[assembly: AssemblyProduct("ShellDev")]
[assembly: AssemblyConfiguration("DEVELOPMENT")]
[assembly: AssemblyTitle("MMD Smart Client - Development Version")]
[assembly: AssemblyVersion("1.2.0.0")]
#endif

#if (RELEASE)
[assembly: AssemblyProduct("Shell")]
[assembly: AssemblyTitle("MMD Smart Client")]
[assembly: AssemblyVersion("1.2.0.0")]
#endif
Run Code Online (Sandbox Code Playgroud)

bit*_*onk 10

C#中没有全局变量.将字符串放在静态类中.

internal static class Version
{
   public const string VersionString = "1.2.0.0"; 
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

[assembly: AssemblyVersion(Version.VersionString)]
Run Code Online (Sandbox Code Playgroud)

  • 属性参数必须是常量表达式,因此`readonly`不起作用.它会给你一个编译器错误. (2认同)