访问通用对象的属性 [英] Access to properties of generic object

查看:52
本文介绍了访问通用对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有远程上下文,并且我知道该上下文中的类具有相同的结构.我想避免为每个类创建方法. 例子

I have remote context and I know that classes in this context have the same structure. I want to avoid creating method for each class. Example

public IEnumerable<CommonPersonFeatureValue> GetRegulation()
{
    var query = from c in remoteContext.RegulaminDaneOsobowe
        select new CommonPersonFeatureValue
        {
            PersonId = c.PersonId ?? 0,
            Crated = 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;
}

public IEnumerable<CommonPersonFeatureValue> GetNDA()
{
    var query = from c in remoteContext.NDA
         select new CommonPersonFeatureValue
         {
             PersonId = c.PersonId ?? 0,
             Crated = 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;
}

是否可以创建这样的方法

Is it possible to create method like this

public IEnumerable<CommonPersonFeatureValue> GetCommonPersonFeatureValue<T>(this System.Data.Services.Client.DataServiceQuery<T> items)

问题是我不知道如何正确访问通用对象的属性.一种解决方案是使用此代码

The problem is that I don't know how to properly access property of generic object. One solution is use this code

string newStatus = item.GetType().GetProperty("NewStatus").GetValue(item) as string

但是我认为这是一种薄弱的方法.您是否有更好的解决方案,可以用一种通用方法替换这两种方法?

But I think that it is weak approach. Do you have better solution to replace these two methods in one common method ?

推荐答案

如果可以使RegulaminDaneOsoboweNDA对象实现公共接口(例如IThing),则您的方法将采用该接口的集合:

If you can make the RegulaminDaneOsobowe and NDA objects implement a common interface (e.g. IThing), then your method take a collection of the interface:

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);

这篇关于访问通用对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆