Reputation: 97
public class DataTableRequest
{
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public List<Column> columns { get; set; }
public Search search { get; set; }
public List<Species> data { get; set; }/*here I want to hold list of any type */
public int recordsTotal { get; set; }
}
Above is the model there is property List data in which I want to hold data of both below model. I dont want to create another list property in the DataTableRequest for Chemical. I want a way to create a property which can hold list of both type of model.
public partial class Species
{
public int SpeciesId { get; set; }
public string SpeciesName { get; set; }
public string FileName { get; set; }
public bool IsActive { get; set; }
}
public partial class Chemical
{
public int ChemicalId { get; set; }
public string ChemicalName { get; set; }
public string FileName { get; set; }
public bool IsActive { get; set; }
}
Is there anyway to define a list which can hold list of both Chemical and Species?
Upvotes: 1
Views: 3375
Reputation: 2887
There are two ways you can achieve this.
Define your data
property as List<object>
or just
With this approach every time you'll need to do a lookup based on the type of the object, you'll have to check the actual type of the object using the ArrayList
, which essentially is again an object list.is
operator.
The second option is to do what @ViRuSTriNiTy has said - introduce an artificial base class, but the lookup will need to follow a similar pattern as in the first case. You also have an ability to use a marker interface here (an interface without any members). But again, the only benefit you'll get from this approach is that you'll use generics and reduce the types of objects you'll be able to store in the data
property.
If you have an ability to alter the DataTableRequest
type, it'll be better to add two separate generic lists for two different types. Alternatively, if that's not an option, just add filter properties to the DataTableRequest, as follows:
public class DataTableRequest
{
// the list of existing properties here
public List<object> Data {get; set;}
public IEnumerable<Species> Species
{
get { return this.Data.OfType<Species>(); }
}
public IEnumerable<Chemical> Chemicals
{
get { return this.Data.OfType<Chemical>(); }
}
}
These will just help to iterate over the specific types of objects, in case you need. Of course, you shouldn't add these properties, if you have no such a scenario.
Upvotes: 2
Reputation: 892
You can achieve it with minimal code changes, something like this -
Just add one interface and include list of your classes (it can be extended in case new classes got added)
public interface IListOfAnything
{
List<Species> ListOfSpecies { get; set; }
List<Chemical> ListOfChemical { get; set; }
// List<SomeNewitem> ListOfSomeNewitem { get; set; }
}
public class DataTableRequest
{
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public List<Column> columns { get; set; }
public Search search { get; set; }
public IListOfAnything data { get; set; }/*here you get list both type of model */
public int recordsTotal { get; set; }
}
And your Main() will look something like this -
static void Main(string[] args)
{
DataTableRequest dtr = new DataTableRequest();
dtr.data.ListOfChemical = new List<Chemical>();
dtr.data.ListOfSpecies = new List<Species>();
// dtr.data.ListOfSomeNewitem = new List<SomeNewitem>();
}
This way your current code structure is preserved as near as possible.
Upvotes: 1
Reputation: 32445
You cannot use generics for this purpose, but based on the structure of your class you can introduce common interface of your classes
public interface IData
{
public int Id { get; set; }
public string Name { get; set; }
public string FileName { get; set; }
public bool IsActive { get; set; }
}
public class Species :IData {}
public class Chemical :IData {}
Prefixes "SpeciesId" and "ChemicalId" are redundant because name of the classes will represent required information.
Upvotes: 0
Reputation: 15663
Use generics
void Main()
{
var speciesDataTableRequest = new DataTableRequest<Species>();
var chemicalsDataTableRequest = new DataTableRequest<Chemical>();
}
// Define other methods and classes here
public class DataTableRequest<T>
{
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public List<T> data { get; set; }/*any type now*/
public int recordsTotal { get; set; }
}
public partial class Species
{
public int SpeciesId { get; set; }
public string SpeciesName { get; set; }
public string FileName { get; set; }
public bool IsActive { get; set; }
}
public partial class Chemical
{
public int ChemicalId { get; set; }
public string ChemicalName { get; set; }
public string FileName { get; set; }
public bool IsActive { get; set; }
}
Upvotes: 3
Reputation: 5155
Yes, one way would be to add a base class and inherit from it.
Example:
First add the base class ...
class BaseClass
{}
class Species : BaseClass
{...}
class Chemical : BaseClass
{...}
... and then define your list as
public List<BaseClass> data { get; set; }
Upvotes: 2