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

Remove specific attributes from object in C#

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

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

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

我创建了一个 class,其中 returns 是所有允许属性的列表(您只能看到名字和姓氏):

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 查询问题吗?我只想获取列表中指定的属性。

P.S。在我的应用程序中,列表更长,所以只输入属性是行不通的。

提前致谢:)

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