fail to inherit generic classes

Asked 1 years ago, Updated 1 years ago, 342 views

Generic class inheritance doesn't work out as expected.
The implementation itself has been completed, but I am looking for a way to simplify the code.
When I searched, I couldn't translate the method I wanted into a language, so I decided to ask a question here a question.

■Preparation

class Weapon{}
class Bomb: Weapon {}

class MachineBase<T>where T:Weapon{}
class machine:MachineBase<Bomb>{}

Inherit this code as follows:


現状Current state code

class RobotBase<T1,T2>where T1:MachineBase<T2>where T2:Weapon{}
class Robot —RobotBase<Machine, Bomb>{}

Although the implementation itself is successful, it takes a lot of effort to specify <T1,T2> and two types each time you inherit a class.
It is clear that the Machine class specifies the Weapon class as the generic type.
Therefore, I thought there might be a way to complete the model designation with just one, so I changed it as follows.

改 Modified code (error)

class RobotBase<T>where T:MachineBase<Weapon>{}
class Robot —RobotBase<Machine>{}// Error

Reduced the type argument to one.
Because the RobotBase<T> class must have some type, we have specified Weapon as the generic type, which is the constraint of the type itself.
Then, the error described below appears.

■error message

"Error CS0311 type 'Machine' cannot be used as a type parameter 'T' in generic type or method 'RobotBase<T>'. There is no implicit reference translation from 'Machine' to 'MachineBase<Weapon>'.    "

Machine inherits MachineBase<Bomb> and Bomb inherits Weapon, so RobotBase <T> inherits
T=Machine=MachineBase<Bomb>=MachineBase<Weapon>
I thought it would be interpreted as , but I got an error.

class Robot:RobotBase<Machine>{}// error

The above statement that is causing the error is

class Robot:RobotBase<MachineBase<Weapon>>{}

The error disappeared when I rewritten it.

class Robot:RobotBase<MachineBase<Bomb>>{}// error

Rewrite to get an error again.

When implementing RobotBase<T>, I think the reason for the error is that Weapon was typed.
In order to avoid errors, if you try to throw the specification of the type argument into the derived class, you will end up with two specification of the type argument as shown in code の.
Is there a notation that leaves the implementation of arguments to the derived class but does not increase the number of arguments?

■What questions would you like to ask

How to use only one type argument for inheritance like the one shown in Or, I would like to know how to implement it so that there are no errors in で.
If both are not possible, I don't mind changing the code in the text from the root, so please tell me how to inherit the arguments as low as possible.

c#

2023-01-22 22:06

1 Answers

I understand that MachineBase is a generic class with type arguments, but do you need type arguments in the first place? Can't I keep Weapon?

I think it is difficult to simplify with the current class configuration.So I changed my perspective

Each time you inherit a class, you need to specify <T1,T2> and two types.

Where does it take time and effort? If it doesn't take much time, I think it's okay to keep the type arguments T1 and T2.
For example, the Select extension method frequently used in LINQ is

public static System.Collections.Generic.IEnumerable<Tresult>TSource,Tresult> (this System.Collections.Generic.IEnumerable<TSource,Func<TSource,int,Tresult>)
public static System.Collections.Generic.IEnumerable<Tresult>Select>TSource,Tresult>(this System.Collections.Generic.IEnumerable>TSource,Func<TSource,Tresult>Selector);

It has very complex type arguments, but you can almost omit them and rarely say they are labor-intensive.

Why don't you keep the class configuration intact and make it easy to prepare auxiliary methods?


2023-01-22 22:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.