Hcabnettek
Hcabnettek

Reputation: 12928

Reflection help. Make a collection from a class based on its properties?

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

Upvotes: 3

Views: 237

Answers (6)

Matthew Whited
Matthew Whited

Reputation: 22443

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

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292425

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

Upvotes: 0

Mark Seemann
Mark Seemann

Reputation: 233150

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

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564413

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

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062790

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

Upvotes: 1

Adam Robinson
Adam Robinson

Reputation: 185643

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

Upvotes: 6

Related Questions