简体   繁体   中英

C# Generics method - calling base class constructor on a type

I have an Object called CAR declared as follows:

public class Car{
   public int NumberOfWheels;
}  

and using another class I can retrieve three of its subclasses.
As follows:

public TruckA getCarA(int param){
   return new TruckA () {NumberOfWheels = param} 
}
public TruckB getCarB(int param){
   return new TruckB () {NumberOfWheels = param} 
}
public TruckC getCarC(int param){
   return new TruckC () {NumberOfWheels = param} 
}

How do I write in using generics ?
(I know it is too simple, I need an example for a more complicated case)

I want to create a method that creates a new (T), enters the parameter and returns it.

something like

public TruckC getCarC(int param){
   return getTruck<TruckC>(param)
}

Only that :
1. TruckA/B/C dont have constructor.
2. Car has no constructor. initialized using {}.
3. The method should receive only classes that derive from Car so that it can use its initialization ( {} ).

(something equivalent to ?? extends ? in java generics)
Possible ?

Thanks.

private static T GetTruck<T>(int param) where T : Car, new()
{
    return new T() {NumberOfWheels = param} ;
}

try this :

public T getCarC<T>(int param) where T : Car, new()
        {
            T truck=new T();
            truck.NumberOfWheels = param;

            return track;
        }

Generics are the most powerful feature of C# 2.0. Generics allow you to define type-safe data structures, without committing to actual data types.

When deriving from a generic base class, you must provide a type argument instead of the base-class's generic type parameter:

public class BaseClass<T>
{...}
public class SubClass : BaseClass<int>
{...}

If the subclass is generic, instead of a concrete type argument, you can use the subclass generic type parameter as the specified type for the generic base class:

public class SubClass<T> : BaseClass<T> 
{...}

When using the subclass generic type parameters, you must repeat any constraints stipulated at the base class level at the subclass level. For example, derivation constraint:

public class BaseClass<T>  where T : ISomeInterface 
{...}
public class SubClass<T> : BaseClass<T> where T : ISomeInterface
{...}

Or constructor constraint:

public class BaseClass<T>  where T : new()
{   
   public T SomeMethod()
   {
      return new T();
   }
}
public class SubClass<T> : BaseClass<T> where T : new() 
{...}

You need to do this using generic type constraints. These let you make requirements about the type that is passed.

It would look something like this:

public static T GetCar<T>(int param)
    where T : Car, new() // Must inherit from Car, must have a parameterless constructor
{
    return new T() { NumberOfWheels = param };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM