Help reflection. Create a collection from a class based on its properties?

I need a little help. I'm new to thinking. We use a third-party api, and it returns a class called "AddressList". It has public properties, which are literally called Address1, Address1Name, Address1Desc, Address2, Address2Name, Address2Desc, Address3, Address3Name, Address3Desc, ... Address99, Address99Name, Address99Desc .. There are also several other properties. I have a class called "SimpleAddress" that has only 3 properties (address, name, description). What I want to do is when I return the class of the class "AddressList", I would like the AddressDesc1 loop ... through AddressDesc99 ... and whichever is neither empty nor empty, I would like to instantiate " SimpleAddress ", fill in its properties and add it to the list ...Can someone point me in the right direction? Obviously, it would be better if "AddressList" was a kind of collection, but unfortunately this is not so. It is generated from the mainframe return line.

Thanks for any help, ~ ck in San Diego

+3
source share
6 answers

Hk. You can 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);
}
+6
source

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

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

Each PropertyInfo has a Name attribute (string) that you can use for matching. Flip all the properties and write the code that creates the new SimpleAddress instance.

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

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

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

+3
source

- :

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

( ), - :

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

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();
    }
}
+1
source
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
        };
0
source

Source: https://habr.com/ru/post/1719435/


All Articles