类型参数不能从使用推断。请尝试显式指定类型参数 [英] The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly

查看:3512
本文介绍了类型参数不能从使用推断。请尝试显式指定类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人请澄清我的东西。在我的ASP.NET MVC 2的应用程序,我有一个 BaseViewModel 类,其中包括以下方式:

Could someone please clarify something for me. In my ASP.NET MVC 2 app, I've got a BaseViewModel class which includes the following method:

public virtual IDictionary<string, object> GetHtmlAttributes<TModel, TProperty>
                        (Expression<Func<TModel, TProperty>> propertyExpression)
{
    return new Dictionary<string, object>();
}

这个想法是,每个孩子视图模型可以重写此方法,并提供一组合适的HTML属性的基础上,一些逻辑,在视图中呈现:

The idea being that each child viewmodel can override this method and provide a suitable set of html attributes, based on some logic, to be rendered in the view:

<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
                                                 (model => model.MyProperty)) %>

但如上线使用时,我得到一个编译错误,当我打的看法:

However when used as in the line above, I get a compilation error when I hit the view:

该类型参数的方法 ... BaseViewModel.GetHtmlAttributes&LT;的TModel,TProperty&GT;防爆pression&LT; System.Func&LT;的TModel,TProperty&GT;)'不能从使用推断。尝试显式指定类型参数。

The type arguments for method '...BaseViewModel.GetHtmlAttributes<TModel,TProperty> Expression<System.Func<TModel,TProperty>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

我要做到以下几点:

<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
                             <ChildModel, string>(model => model.MyProperty)) %>

我只是在寻找一些清晰度如何它试图推断出类型,它有没有问题,在的HtmlHelper / TextBoxFor 扩展方法,这样做?

是因为的HtmlHelper 视图将自动成为中指定了同一类型的 ViewUserControl 在在页面的顶部,而我的code可以是任何类型从 BaseViewModel 继承?是否有可能以这样的方式来写这篇文章,它可以推断​​出我的模型/物业类型?

Is it because HtmlHelper in the view will automatically be for the same type as is specified in the ViewUserControl at the top of the page, whereas my code can be for any type inheriting from BaseViewModel? Is is possible to write this in such a way that it can infer my model/property types?

推荐答案

在你的榜样,编译器无法知道什么类型应的TModel 是这样。你可以做一些接近你可能正在尝试使用扩展方法做。

In your example, the compiler has no way of knowing what type should TModel be. You could do something close to what you are probably trying to do with an extension method.

static class ModelExtensions
{
   public static IDictionary<string, object> GetHtmlAttributes<TModel, TProperty>
      (this TModel model, Expression<Func<TModel, TProperty>> propertyExpression)
   {
       return new Dictionary<string, object>();
   }
}

但是,你不能有类似事情虚拟,我想。

编辑:

其实,你可以做虚拟,用自我指涉的仿制药:

Actually, you can do virtual, using self-referential generics:

class ModelBase<TModel>
{
    public virtual IDictionary<string, object> GetHtmlAttributes<TProperty>
        (Expression<Func<TModel, TProperty>> propertyExpression)
    {
        return new Dictionary<string, object>();
    }
}

class FooModel : ModelBase<FooModel>
{
    public override IDictionary<string, object> GetHtmlAttributes<TProperty>
        (Expression<Func<FooModel, TProperty>> propertyExpression)
    {
        return new Dictionary<string, object> { { "foo", "bar" } };
    }
}

这篇关于类型参数不能从使用推断。请尝试显式指定类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆