C#方法获取具有相同属性的类型列表

C#方法获取具有相同属性的类型列表,c#,list,C#,List,我有几种不同的类型,它们都有一个共同的属性: public class A { public int SomeCommonProperty { get; set; } /* * some other stuff */ } public class B { public int SomeCommonProperty { get; set; } /* * some other stuff */ } 我希望有一个方法,可以获取具有此属性的这些对象的

我有几种不同的类型,它们都有一个共同的属性:

public class A {
  public int SomeCommonProperty { get; set; }

   /*
    * some other stuff
    */
}

public class B {
  public int SomeCommonProperty { get; set; }

  /*
   * some other stuff
   */
}
我希望有一个方法,可以获取具有此属性的这些对象的列表,这样我就可以遍历该列表,并将该属性与另一个参数进行比较,如:

public static List<T> MethodToTakeListOfAboveTypes<T>(this List<T> destinationList, List<T> sourceList, string someProp)
{
  if (sourceList != null && sourceList.Exists(tab => tab.SomeCommonProperty  == someProp))
  {
    destinationList = sourceList.Where(tab => tab.SomeCommonProperty == someProp).ToList();
  }
  return destinationList;
}
public static List MethodToTakeListOfAboveTypes(此列表目标列表、列表源列表、字符串someProp)
{
如果(sourceList!=null&&sourceList.Exists(tab=>tab.SomeCommonProperty==someProp))
{
destinationList=sourceList.Where(tab=>tab.SomeCommonProperty==someProp.ToList();
}
回归宿命论者;
}
上面的代码不起作用,因为T没有“SomeCommonProperty”的定义,这是有意义的。 我只想传递一个具有该属性的更通用的对象,这样就不必为每种类型创建相同的方法。我就是不能理解正确的语法。这有意义吗

我知道我应该将SomeCommonProperty字段放入基类并继承,但出于某种原因,这似乎也不起作用

我知道我应该将SomeCommonProperty字段放入基类并继承,但出于某种原因,这似乎也不起作用

您需要添加一个通用约束,然后它就可以正常工作了

where T : CommonType
我知道我应该将SomeCommonProperty字段放入基类并继承,但出于某种原因,这似乎也不起作用

您需要添加一个通用约束,然后它就可以正常工作了

where T : CommonType

使所有类实现某个接口(或从某个基类继承此属性),例如

然后可以对泛型参数类型设置接口/类约束:

public static List<T> MethodToTakeListOfAboveTypes<T>(          
   this List<T> destinationList, List<T> sourceList, string someProp)
    where T: ICommonInterface
{
   // ...
}
publicstaticlist方法创建上述类型的列表(
此列表目标列表、列表源列表、字符串someProp)
其中T:icommon接口
{
// ...
}
注意:您可以避免检查源中是否存在任何带有someProp的项(在最坏的情况下,您必须枚举sourceList两次)。只需进行过滤并检查是否有任何结果

public static List<T> MethodToTakeListOfAboveTypes<T>(
     this List<T> destinationList, List<T> sourceList, string someProp)
     where T: ICommonInterface
{
    if (sourceList == null)
        return destinationList;

    var filtered = sourceList.Where(s => s.SomeCommonProperty == someProp).ToList();
    return filtered.Any() ? filtered : destinationList;
}
publicstaticlist方法创建上述类型的列表(
此列表目标列表、列表源列表、字符串someProp)
其中T:icommon接口
{
if(sourceList==null)
回归宿命论者;
var filtered=sourceList.Where(s=>s.SomeCommonProperty==someProp.ToList();
返回filtered.Any()?filtered:destinationList;
}

使所有类实现某个接口(或从某个基类继承此属性),例如

然后可以对泛型参数类型设置接口/类约束:

public static List<T> MethodToTakeListOfAboveTypes<T>(          
   this List<T> destinationList, List<T> sourceList, string someProp)
    where T: ICommonInterface
{
   // ...
}
publicstaticlist方法创建上述类型的列表(
此列表目标列表、列表源列表、字符串someProp)
其中T:icommon接口
{
// ...
}
注意:您可以避免检查源中是否存在任何带有someProp的项(在最坏的情况下,您必须枚举sourceList两次)。只需进行过滤并检查是否有任何结果

public static List<T> MethodToTakeListOfAboveTypes<T>(
     this List<T> destinationList, List<T> sourceList, string someProp)
     where T: ICommonInterface
{
    if (sourceList == null)
        return destinationList;

    var filtered = sourceList.Where(s => s.SomeCommonProperty == someProp).ToList();
    return filtered.Any() ? filtered : destinationList;
}
publicstaticlist方法创建上述类型的列表(
此列表目标列表、列表源列表、字符串someProp)
其中T:icommon接口
{
if(sourceList==null)
回归宿命论者;
var filtered=sourceList.Where(s=>s.SomeCommonProperty==someProp.ToList();
返回filtered.Any()?filtered:destinationList;
}

如果基类包含具有该名称的属性,则只需告诉泛型方法该类型必须从该基类继承:

public static List<T> MethodToTakeListOfAboveTypes<T>(this List<T> destinationList, List<T> sourceList, string someProp) where T : BaseClass

如果基类包含具有该名称的属性,则只需告诉泛型方法该类型必须从该基类继承:

public static List<T> MethodToTakeListOfAboveTypes<T>(this List<T> destinationList, List<T> sourceList, string someProp) where T : BaseClass

使用每个类实现的属性创建接口,然后向泛型方法添加约束:

public interface ICommonProperty
{
    int SomeCommonProperty {get;set;}
}

public class A : ICommonProperty
{
    public int SomeCommonProperty { get; set; }
}

public class B : ICommonProperty
{
    public int SomeCommonProperty { get; set; }
}

public static List<T> MethodToTakeListOfAboveTypes<T>(this List<T> destinationList, List<T> sourceList, string someProp) where T : ICommonProperty
公共接口ICommonProperty
{
int SomeCommonProperty{get;set;}
}
公共A类:ICommonProperty
{
公共int SomeCommonProperty{get;set;}
}
公共B类:ICommonProperty
{
公共int SomeCommonProperty{get;set;}
}
public static List MethodToTakeListOfAboveTypes(此列表目标列表、列表源列表、字符串someProp),其中T:ICommonProperty

使用每个类实现的属性创建一个接口,然后向泛型方法添加一个约束:

public interface ICommonProperty
{
    int SomeCommonProperty {get;set;}
}

public class A : ICommonProperty
{
    public int SomeCommonProperty { get; set; }
}

public class B : ICommonProperty
{
    public int SomeCommonProperty { get; set; }
}

public static List<T> MethodToTakeListOfAboveTypes<T>(this List<T> destinationList, List<T> sourceList, string someProp) where T : ICommonProperty
公共接口ICommonProperty
{
int SomeCommonProperty{get;set;}
}
公共A类:ICommonProperty
{
公共int SomeCommonProperty{get;set;}
}
公共B类:ICommonProperty
{
公共int SomeCommonProperty{get;set;}
}
public static List MethodToTakeListOfAboveTypes(此列表目标列表、列表源列表、字符串someProp),其中T:ICommonProperty

如果你可以继承(就像你说的那样),你应该这样做。但是,如果出于某种原因,您需要遍历一组没有源代码的对象,并访问已知的某些属性,您可以使用:

class-foo
{
公共字符串myProp=“foo”;
}
分类栏
{
公共字符串myProp=“bar”;
}
班级计划
{
静态void Main(字符串[]参数)
{
列表=新列表();
添加(新的foo());
添加(新条());
foreach(列表中的动态o)
{
控制台写入线(o.myProp);
}
}
}

您还可以在
动态
上调用任意方法。如果属性/方法不存在,则会出现异常。

如果可以使用继承(正如您所说的那样),则应该这样做。但是,如果出于某种原因,您需要遍历一组没有源代码的对象,并访问已知的某些属性,您可以使用:

class-foo
{
公共字符串myProp=“foo”;
}
分类栏
{
公共字符串myProp=“bar”;
}
班级计划
{
静态void Main(字符串[]参数)
{
列表=新列表();
添加(新的foo());
添加(新条());
foreach(动态o-in-list