【发布时间】:2018-02-25 15:45:14
【问题描述】:
我有两个具有一些共同属性的类(或模型)。例如:
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; }
}
我需要编写一个可以接受成员选择器表达式的方法,其中要选择的成员可以来自两个模型中的任何一个。这就是我的意思:
public List<T> GetAnon<T>(Expression<Func<??, T>> selector) // <-- intriguing part
{
// codes to extract property names from selector and create the return object of type T
}
// And this is an example usage of the method
// At this point, I don't know which model Common1, Prop11 or Prop21 belongs to
var anonObj = GetAnon(m => new { m.Common1, m.Prop11, m.Prop21 });
// anonObj = { Common1 = VALUE_1, Prop11 = VALUE_2, Prop21 = VALUE_3 }
请注意,selector 传递的选择既来自Model1,也来自Model2。
我当前的解决方案:创建一个具有公共属性的模型,例如 Model,让 Model1 和 Model2 继承 Model 以及各个模型中的其余属性。然后创建另一个模型,比如TempModel,它继承Model,并为其添加非公共属性。像这样:
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; }
}
// Finally, first type parameter can be TempModel
public List<T> GetAnon<T>(Expression<Func<TempModel, T>> selector)
{
// codes to extract property names from selector and create the return object of type T
}
这种方法的问题是,如果Model1 和Model2 发生变化,我将不得不记住对TempModel 也进行更改,这是我想避免的.另外,我想避免将属性名称作为字符串传递,以免失去智能感知。
我怎样才能做到这一点?显然,在 C# 中不能进行多重继承,否则我的问题将变得微不足道。
编辑:
我已经编辑了我的问题以反映我的实际需求(尽管我不确定这是否真的有帮助)。我实际上需要在方法GetAnon() 中创建并返回一个匿名类型T 的对象。要创建对象,我需要属性的名称,以便我可以执行检索(使用 SQL)。我不想通过将名称作为字符串传递而失去智能。另外,在调用方法时,我不知道哪个属性来自哪个模型,我只知道属性名称。
【问题讨论】:
-
为什么需要成员名单?使用接口(支持多重继承)会有帮助吗?
-
@Adrian 这是个好主意,我也想过。恐怕这无济于事,因为这些类实际上是模型。此外,我在我正在从事的项目中允许将这些模型更改为接口。现在,关于您的问题,为什么我需要这些名称,我是否应该编辑问题并添加详细信息,但不知何故,我认为这不会有太大帮助。
-
也许只是通过 Expression
> 选择器? -
@Evk 这是个好主意,但就像我在 Ron 的回答中评论说的那样,我不知道在调用它的地方哪个属性来自哪个模型。但是,我认为这毕竟是一个不错的解决方案。
-
那我真的不明白你在做什么。我的意思是我看不出你可以用你的
TempModel方法做什么,而你不能用我的方法做。你的GetMemberNames(x => x.Common1)是我的GetMemberNames((m1,m2) => m1.Common1)(或者m2.Common1,没关系),以此类推。
标签: c# inheritance multiple-inheritance expression-trees