The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly

后端 未结 8 1863
灰色年华
灰色年华 2020-12-14 06:21

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:

pu         


        
8条回答
  •  不知归路
    2020-12-14 06:36

    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 GetHtmlAttributes
          (this TModel model, Expression> propertyExpression)
       {
           return new Dictionary();
       }
    }
    

    But you wouldn't be able to have anything similar to virtual, I think.

    EDIT:

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

    class ModelBase
    {
        public virtual IDictionary GetHtmlAttributes
            (Expression> propertyExpression)
        {
            return new Dictionary();
        }
    }
    
    class FooModel : ModelBase
    {
        public override IDictionary GetHtmlAttributes
            (Expression> propertyExpression)
        {
            return new Dictionary { { "foo", "bar" } };
        }
    }
    

提交回复
热议问题