views:

98

answers:

6

Hi All, I need a little help. I am fairly new to reflection. We're using a 3rd party api and it returns a class called "AddressList". It has public properties within it literally called Address1, Address1Name, Address1Desc, Address2, Address2Name, Address2Desc, Address3, Address3Name, Address3Desc,... Address99, Address99Name, Address99Desc.. There are also a couple of other properties. I have a class called "SimpleAddress" that has just the 3 properties (Address, Name, Description). What I want to do is when I get the "AddressList" class returned, I would like to loop AddressDesc1... through AddressDesc99... and whichever ones are not null or empty, I would like to create an instance of "SimpleAddress", populate it's properties, and add it to a List... Can someone point me in the right direction? Obviously this would have been better if "AddressList" was some sort of collection, but unfortunately it is not. It is generated from a return string from a mainframe.

Thanks for any help, ~ck in San Diego

+6  A: 

Ick. You could do something like this:

List<SimpleAddress> addresses = new List<SimpleAddress>();

string addressPropertyPattern = "Address{0}";
string namePropertyPattern = "Address{0}Name";
string descPropertyPattern = "Address{0}Desc";

for(int i = 1; i <= MAX_ADDRESS_NUMBER; i++)
{
    System.Reflection.PropertyInfo addressProperty = typeof(AddressList).GetProperty(string.Format(addressPropertyPattern, i));
    System.Reflection.PropertyInfo nameProperty = typeof(AddressList).GetProperty(string.Format(namePropertyPattern, i));
    System.Reflection.PropertyInfo descProperty = typeof(AddressList).GetProperty(string.Format(descPropertyPattern, i));

    SimpleAddress address = new SimpleAddress();

    address.Address = (string)addressProperty.GetValue(yourAddressListObject, null);
    address.Name = (string)nameProperty.GetValue(yourAddressListObject, null);
    address.Description = (string)descProperty.GetValue(yourAddressListObject, null);

    addresses.Add(address);
}
Adam Robinson
All great answers! This worked perfectly. Thanks all! ~ck
Hcabnettek
+1  A: 

Not tested (for obvious reasons), but something like:

List<SimpleAddress> newList = new List<SimpleAddress>();
AddressList list = ...
Type type = list.GetType();
PropertyInfo prop1, prop2, prop3;
int index = 1;
while((prop1 = type.GetProperty("Address" + index)) != null
   && (prop2 = type.GetProperty("Address" + index + "Name")) != null
   && (prop3 = type.GetProperty("Address" + index + "Desc")) != null) {
    string addr = (string) prop1.GetValue(list, null),
           name = (string) prop2.GetValue(list, null),
           desc = (string) prop3.GetValue(list, null);

    if(addr == null || name == null || desc == null) {
       continue; // skip but continue
    }

    SimpleAddress newAddr = new SimpleAddress(addr, name, desc);
    newList.Add(newAddr);
    index++;
}
Marc Gravell
+2  A: 

You should be able to do something like:

List<SimpleAddress> CreateList(AddressList address)
{
    List<SimpleAddress> values = new List<SimpleAddress>();
    Type type = address.GetType();
    for (int i=1;i<=99;++i)
    {
         string address = type.GetProperty("Address" + i.ToString()).GetValue(address,null).ToString();
         string addressDesc = type.GetProperty("Address" + i.ToString() + "Desc").GetValue(address,null).ToString();
         string addressName = type.GetProperty("Address" + i.ToString() + "Name").GetValue(address,null).ToString();


         if (!string.IsNullOrEmpty(addressDesc) || !string.IsNullOrEmpty(addressName) || !string.IsNullOrEmpty(address)  )
              value.Add(new SimpleAddress(address,addressDesc,addressName));
    }

    return values;
 }
Reed Copsey
He was trying to extract the all three fields out.
Adam Robinson
Yeah, fixed it as soon as I finished. Forgot about that the first time. Thanks.
Reed Copsey
+3  A: 

Start by getting the type of the class in question and invoke the GetProperties method.

PropertyInfo[] properties = myMainframeObject.GetType().GetProperties();

Each PropertyInfo has a Name attribute (a string) you can use to match against. Loop over all the properties, and write the code that creates a new instance of SimpleAddress.

Inside this loop, you can access your mainframe object and pull out the property values you need:

// imagine that in this case, 'p' is a PropertyInfo that represents Address2Name
var simpleAddress = new SimpleAddress();
simpleAddress.Name = p.GetValue(myMainframeObject, null);

(the null is never used for normal properties - it is intended for use with indexed properties).

Mark Seemann
A: 
var addrList = new AddressList
{
 Address1Name = "ABC",
 Address1Desc = "DEF",
 Address1 = "GHI",
 Address3Name = "X",
 Address3Desc = "Y",
 Address3 = "Z"
};

var addresses =
  from i in Enumerable.Range(1, 99)
  let desc = typeof(AddressList).GetProperty(string.Format("Address{0}Desc", i)).GetValue(addrList, null) as string
  let name = typeof(AddressList).GetProperty(string.Format("Address{0}Name", i)).GetValue(addrList, null) as string
  let address = typeof(AddressList).GetProperty(string.Format("Address{0}", i)).GetValue(addrList, null) as string
  where !string.IsNullOrEmpty(address)
  select new SimpleAddress
  {
   Name = name,
   Description = desc,
   Address = address
  };
Thomas Levesque
guess I should of looked at your answer closer first... oh well I just moved soem of the code out into an extension method to reuse it better.
Matthew Whited
+1  A: 

if you want to use linq

public static class MyTools
{
    public static TReturn GetValue<TReturn>(this object input, 
                                            string propertyName)
    {
        if (input == null)
            return default(TReturn);
        var pi = input.GetType().GetProperty(propertyName);
        if (pi == null)
            return default(TReturn);
        var val = pi.GetValue(input, null);
        return (TReturn)(val == null ? default(TReturn) : val);
    }

    public static string GetString(this object input, string propertyName)
    {
        return input.GetValue<string>(propertyName);
    }

    public static List<SimpleAddress> GetAddress(this MyObject input)
    {
        return (
            from i in Enumerable.Range(1, 2)
            let address = input.GetString("Address" + i.ToString())
            let name = input.GetString("Address" + i.ToString() + "Name")
            let desc = input.GetString("Address" + i.ToString() + "Desc")
            select new SimpleAddress() { Address = address, 
                                         Name = name, 
                                         Description = desc }
           ).ToList();
    }
}
Matthew Whited
That extension method isn't a half-bad idea. Make it generic and you'll get a +1 from me.
Adam Robinson
@Adam: for the outputs like this?
Matthew Whited
@Matthew: +1. Exactly, though I wouldn't put `GetString` in there at all, since it doesn't do anything but call `GetValue<string>`.
Adam Robinson
I left it there because I didnt feel like fixing the code below it.
Matthew Whited
non-breaking changes :oP
Matthew Whited