C# 访问泛型对象的属性

C# 访问泛型对象的属性,c#,generics,C#,Generics,我有远程上下文,我知道这个上下文中的类具有相同的结构。我希望避免为每个类创建方法。 范例 但我认为这是一种软弱的做法。您是否有更好的解决方案将这两种方法替换为一种通用方法?如果您可以使RegulationAnoSobowe和NDA对象实现一个通用接口(例如IThing),则您的方法将获取该接口的集合: public IEnumerable<CommonPersonFeatureValue> QueryThings<TThing>(IQueryable<TThing&

我有远程上下文,我知道这个上下文中的类具有相同的结构。我希望避免为每个类创建方法。 范例


但我认为这是一种软弱的做法。您是否有更好的解决方案将这两种方法替换为一种通用方法?

如果您可以使
RegulationAnoSobowe
NDA
对象实现一个通用接口(例如
IThing
),则您的方法将获取该接口的集合:

public IEnumerable<CommonPersonFeatureValue> QueryThings<TThing>(IQueryable<TThing> things)
    where TThing : IThing
{
    var query = from c in things
                select new CommonPersonFeatureValue
                {
                    PersonId = c.PersonId ?? 0,
                    Created = c.Created ?? DateTime.MinValue,
                    CurStatus = String.IsNullOrEmpty(c.CurStatus) ? c.CurStatus : String.Empty,
                    NewStatus = String.IsNullOrEmpty(c.NewStatus) ? c.NewStatus : String.Empty,
                    Modified = c.Modified ?? DateTime.MinValue
                };
    return query;
}

感谢您的回复,但我不能让这些类实现公共接口,因为它来自外部源。我只能通过web服务访问。所以我需要在代码中映射它。
public IEnumerable<CommonPersonFeatureValue> GetCommonPersonFeatureValue<T>(this System.Data.Services.Client.DataServiceQuery<T> items)
string newStatus = item.GetType().GetProperty("NewStatus").GetValue(item) as string
public IEnumerable<CommonPersonFeatureValue> QueryThings<TThing>(IQueryable<TThing> things)
    where TThing : IThing
{
    var query = from c in things
                select new CommonPersonFeatureValue
                {
                    PersonId = c.PersonId ?? 0,
                    Created = c.Created ?? DateTime.MinValue,
                    CurStatus = String.IsNullOrEmpty(c.CurStatus) ? c.CurStatus : String.Empty,
                    NewStatus = String.IsNullOrEmpty(c.NewStatus) ? c.NewStatus : String.Empty,
                    Modified = c.Modified ?? DateTime.MinValue
                };
    return query;
}
var ndaQuery = QueryThings(remoteContext.NDA);
var regulaminDaneOsoboweQuery = QueryThings(remoteContext.RegulaminDaneOsobowe);