代码之家  ›  专栏  ›  技术社区  ›  Robert MacLean

为什么要使用后编译器?

  •  8
  • Robert MacLean  · 技术社区  · 15 年前

    我在努力弄明白为什么一个后编译器,比如 PostSharp 应该需要吗?

    我的理解是,它只是在原始代码的属性处插入代码,那么为什么开发人员不自己编写代码呢?

    我希望有人会说,编写起来更容易,因为您可以在方法上使用属性,然后不会使它们混乱在样板代码中,但这可以通过使用DI或反射以及一些没有后编译器的预先考虑来完成。我知道,因为我说过反射,性能大象现在将进入-但我不关心这里的相对性能,当绝大多数场景的绝对性能是微不足道的(亚毫秒到毫秒)。

    4 回复  |  直到 15 年前
        2
  •  22
  •   barakcaf    7 年前

    string key = string.Format("[{0}].MyMethod({1},{2})", this, param1, param2 );
    T value;
    if ( !cache.TryGetValue( key, out value ) )
    {
       using ( cache.Lock(key) )
       {
          if (!cache.TryGetValue( key, out value ) )
          {
             // Do the real job here and store the value into variable 'value'.
             cache.Add( key, value );
          }
       }
    }
    

    http://vimeo.com/2116491

        3
  •  11
  •   Jon Skeet    15 年前

        4
  •  3
  •   stormianrootsolver    14 年前