基于 lambda 表达式添加属性

发布于 2025-01-05 22:35:00 字数 1143 浏览 1 评论 0原文

我正在使用 C# 创建一个视图模型,稍后将其序列化为 Json 以与 KnockoutJs 一起使用。

现在,如果某个用户有权查看和/或编辑属性,我需要添加属性级别的信息。

由于 Javascript 几乎没有反射可能性,因此我想在序列化为 Json 之前使用 C# 执行此操作。

我的视图模型可能如下所示:

class ViewModel {
    public string Name { get; set; }        
}

我想使用用户访问服务,该服务将接受具有一些角色的用户和视图模型,并遍历角色并应用访问权限。像这样的东西:

class AccessService {
    public void Apply(IUser user, ViewModel viewModel) {
        if(user.Roles.Contains(Roles.Admin)) {
            viewModel.AllowRead(vm => vm.Name);
        }
    }
}

viewModel.AllowRead(..)方法将是一个扩展方法,它将采用一个对象(或者如果需要的话可能是一个接口或类型),这是魔法发生的地方。

我希望此操作的结果是 viewModel 将获得一个名为 CanRead 的新属性,而该属性又将有一个名为 Name 的布尔属性。

生成的 viewModel 看起来像这样:

class ViewModel {
    public string Name { get; set; }
    public object CanRead { // Could non anonymous as well.
        return new {
            Name = true;
        };
    }
}

Can this be did withdynamics or do I have to use Reflection.Emit?我不是要求“显示代码”。我只是想知道我的想法是否是精神上的或者是否可能。

有什么想法吗?

I am using C# to create a view model that I later serialize into Json for use with KnockoutJs.

Now I need to add information on a property level if a certain user has access to view and/or edit the property.

Since Javascript has little to no reflection possibilities I want to do this with C# before serializing to Json.

My view model could look like this:

class ViewModel {
    public string Name { get; set; }        
}

I want to use a User Access Service that would take the user, which has some Roles, and the view model and go through the Roles and apply the access rights. Something like this:

class AccessService {
    public void Apply(IUser user, ViewModel viewModel) {
        if(user.Roles.Contains(Roles.Admin)) {
            viewModel.AllowRead(vm => vm.Name);
        }
    }
}

The viewModel.AllowRead(..) method would be an extension method that would take an object (or maybe an interface or type if necessary) and this is where the magic would happen.

I would like the result of this operation to be that the viewModel would get a new property with the name CanRead which in turn would have a boolean property with the name Name.

The resulting viewModel would look like this:

class ViewModel {
    public string Name { get; set; }
    public object CanRead { // Could non anonymous as well.
        return new {
            Name = true;
        };
    }
}

Can this be done with dynamics or do I have to use Reflection.Emit? I'm not asking for "show me the codez". I just want to know if my idea is mental or if it's possible.

Any ideas?

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

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

发布评论

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

评论(1

尐偏执 2025-01-12 22:35:00

我认为这是可能的,您可以使用 Lambdanator帮助实现这一目标。

用法示例:

Lambda.Reflect<SomeClass>(x => x.AMethod());    
Lambda.Reflect<SomeClass>(x => x.AProperty);    
Lambda.Reflect(() => local);

I think it would be possible and you could use the Lambdanator to help achieve this.

Example usage:

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