Вопрос

I have a function which receives Func<T,string> as a parameter and I'm trying to do an overload with Func<T,MvcHtmlString>

The object I am currently sending is Html.Partial("MyPartialPath", MyModel).ToString() and I want to call that method without redundant .ToString() extension

So, this is the original method :

   public void Whatever(Func<T, string> partial)
    {
        this.myProperty= partial;
    }

And I'm trying to create something like this:

   public void Whatever(Func<T, MvcHtmlString> partial)
    {
        this.myProperty= partial; // here fails because myProperty is of type Func<TRow, string>
    }

I want to convert a Func<T,MvcHtmlString> to Func<T,string>.

Here is what I've tried

Func<TRow, string> test= t => partial.ToString(); // it's not working as expected ( doesn't have the same results as the original method )

Is this the correct way to convert, and the error is elsewhere, or I'm not doing the conversion as I should ?

Это было полезно?

Решение

As a quick shot I'd say you have a mistake here:

Func test= t => partial.ToString();

since it references the method partial instead of its result. I think what you probably meant/wanted was:

Func test= t => partial(t).ToString();

BTW: If MvcHtmlString would derive from String, your code would work, since the result type is covariant. But unfortunately this is actually not the case, it was just my 2 cents as a side note.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top