I have Blazor component that has only a razor file (without razor.cs code behind) and want the component to be inherited from an interface, first I tried the following :
MyComponent.razor :
@inherits IMyInterface
Then after build I got an error in the .cs generated code(by Visual Studio) and I realized the component is only derived form my interface and not ComponentBase
class. Here is the generated code:
...
public partial class MyComponent: IMyInterface
{
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
}
#pragma warning restore 1998
}
...
so I got the 'no suitable method found to override' error.
then I tried the following codes and got a compile error with all of them :
// 1) multiple inheritance
@inherits ComponentBase,IMyInterface
// 2) adding semicolon
@inherits ComponentBase,IMyInterface;
// 3) mutiple @inherits directive
@inherits ComponentBase
@inherits IMyInterface
The problem solved by adding a code behind file MyComponent.razor.cs and write the inheritance code there but I wonder that is it possible to have multiple inheritance of interfaces in a razor file?
Multiple inheritance is not possible in C#, and Blazor template generates C# classes. So you can not inherits
multiple base classes. However, you can @implements
multiple interfaces.
An example would be as follows:
@implements IEventListener
@implements IInterface
I guess it is just a confusion over terms. You inherit
a class but you implement
an interface