Java中的Lombok项目是如何工作的,是否可以在.net中使用属性? [英] How project Lombok in java works and is that possible in .net using attributes?

查看:47
本文介绍了Java中的Lombok项目是如何工作的,是否可以在.net中使用属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Project Lombok 使在类中实现样板代码变得轻而易举..NET属性有可能吗?那里有.net端口吗?

Project Lombok makes it trivial to implement the boilerplate code in the classes. Is that possible with .NET attributes? Is any .net port there?

推荐答案

在龙目岛,一个Java类可能看起来像这样

Well in Lombok a Java class might look like this

import lombok.Data;

@Data public class Cart {
  private int id;
  private DateTime created;
  private int items;
  private int status;
}

在C#中,同一类看起来像这样

While in C# the same class would look like this

public class Cart {
  public int Id { get; set; }
  public DateTime Created { get; set; }
  public int Items { get; set; }
  public int Status { get; set; }
}

因此C#(在此示例中为3.0)在没有任何其他库的情况下非常接近,但是当您开始向某些属性添加最终"时,Lombok的神奇的自动构造器"部分确实发光了.至于.Net替代方案,据我所知,.Net注释不提供在将字节码传递给编译器之前对其进行拦截的功能(Lombok发挥了如此大的作用),因此您的选择仅限于某些模板系统+像nAnt这样的构建脚本.这将是一团糟.

So C# (3.0 in this example) gets rather close without any other libraries, but as you start adding "final" to some properties the magic "auto constructor" part of Lombok really shines. As for a .Net alternative, as I understand it the .Net annotations don't provide the ability to intercept the byte code before it goes to the compiler (what Lombok uses to such great effect), so your options are limited to some template system + a build script like nAnt. This would be a mess to maintain.

这篇关于Java中的Lombok项目是如何工作的,是否可以在.net中使用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆