gpt4 book ai didi

c# - 从 C# 中的对象中删除特定属性

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:44 25 4
gpt4 key购买 nike

我正在为我的应用程序实现一些授权。我想实现具有某些角色和权限的用户只能看到某些属性。看一看:

// User Model
string lastname;
string firstname;
string birthdate;

假设用户是管理员,所以他可以看到所有用户他不能看到用户的生日。

我创建了一个返回所有允许属性列表的类(您只能看到名字和姓氏):

public class AllowedAttributes
{
private List<string> AllowedAttributes = new List<string>();

public AllowedAttributes()
{
this.AllowedAttributes.Add("lastname");
this.AllowedAttributes.Add("firstname");
}

public List<string> GetAllowedAttributes()
{
return this.AllowedAttributes;
}
}

我的 NHibernate 查询如下所示:

AllowedAttributes attributes = new AllowedAttributes();

var user = sessionService.GetDefaultSession()
.Query<User>()
// something like...
// .Select(attributes.GetAllowedAttributes())
.ToList();

有人可以帮我解决一个正确的 NHibernate 查询吗?我只想获取列表中指定的属性。

附言在我的应用程序中,列表要长得多,所以只输入属性是行不通的。

提前致谢:)

最佳答案

您可以通过使用 NHibernate 投影来实现这一点。按照此条目获取更多信息。 Can someone better explain what 'Projections' are in nHibernate?

关于c# - 从 C# 中的对象中删除特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50811646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com