自动填充类中的属性

时间:2021-04-05 05:42:12

标签: c# visual-studio

假设我有一个类名 User,有什么方法可以在 Visual Studio 中填充类属性和默认值,如下所示。我觉得一遍又一遍地重复输入属性很乏味。

可能在 Visual Studio 或任何可以执行此操作的扩展程序中有默认快捷方式来执行此操作?

案例 1

var user = db.User.Where(x => x.UserID).Select(o => new User
{
   UserId = o.UserId, // auto populate
   Username = o.Username, // auto populate
   ........  // auto populate all properties inside User class
}
);

案例 2

var user = new User();
user.UserID = "",  // auto populate with type default value
user.Username = "",  // auto populate with type default value
........ // auto populate all properties inside User class
........ // auto populate all properties inside User class

2 个答案:

答案 0 :(得分:0)

我不太确定你想在这里实现什么

var user = db.User.Where(x => x.UserID).Select(o => new User
{
  UserId = o.UserId, // auto populate
  Username = o.Username, // auto populate
  ........  // auto populate all properties inside User class
}

因为 new Userdb.User 是相同类型的类/实体。但是假设您有一个实体模型 User 和一个名为 UserViewModel 的 DTO/视图模型,并且您希望自动将 User 的值映射到 UserViewModel,您可以使用 automapper { {3}} 示例这是实体定义

public class User 
{
   public int Id {get; set;}
  
   public string FirstName {get; set;}

   public string LastName {get; set;}
 
   public int Age {get; set;}
}

我们有一个 UserViewModel,您想将 User 中的数据映射到该 public class UserViewModel { public int Id {get; set;} public string FirstName {get; set;} public string LastName {get; set;} public int Age {get; set;} }

AutoMapper

使用 var configuration = new MapperConfiguration(config => { config.CreateMap<User, UserViewModel>(); } ,您可以做到这一点

var user = db.User.Where(x => x.UserID).First();
var userViewModel = configuration.CreateMapper().Map<UserViewModel>(user);

然后你可以像这样使用映射器配置

User

这会自动将 UserViewModel 的属性值填充到 public class UserViewModel { public int Id {get; set;} public string FullName {get; set;} public int Age {get; set;} } 。或者,您可以拥有这样的视图模型

User

这次并非 UserViewModelvar configuration = new MapperConfiguration(config => { config.CreateMap<User, UserViewModel>() .ForMember(uvm => uvm.FullName, o => o.MapFrom($"{u.FirstName} {u.LastName}") ); } 的所有属性都一一匹配,您必须设置自定义映射器配置,即

User

通过这种方式,您可以配置自动 UserViewModelFullName 映射,以通过连接 {{1} 中的 FirstNameLastName 来映射 User 属性值}

答案 1 :(得分:0)

好吧,我已经建立了一个可以为您解决这个问题的库,名为 FastDeepCloner

   public class User 
    {

        public virtual string Name { get; set; } = "sdjh";

        public virtual int PasswordLength { get; set; } = 6;

        public Circular Test { get; set; } = new Circular();

    }
    
    public class CloneToTest
    {
        [FastDeepCloner.FastDeepClonerColumn("Name")] 
        [FastDeepCloner.FastDeepClonerColumn("LastName")] // Or
        public string FullName { get; set; }
        
        // You see here the type could be difrrent then the orginal type. 
        // FastDeepCloner will try to convert it, if it fail then a default value will be inserted insted
        public string PasswordLength { get; set; }
        
        // You could add a path insted, remember this only work on none list items.
        [FastDeepClonerColumn("Test.myBar.Id")]
        public int Id { get; set; }

        public Circular Test { get; set; }
    }

现在简单使用

  var user = new User() { Name = "alen toma" };
        var cloneTo =new  CloneToTest();

        FastDeepCloner.DeepCloner.CloneTo(user, cloneTo);
  
        Assert.AreEqual(user.Name, cloneTo.FullName);