java中的Lombok项目如何工作?在.net中使用属性是否可以实现?

发布于 2024-09-27 22:40:56 字数 121 浏览 1 评论 0原文

项目 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲歌长辞 2024-10-04 22:40:56

现在有一个 .NET 端口!查看 Lombok.NET

免责声明:我是这个库的作者。

Now there is a .NET port! Check out Lombok.NET.

Disclaimer: I am the author of this library.

若能看破又如何 2024-10-04 22:40:56

在 Lombok 中,Java 类可能看起来像这样,

import lombok.Data;

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

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

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)在没有任何其他库的情况下变得相当接近,但是当您开始向某些属性添加“final”时,神奇的“ Lombok 的“自动构造函数”部分确实很出色。至于 .Net 替代方案,据我了解,.Net 注释不提供在字节码进入编译器之前拦截字节码的能力(Lombok 使用的效果如此之大),因此您的选择仅限于某些模板系统 + 像 nAnt 这样的构建脚本。维护起来会很混乱。

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;
}

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; }
}

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.

孤君无依 2024-10-04 22:40:56

我会说:

public class Cart {
   public int Id { get; set; }
   public DateTime Created { get; set; }
   public int Items { get; set; }
   public int Status { get; set; }
}
var cart = new Cart
{
   Id = 10,
   DateTime = DateTimeNow,
   ....
};

本身不需要任何构建器。只使用 C# 对象初始值设定项?也就是说,它没有隐藏任何实现内容。因此,如果您想将一些更复杂的初始化数据“分解”到您的初始化中,您可以将其隐藏在构建器中,但不能直接隐藏在对象初始值设定项中。您必须将其放在 getter 或辅助类中的某个位置。顺便说一下,Lombok java 生成器也没有。
并且对象初始值设定项也不执行任何流畅的操作,例如 .id(..).name() 等。

I would say:

public class Cart {
   public int Id { get; set; }
   public DateTime Created { get; set; }
   public int Items { get; set; }
   public int Status { get; set; }
}
var cart = new Cart
{
   Id = 10,
   DateTime = DateTimeNow,
   ....
};

Dont need any builder per se. Just use c# object initializer? That said, it does not hide any implementation stuff. So if you would like to put some "factoring" of more complex init data to your init, you can hide that in the builder, but not directly in the object initializer. You must put that somewhere in the getter, or a helper class. The Lombok java generator does not either by the way.
And the object initializer does not do any fluent stuff either, like .id(..).name() etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文