是否可以引用类似属性的方法?类似于 Validate(x => x.Property)

发布于 2024-08-06 21:24:27 字数 1592 浏览 4 评论 0原文

因为我需要构建一个非常强类型的 & Asp.net MVC 的类型安全项目。但我发现视图页面中的很多语法都不是类型安全的变量。请看下面的例子。

BeginForm("LogOn", "Account")

而不是

Form.BeginForm(x => Account.LogOn)

是否可以创建类似上面的代码?这是一个非常漂亮的强类型源代码。

更新!

我发现用表达式树是不可能的。请看下面的代码。

仅包含 1 个操作的控制器类。 公共类控制器1 { 公共视图 Action1() { 返回空值; } 请

查看表达式代码。

Expression<Action<Controller1>> exp1 = (c => c.Action1());

Expression part = exp1.Body;

if (part.NodeType == System.Linq.Expressions.ExpressionType.Call)
{
    MethodCallExpression callExpression = (MethodCallExpression)part;

    MessageBox.Show(callExpression.Method.DeclaringType.Name + " : " + callExpression.Method.Name);
}

结果是“Controller1:Action1”!

更新2!

以下代码是强类型开始表单的最终代码。

public static MvcForm BeginForm<BaseController>(this HtmlHelper htmlHelper, Expression<Action<BaseController>> routeExp)
{
    Expression part = routeExp.Body;

    if (part.NodeType == System.Linq.Expressions.ExpressionType.Call)
    {
        MethodCallExpression callExpression = (MethodCallExpression)part;
        MethodInfo actionMethod = callExpression.Method;

        return htmlHelper.BeginForm(actionMethod.Name, actionMethod.DeclaringType.Name);
    }

    throw new Exception();
}

因此,您可以使用以下模式来调用它。

<% Html.BeginForm<HomeController>(x => x.Index()); %>

谢谢

Because I need to build a very strongly-typed & type-safed project for Asp.net MVC. But I found that a lot of syntax in View page isn't type-safed variable. Please look at the following example.

BeginForm("LogOn", "Account")

instead of

Form.BeginForm(x => Account.LogOn)

Is it possible to create something like above code? It's a very beutiful strongly-typed sourcecode.

Update!

I found that it's impossible with expression tree. Please look at the following code.

Controller class that contains only 1 action.
public class Controller1
{
public View Action1()
{
return null;
}
}

Please look at expression code.

Expression<Action<Controller1>> exp1 = (c => c.Action1());

Expression part = exp1.Body;

if (part.NodeType == System.Linq.Expressions.ExpressionType.Call)
{
    MethodCallExpression callExpression = (MethodCallExpression)part;

    MessageBox.Show(callExpression.Method.DeclaringType.Name + " : " + callExpression.Method.Name);
}

The result is "Controller1 : Action1"!

Update 2!

The following code is the final code of strongly-typed begin form.

public static MvcForm BeginForm<BaseController>(this HtmlHelper htmlHelper, Expression<Action<BaseController>> routeExp)
{
    Expression part = routeExp.Body;

    if (part.NodeType == System.Linq.Expressions.ExpressionType.Call)
    {
        MethodCallExpression callExpression = (MethodCallExpression)part;
        MethodInfo actionMethod = callExpression.Method;

        return htmlHelper.BeginForm(actionMethod.Name, actionMethod.DeclaringType.Name);
    }

    throw new Exception();
}

So, you can call it by using the following pattern.

<% Html.BeginForm<HomeController>(x => x.Index()); %>

Thanks,

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

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

发布评论

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

评论(2

暮年慕年 2024-08-13 21:24:27

尽管现在可以使用 lambda 表达式,但在这种情况下它们不会为您提供帮助。让我们看一下您的示例:BeginForm 方法接受两个字符串作为输入,并且任何 lambda 表达式都不会改变这一点。

您可以决定将这些字符串封装为一个或多个类的属性,但由于这个原因,它不会变得更加类型安全。

将所有字符串移动到中心位置可能会有所帮助,以减少拼写错误名称的风险,但它们仍然是字符串。

Although lambda expressions are now possible, they are not going to help you in this case. Let's just look at your example: the BeginForm method takes two strings as input, and no amount of lambda expressions are going to change that.

You could decide to encapsulate these strings as properties on one or more classes, but it wouldn't become more type-safe for that reason.

It might help a bit to move all strings to a central place to reduce the risk of misspelling names, but they will still be strings.

极度宠爱 2024-08-13 21:24:27

MVC Futures 支持类似的内容

您可以从 asp.net/mvccodeplex 并获取源代码作为“手册”

警告:作为“未来”的东西,它可能会在未来的版本中发生变化

MVC Futures supports something like this

You can download it from asp.net/mvc or codeplex and get the source code too as a 'manual'

Warning: Being 'futures' stuff, it may change in future releases

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