创建 Func运行时
Role 和 CustomRole
public class CustomRole
{
public string RoleName { get; set; }
public int RoleId { get; set; }
}
public class Role
{
public string RoleName { get; set; }
public int RoleId { get; set; }
public int MyRole { get; set; }
}
我有两个类:编译时的
Func<CustomRole, bool> Del = o => o.RoleId > 0;
我有一个像这样的委托:问题是在运行时我需要再创建一个具有相同条件但表名称已更改的
Func<Role, bool> Del1 = o => o.RoleId > 0;
委托 我怎样才能实现这一点?
I have two classes: Role and CustomRole
public class CustomRole
{
public string RoleName { get; set; }
public int RoleId { get; set; }
}
public class Role
{
public string RoleName { get; set; }
public int RoleId { get; set; }
public int MyRole { get; set; }
}
at compile time I have a delegate like this:
Func<CustomRole, bool> Del = o => o.RoleId > 0;
The problem is at runtime I need to create one more delegate with same condition but table name is changed
Func<Role, bool> Del1 = o => o.RoleId > 0;
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以让 Role 和 CustomRole 实现包含 RuleId 属性的 IRole 接口,然后让您的委托使用 IRole。
如果这不是一个选择,您可以考虑鸭子类型。
You could have both Role and CustomRole implement a IRole interface that includes the RuleId property, then make your delegate use IRole.
If that is not an option, you could look into duck typing.
您可以通过使用反射来获取属性值来实现此目的:
当您使用实体框架时,您也可以这样做:
You could achieve this by using reflection to get the property value:
As you're using Entity Framework you could probably do this as well: