我有两个类(或模型),有一些共同的属性。例如:
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
进行更改,这是我想要避免的。另外,我想避免将属性的名称作为字符串传递,这样我就不会失去intellisense。
我怎样才能做到这一点?显然,在C#中不可能实现多重继承,否则我的问题就变得微不足道了。
编辑:
我编辑了我的问题以反映我的实际需要(虽然我不确定这是否真的有帮助)。我实际上需要在方法T
中创建并返回匿名类型GetAnon()
的对象。要创建对象,我需要属性的名称,以便我可以执行检索(使用SQL)。我不想通过将名称作为字符串传递而失去智慧。此外,在调用方法时,我不知道哪个属性来自哪个模型,我所知道的都是属性名称。
我认为最简单的方法就是使用:
public List<T> GetAnon<T>(Expression<Func<Model1, Model2, T>> selector) {
}
这可能对调用者来说有点不方便,因为他必须记住(或通过反复试验弄清楚)哪种模型具有他需要的属性,但我认为不便很小并且收益足够大(不需要额外的课程) ,正如你所说的那样容易出错)。
用法是:
var propNames = GetAnon((m1, m2) => new { m1.Common1, m1.Prop11, m2.Prop21 });
我认为你可能会过度复杂化选择器功能的使用。我假设您想在那里使用lambda,因为如果在模型中更改它,您不必担心更改字符串中的属性名称。这正是nameof
功能的设计目标。
public List<string> GetMemberNames<T1, T2>(IEnumerable<string> propNames)
{
var commonProps = (from pn in propNames
where
typeof(T1).GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Any(p => p.Name == pn) ||
typeof(T2).GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Any(p => p.Name == pn)
select pn).ToList();
return commonProps;
}
用法:
var cp = GetMemberNames<Model1, Model2>(new[]
{ nameof(Model1.Common1),
nameof(Model2.Common2),
nameof(Model1.Prop11),
nameof(Model2.Prop21),
"SomeUnknownProperty",
});
现在请注意,您不需要拥有任何一个模型的实例来获取属性名称列表。
结果: