组合两个 类 的成员选择器表达式
Member selector expression combining two classes
我有两个 类(或者更确切地说是模型)具有一些共同的属性。例如:
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; }
}
我需要编写一个可以接受成员 select 或表达式的方法,其中 select 的成员可以来自两个模型中的任何一个。这就是我的意思:
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
传递了 select。
我目前的解决方案: 创建一个具有共同属性的模型,比如说 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()
中创建并 return 一个匿名类型 T
的对象。要创建对象,我需要属性的名称以便我可以执行检索(使用 SQL)。我不想通过将名称作为字符串传递来失去智能。另外,在调用方法时,我不知道哪个 属性 来自哪个模型,我只知道 属性 名称。
我想你可能over-complicating使用了选择器功能。我假设您想在那里使用 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",
});
请注意,现在您无需拥有任一模型的实例即可获取 属性 名称列表。
结果:
我认为最简单的方法就是使用:
public List<T> GetAnon<T>(Expression<Func<Model1, Model2, T>> selector) {
}
这对调用者来说可能有点不方便,因为他必须记住(或通过反复试验弄清楚)哪个模型有他需要的属性,但我认为不便是次要的,但有好处足够大(不需要额外的 类,正如你所说的 error-prone 维护)。
用法为:
var propNames = GetAnon((m1, m2) => new { m1.Common1, m1.Prop11, m2.Prop21 });
我有两个 类(或者更确切地说是模型)具有一些共同的属性。例如:
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; }
}
我需要编写一个可以接受成员 select 或表达式的方法,其中 select 的成员可以来自两个模型中的任何一个。这就是我的意思:
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
传递了 select。
我目前的解决方案: 创建一个具有共同属性的模型,比如说 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()
中创建并 return 一个匿名类型 T
的对象。要创建对象,我需要属性的名称以便我可以执行检索(使用 SQL)。我不想通过将名称作为字符串传递来失去智能。另外,在调用方法时,我不知道哪个 属性 来自哪个模型,我只知道 属性 名称。
我想你可能over-complicating使用了选择器功能。我假设您想在那里使用 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",
});
请注意,现在您无需拥有任一模型的实例即可获取 属性 名称列表。
结果:
我认为最简单的方法就是使用:
public List<T> GetAnon<T>(Expression<Func<Model1, Model2, T>> selector) {
}
这对调用者来说可能有点不方便,因为他必须记住(或通过反复试验弄清楚)哪个模型有他需要的属性,但我认为不便是次要的,但有好处足够大(不需要额外的 类,正如你所说的 error-prone 维护)。
用法为:
var propNames = GetAnon((m1, m2) => new { m1.Common1, m1.Prop11, m2.Prop21 });