2

I have a class in dll which looks as follows

    // Summary:
   //     View component Builder base class.
    public abstract class WidgetBuilderBase<TViewComponent, TBuilder> : IHtmlString, IHideObjectMembers
        where TViewComponent : global::Kendo.Mvc.UI.WidgetBase
        where TBuilder : global::Kendo.Mvc.UI.Fluent.WidgetBuilderBase<TViewComponent, TBuilder>
    {
    }

i want to inherit this class in my following class

public abstract class WidgetBuilderBase<TViewComponent, TBuilder>

I surf on net but most on the site i didn't get satisfactory answer most of it says we can not inherit the generic class. why it is not possible to inherit the generic class.

I am trying to do following code

 public abstract class WidgetBuilderBase<TViewComponent, TBuilder> : Kendo.Mvc.UI.Fluent.WidgetBuilderBase<TViewComponent, TBuilder>
    {

    }

But it gives me following error.

The type 'TBuilder' cannot be used as type parameter 'TBuilder' in the generic type or method 'Kendo.Mvc.UI.Fluent.WidgetBuilderBase'. There is no boxing conversion or type parameter conversion from 'TBuilder' to 'Kendo.Mvc.UI.Fluent.WidgetBuilderBase'.

same error for TViewComponent. How to solve this problem... :(

Any help on inheriting generic class?

3
  • 1
    you probably need to add the same generic constraints to the derived type... Commented Dec 3, 2012 at 7:04
  • But i want to make it dynamic at runtime Commented Dec 3, 2012 at 7:06
  • 2
    what do you want to be dynamic? When inheriting a generic class you also inherit it's constraints Commented Dec 3, 2012 at 7:08

2 Answers 2

1

You should declare a derived class like this:

public abstract class WidgetBuilderBase<TViewComponent, TBuilder> 
             : Kendo.Mvc.UI.Fluent.WidgetBuilderBase<TViewComponent, TBuilder>
    where TViewComponent : global::Kendo.Mvc.UI.WidgetBase
    where TBuilder : global::Kendo.Mvc.UI.Fluent.WidgetBuilderBase<TViewComponent, TBuilder>
{

}

You cannot override base class generic constraints in a derived classes.

Sign up to request clarification or add additional context in comments.

Comments

1

WidgetBuilderBase<TViewComponent, TBuilder> has some generic constraints for its type arguments:

where TViewComponent : global::Kendo.Mvc.UI.WidgetBase
where TBuilder : global::Kendo.Mvc.UI.Fluent.WidgetBuilderBase<TViewComponent, TBuilder>

You can't declare descendant class, which can possibly violate these constraints. Hence, you must apply them to descendant class too:

 public abstract class WidgetBuilderBase<TViewComponent, TBuilder> : Kendo.Mvc.UI.Fluent.WidgetBuilderBase<TViewComponent, TBuilder>
    where TViewComponent : global::Kendo.Mvc.UI.WidgetBase
    where TBuilder : global::Kendo.Mvc.UI.Fluent.WidgetBuilderBase<TViewComponent, TBuilder>
    {

    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.