Why does C # not support an object with an interface as a parameter?

I have the following class declaration:

public class EntityTag : BaseEntity, ITaggable

I have an Html helper method:

public static string TagCloud(this HtmlHelper html, IQueryable<ITaggable> taggables, 
  int numberOfStyleVariations, string divId)

This is my ASP.NET MVC ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IQueryable<EDN.MVC.Models.EntityTag>>" %>
<%@Import Namespace="EDN.MVC.Helpers" %>
<%= Html.TagCloud(Model, 6, "entity-tags") %>

When I pass to the IQueryable ascx collection, I get this error:

Compiler error message: CS1928: "System.Web.Mvc.HtmlHelper>" does not contain a definition for "TagCloud", and the best extension method is "EDN.MVC.Helpers.EdnHelpers.TagCloud (System.Web.Mvc) .HtmlHelper, System.Linq.IQueryable, int, string) 'has some invalid arguments

If I try to explicitly convert a collection of objects using this:

    public static string TagCloud(this HtmlHelper html, IQueryable<Object> taggables, int numberOfStyleVariations, string divId)
    {
        var tags = new List<ITaggable>();
        foreach (var obj in taggables)
        {
            tags.Add(obj as ITaggable);
        }
        return TagCloud(html, tags.AsQueryable(), numberOfStyleVariations, divId);
    }

I get the same error - the values ​​I pass do not like the compiler.

EntityTag IQueryable? ? - . ().

+5
3

, IQueryable , IQueryable<ITaggable>, "", CS1928 ( , ).

, IQueryable<object> ( ), AsQueryable , :

public static string TagCloud(this HtmlHelper html, IQueryable taggables, int numberOfStyleVariations, string divId)  
{  
    var tags = new List<ITaggable>();  
    foreach (var obj in taggables)  
    {  
        tags.Add(obj as ITaggable);  
    }  
    return TagCloud(html, tags.AsQueryable<ITaggable>(), numberOfStyleVariations, divId);  
}  

, IQueryable<T> IQueryable, , IQueryable IQueryable<T>, . , .. "" IQueryable, , , IQueryable<T> ( IQueryable<T>, , IQueryable).

Per Craig Stuntz, LINQ: <%= Html.TagCloud(Model.Select(t => (ITaggable)t), 6, "entity-tags") %>. <%= Html.TagCloud(Model.Cast<ITaggable>(), 6, "entity-tags") %>, .

+5

# 4.0 . " # 4"

+2

Your EntityTagclass IQueryable, however, the compiler does not know that your tag list is actually a list of objects EntityTag, it only knows that it is a list of objects that implement ITaggable, which are probably not IQueryable.

+1
source

All Articles