I have two classes (or models) that have some common properties. For instance:
public class Model1
{
public int Common1 { get; set; }
public int Common2 { get; set; }
public int Prop11 { get; set; }
public int Prop12 { get; set; }
}
public class Model2
{
public int Common1 { get; set; }
public int Common2 { get; set; }
public int Prop21 { get; set; }
public int Prop22 { get; set; }
}
I need to write a method that can accept an element selector expression, where the element to select can come from either of two models. Here is what I mean:
public List<T> GetAnon<T>(Expression<Func<??, T>> selector)
{
}
var anonObj = GetAnon(m => new { m.Common1, m.Prop11, m.Prop21 });
Note that the transmitted selectorselects both from Model1and from Model2.
My current solution: Create a model with common properties, such Modelas c Model1and Model2inherit, Modelwith the rest of the properties in separate models. Then create another model, say TempModel, which inherits Modeland adds non-common properties to it. For instance:
public class Model
{
public int Common1 { get; set; }
public int Common2 { get; set; }
}
public class Model1 : Model
{
public int Prop11 { get; set; }
public int Prop12 { get; set; }
}
public class Model2
{
public int Prop21 { get; set; }
public int Prop22 { get; set; }
}
public class TempModel : Model
{
public int Prop11 { get; set; }
public int Prop12 { get; set; }
public int Prop21 { get; set; }
public int Prop22 { get; set; }
}
public List<T> GetAnon<T>(Expression<Func<TempModel, T>> selector)
{
}
, Model1 Model2 , , TempModel, , , , intellisense.
? , #, .
Edit:
, ( , ). T GetAnon(). , ( SQL). , . , , , , , .