Not able to retreive Items of BusinessListBase using reflection

Not able to retreive Items of BusinessListBase using reflection

Old forum URL: forums.lhotka.net/forums/t/9730.aspx


stiv posted on Tuesday, November 09, 2010

Hello,

I have the following problem, using reflection i need to read each property of a BusinessObject to find if it implements
a given property to read it's current value, if the property that i'm reading is a BusinessListBase (IEditableCollection) i need to loop through
each item to find it they implement the wanted property...etc

But i don't know how to retrieve each item using reflection foreach doesn't work with IEditableCollection

I'm using the following to code to know it the object is a BusinessListBase

Type objectTypet = lista.GetType();
PropertyInfo[] pi = objectType.GetProperties();
          
foreach (PropertyInfo propInfo in pi)
{               
    object obj = p.GetValue(objecto, null);
 if (obj is IEditableCollection)
    {
        Type t = lista.GetType();
        PropertyInfo[] pi = t.GetProperties();
           
        PropertyInfo count = t.GetProperty("Count");
        Int32 numCount = (int)count.GetValue(lista, null);
  
  *** Not able to read Items****
    }
}

Any help will be appreciated.

Thanki'm advance,

Stiv

RockfordLhotka replied on Tuesday, November 09, 2010

You probably just need to cast the collection to a different collection type - such as ICollection, IEnumerable, etc.

stiv replied on Tuesday, November 09, 2010

Thanks Rocky for your quick answer.

I resolved the problem. Here is the code  if it helps someone.

Regards,

Stiv

public static StatusTransaction TransactionChecker(object objecto)
        {
            Type objectType = objecto.GetType();

            MemberInfo[] members =
                objectType.FindMembers(MemberTypes.Property,
                   BindingFlags.Public |
                   BindingFlags.Static |
                   BindingFlags.NonPublic |
                   BindingFlags.Instance |
                   BindingFlags.DeclaredOnly,
                   Type.FilterName, "BreintekStatus");
           
            if ((members.Count() == 1) && ((int)((PropertyInfo)members[0]).GetValue(objecto, null) <= 0))
            {

                return new StatusTransaction(false, (string)(objectType.GetProperty("BreintekStatusMessage")).GetValue(objecto, null), objecto, objectType);
            }
            else
            {
                PropertyInfo[] propInfos = objectType.GetProperties();

                foreach (PropertyInfo propInfo in propInfos)
                {
                    object obj = propInfo.GetValue(objecto, null);                 

                    if ((obj is IEditableCollection) && (obj is IQueryable))
                    {
                        foreach (Object item in obj as IQueryable)
                        {
                            var transaction = TransactionChecker(item);
                            if (!transaction.TransactionOK)
                            {
                                return new StatusTransaction(false, transaction.TransactionMessage, item, item.GetType() );
                            }
                        }
                    }
                    else
                    {
                        if (obj is IEditableBusinessObject)
                        {
                            var transaction = TransactionChecker(obj);
                            if (!transaction.TransactionOK)
                            {
                                return new StatusTransaction(false, transaction.TransactionMessage, obj, obj.GetType());
                            }
                        }
                    }
                }
            }

            return new StatusTransaction(true, "OK", objecto, objectType);
        } 

 

 

Copyright (c) Marimer LLC