简体   繁体   中英

C# Adding items of unknown types to an array/list/etc of a type inferred from an object type

I am having a tough time figuring out a way to add objects into an array without knowing the type of either object...so basically, we are populating objects using data we import from a text file. The objects we are populating will vary, we identify the objects using reflection to find the object with the same class type as the string provided in the txt file. The string representation of the class is the ONLY knowledge of the class we have during the entire process. The issue occurs when we have objects that need to be created and then put into an array. I'm able to identify the array it belongs to fine, but storing the object in the collection is another story.

This is as far as I've gotten:

UPDATE:

public bool Populate(Dictionary<string, string> nameValuePairs, object obj) 
            {
                return obj.GetType() == typeof (Array) ? PopulateList(nameValuePairs, obj)  : PopulateObject(nameValuePairs, obj);
            }

private bool PopulateObject<T>(Dictionary<string, string> nameValuePairs, T obj) where T : new()
    {
        foreach (var kvp in nameValuePairs)
        {
            var itemName = kvp.Key;
            var value = kvp.Key;

            try
            {
                var property = obj.GetType().GetProperty(itemName, BindingFlags.Public | BindingFlags.Instance);
                property?.SetValue(obj, value);

            }
            catch (Exception e)
            {
                try
                {
                    var field = obj.GetType().GetField(itemName, BindingFlags.Public | BindingFlags.Instance);
                    field?.SetValue(obj, value);
                }
                catch (Exception e1)
                {

                    return false;
                }
            }
        }

        return true;
    }

    private bool PopulateList<T>(Dictionary<string, string> itemProperties, List<T> list)
             where T : new()
    {
        var baseObj = new T();
        var lookup = new NameLookupHandler();

        Populate(itemProperties, baseObj);
        list.Add(baseObj);
        return true;
    }

private bool PopulateList(Dictionary<string, string> itemProperties, ref object list)
{
        Type baseType = list.GetType().GetElementType();
        var baseObj = Activator.CreateInstance(baseType);
        //Code here populates the object that will be added to the collection.


        return true;
}

Updated Method:

private bool PopulateList<T>(Dictionary<string, string> itemProperties, List<T> list)
                 where T : new()
        {
            var baseObj = new T();
            var lookup = new NameLookupHandler();

            Populate(itemProperties, baseObj);
            list.Add(baseObj);
            return true;
        }

Why not make it generic?

private bool PopulateList<T>(Dictionary<string, string> itemProperties, List<T> list)
                 where T : new();
{

        var baseObj = new T();
        //Code here populates the object that will be added to the collection.

        list.Add(baseObj); <--- This obviously doesn't work.
        return true;
}

Note that the list parameter does not need to be ref unless you're changing what list references. It does not need to be ref to add items to the list.

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