开发者

Is it possible to create a custom attribute , when applied to a method will execute the method in a seperate thread

Is it possible to create a custom attribute , when applied to a method will execute the method in a seperate thread .

Class A {

开发者_运维技巧
  [threaded]
  public void execute()
  {

  }

}

main()

{ A obja = new A(); obja.execute() // will excute in a new thread if [threaded] attribute is applied }


You can use PostSharp to achieve this.


Quick answer: No.

If you create an attribute, the runtime does not know what to do with it (how could it?). Your code could check and then execute in the threadpool (or in .NET 4, via a Task), but you will have to ensure you always check.

The .NET runtime (CLR) knows about the attributes it knows about, and can make execution decisions at runtime. (As can the compiler, e.g. the compiler knows about ConditionalAttribute to allow conditional compilation.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜