FlockOfTanks
FlockOfTanks

Reputation: 67

Generic c# property type

I have three classes, two of which inherit from a base class, and the third which I would like to reference one of the other two depending on the state of the application.

public class Batch
{        
    public Batch() { }
}

public class RequestBatch : Batch
{
    public RequestBatch(string batchJobType) : base(batchJobType) { }

    public override int RecordCount
    {
        get { return Lines.Count; }
    }
}

public class ResponseBatch : Batch
{       
    public ResponseBatch(string batchJobType) : base(batchJobType) { }

    public ResponseBatch(int BatchJobRunID)
    { }
}

Sometimes I have an instance of Child1 instantiated, and sometimes I need Child2. However, I have model that I want to pass around my application to keep everything in one place, but I want a way to make the property that holds Child1 and Child2 generic, for example:

public class BatchJob {
   public List<Batch> Batches { get; set; }
}

And then later do this

public List<RequestBatch> GetBatches(...) {}

var BatchJob = new BatchJob();
BatchJob.Batches = GetBatches(...);

However, the compiler yells at me saying it can't implicitly convert Child1 to (its base type) Parent.

I get red squiggles under "= GetBatches(...." saying "Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'

Is there a way to generify the Property so it can take any abstract of type Parent?

Thanks!

Upvotes: 4

Views: 159

Answers (2)

Matthew Whited
Matthew Whited

Reputation: 22433

One option...

public new IEnumerable<RequestBatch> GetBatches(...) {
    get 
    {
        return base.GetBatches(...).OfType<RequestBatch>();
    }
}

Another...

If you don't need to modify the collection then just change from List<T> to IEnumerable<T>

More Info...

Upvotes: 0

Michael Mairegger
Michael Mairegger

Reputation: 7301

The code snipped you show does work. There is no compiler error:

class Program
{
    static void Main()
    {
        var rj = new RunningJob();
        rj.Property = new Child1();
        rj.Property = new Child2();
    }
}
public class RunningJob { 
    public Parent Property { get; set; }
}
public class Parent {    }
public class Child1 : Parent {    }
public class Child2 : Parent {    }

The only issue that comes with this code is that Property is of type Parent. So you cannot call methods that are specific for Child1/Child2. This can be done using constraints on generic type parameters on class RunningJob :

public class RunningJob<TParent> where TParent : Parent
{
    public TParent Property { get; set; }
}

Hence, now it is ensured that Property is of type Parent or any derived types.

Upvotes: 1

Related Questions