开发者

The type parameter cannot be used with type arguments

I wanted to code a helper method in Unit test project, which will initialize the presenter set the views in开发者_开发问答stance to it and set the presenter state.

It threw me the exception:

the type parameter cannot be used with type arguments

Code:

public static **TPresenter<TView>** Initialize<TPresenter,TView>()
    where TPresenter: BasePresenter<TView>, new()
    where TView : new()
{
}

After couple of minutes I found the issue was with my return type TPresenter<Tview>

I read few posts which didn't clearly explain Why I'm not be able to say T1<T2>

I was forced to make the presenter assignment through reference parameter. Any explanations are welcome!


Basically there's no way of saying that a type parameter is itself a generic type with a particular number of type parameters - which you need to be able to do in order to make TPresenter<TView> make sense.

It's not clear what you mean by making it work via a reference parameter - whatever type you used for that ref parameter should be fine as a return type too. My guess is that it was just of type TPresenter, not TPresenter<TView>.


There is no such thing as a TPresenter<TView> it is meaningless. TPresenter is just a placeholder, until it is constrained by the where it could be anything, e.g. there is no int<tview> so you can't have that. Once you add the constraint it means it has to be a BasePresenter<TView> or some derived type so it will always be a Something<TView> so again TPresenter<TView> is meaningless.


This is an old one, but I hit it too. In the Class definition, just use the single type, then multiple types where you use it. E.g:

public class Template1<T>{}

void SomeFunc()
{
    <Template1<SomeClass1,SomeClass2>> someValue = new <Template1<SomeClass1,SomeClass2>>()
}

//or even:
void SomeOtherFunc<U,V>()
{
    <Template1<U,V>> someValue = new <Template1<U,V>>();
}


I was getting similar error in my code. @Jon Skeet correctly points to the right direction. The return type is already generic, as specified by TPresenter : BasePresenter<TView>. So we can simply use it as TPresenter instead of TPresenter<TView>.

public class BasePresenter<T>
{

}

public class Demo
{
    public static TPresenter Initialize<TPresenter, TView>() where TPresenter: BasePresenter<TView>, new()
    {
        return null;
    }
}


Small addition, I came here because i was trying to write an extension method;

        public static T AutoJoinGroup<T, TD>(this T<TD> groupHubClientBase, string groupName)
            where T : GroupHubClientBase<TD>
        {
...
        }

As you can see, I tried to use T<TD> which is incorrect, you can just use T

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜