简体   繁体   中英

Generics types in C#

I have three similar methods like this for types Loan, Client and Book. I´ma programming student and I would to know if is possible to pass in the parameter, a generic type which match with this 3 classes, I mean pass between brackets a different type like <T> , having only one method for the three classes. I tried <object> but it doesn´t work.

public void LoadStoreLoans(ObservableCollection<Loan> loansCollection)
{
     IList<Loan> loans = db.Query<Loan>(); //db4o 
     loansCollection = loans != null ? new ObservableCollection<Loan>(loans) : new ObservableCollection<Loan>();
}

Thnks.

Yes, and I suggest to make the collection a return value if you always replace it (which isn't the purpose of the observable collection):

public ObservableCollection<T> LoadStoreCollection<T>()
{
     IList<T> queried = db.Query<T>();
     return queried != null ? new ObservableCollection<T>(queried) : new ObservableCollection<T>();
}

For your first try with object: I have never used db4o but I think it uses the given type parameter ( Load or T ) in the Query function to infer which table to query. Given object yields an error as there is no table for object . You need to specify a surrogate type or better - a generic type parameter - which will be bound the the actual type during execution. When called with T = Loan , the db.Query call can query the Loan table as if you would have written db.Query<Loan> in the first place.

As the collection was a parameter in the question, one could think you'd call the function multiple times for the same T , but: Always reinitialising the same observable collection variable with new disables all benefits a observable collection can give, for example you'll never receive a collection changed event (because you listen on the replaced collection), you can't bind it to a control and if you register some other object to the event, you'll introduce a memory hole :)

I would suggest you to create an empty interface, and implement it for your classes.Just in order to make this constraint possible:

public interface ICommon 
{

}

public void LoadStoreLoans<T>(ObservableCollection<T> loansCollection)
     where T : ICommon
{
     IList<T> loans = db.Query<T>(); //db4o 
     loansCollection = loans != null ? new ObservableCollection<T>(loans) : new ObservableCollection<T>();
}

Try:

    public void LoadStoreLoans<T>(ObservableCollection<T> loansCollection)
    {
        IList<T> loans = db.Query<T>(); //db4o 
        loansCollection = loans != null ? new ObservableCollection<T>(loans) : new ObservableCollection<T>();
    }

It really depends what type parameter db.Query<T>() expects.

I think this is what you are asking for correct?

public void Load<T>(ObservableCollection<T> collection)
{
  var items = db.Query<T>(); //db4o 
  collection = items != null ? new ObservableCollection<T>(items) : new ObservableCollection<T>();
}

Hope that helps :)

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