繁体   English   中英

使用反射和GetValue问题

[英]Using Reflection and GetValue problems

我有一个抽象类,看起来像这样:

public abstract class PageObjectsBase
{
    public abstract string FriendlyName { get; }
    public abstract string PageObjectKeyPrefix { get; }
    public abstract string CollectionProperty { get; }
}

还有一个从PageObjectsBase派生的类:

public class PageRatingList : PageObjectsBase
{
    public IList<PageRating> PageRatings { get; set; }

    public PageRatingList()
    {
        this.PageRatings = new List<PageRating>();
    }

    public override string CollectionProperty
    {
        get
        {
            var collectionProperty = typeof(PageRatingList).GetProperties().FirstOrDefault(p => p.Name == "PageRatings");
            return (collectionProperty != null) ? collectionProperty.Name : string.Empty;  
        }
    }

    public override string FriendlyName
    {
        get
        {
            return "Page feedback/rating";
        }
    }

    public override string PageObjectKeyPrefix
    {
        get
        {
            return "pagerating-";
        }
    }
}

还有一个PageRating类,其中PageRatingList.PageRatings持有以下集合:

public class PageRating : PageObjectBase
{
    public int Score { get; set; }
    public string Comment { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

PageRatingList被存储在数据库中(EPiServer的动态数据存储,更具体地说是使用页面对象管理器)。 我需要创建一些报告功能,并且实质上是加载从PageObjectBase派生的所有报告。 当涉及到返回数据时,代码在编译时永远不会知道它要加载什么类型的数据,因此我正在使用反射。 在我的报告班上,我有:

        //this gives me the right type
        var type = Type.GetType("MyNameSpace.PageRatingList", true);

        var startPageData = this._contentRepository.Get<PageData>(startPage);
        PageObjectManager pageObjectManager = new PageObjectManager(startPageData);

        //this loads the instances from the DB
        var props = pageObjectManager.LoadAllMetaObjects()
                         .FirstOrDefault(o => o.StoreName == "Sigma.CitizensAdvice.Web.Business.CustomEntity.PageRatingList");

        //this gives me 4 PropertyInfo objects (IList: PageRatings, string : CollectionProperty, string :FriendlyName, string : PageObjectKeyPrefix)
        var properties = props.Value.GetType().GetProperties();

然后,我可以使用以下方法遍历PropertyInfo对象:

        foreach (var property in properties)
        {
            //extract property value here
        }

我遇到的问题是我无法弄清楚如何获取每个propertyinfo对象的值。 另外,这些属性之一是List类型,同样,直到运行时,我们才知道T的类型。 因此,我还需要一些逻辑来检查PropertyInfo对象之一是否为列表类型,然后提供对列表中每个属性的访问权限-列表的类型为PageRating。

有人可以帮忙吗? 我过去并没有真正使用过反思,所以我正在正确地或错误地在反思中前进!

非常感谢Al

我可能会误解这个问题,但我认为您可能会使用类似以下的内容:

   var props = new PageRatingList(); /*actual instanse of the object, in your case, i think "props.Value" */

   var properties = typeof(PageRatingList).GetProperties();

   foreach (var property in properties)
   {
       if (property.PropertyType == typeof(IList<PageRating>))
       {
           IList<PageRating> list  = (IList<PageRating>)property.GetValue(props);

           /* do */
       }
       else
       {
            object val = property.GetValue(props);
       }
    }

希望这有助于找到您的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM