为什么不编译多次使用PreApplicationStartMethodAttribute?

Shr*_*ike 5 c# system.web

System.Web.PreApplicationStartMethodAttribute定义为:

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple=true)]
public sealed class PreApplicationStartMethodAttribute : Attribute 
{}
Run Code Online (Sandbox Code Playgroud)

即它允许多次使用(AllowMultiple = true).但是如果我尝试将这个属性的几个用法添加到我的程序集中:

[assembly: PreApplicationStartMethod(typeof(MyType1), "Start")]
[assembly: PreApplicationStartMethod(typeof(MyType2), "Start")]
Run Code Online (Sandbox Code Playgroud)

我收到编译器错误:

错误2重复'PreApplicationStartMethod'属性

为什么是这样?

Jon*_*eet 8

我怀疑你正在看.NET 4.5版本,它被记录为具有AllowMultiple = True.

.NET 4.0版本文档显示为AllowMultiple = false:

[AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class PreApplicationStartMethodAttribute : Attribute
Run Code Online (Sandbox Code Playgroud)

因此,如果你的目标是.NET 4.5,它应该没问题.