简体   繁体   中英

Using Reflection and GetValue problems

I have an abstract class that looks like so:

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

And a class that derives from 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-";
        }
    }
}

And a PageRating class which PageRatingList.PageRatings is holding a collection of:

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

The PageRatingList is being stored in a database (EPiServer's Dynamic Data Store, more specifically using the Page Object Manager). I need to create some reporting functionality and am essentially loading all reports that derive from PageObjectBase. When it comes to returning the data, the code will never know at compile time what type of data it is to load, so I am using Reflection. In my reporting class I have:

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

I can then iterate through the PropertyInfo objects using:

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

The issue I am having is that I cannot figure out how to get the value of each of the propertyinfo objects. In addition, one of those properties is type List and again we wont know the type of T until runtime. So I also need some logic that checks if one of the PropertyInfo objects is of type List and then provides access to each of the properties in the List - the List being of type PageRating.

Can anyone help here? I've not really used reflection in the past so I am winging my way through it, rightly or wrongly!

Many thanks Al

I may be missunderstanding the problem, but i think you may use something like this:

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

Hope this helps to find your solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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