lundi 12 octobre 2020

Using a property of T to create a new object of the property type with the property values?

I need a way to convert the properties of an object of type T to objects of the property type, and values as they are in T. My reason for doing this is so that I can check if the property is or inherits from IEnumerable (Lists, arrays, etc.), and if it does then I need to pass that IEnumerable as an object to be worked on. So far I have

foreach (var propInfo in obj.GetType().GetProperties())
        {
            var newObject = Activator.CreateInstance(propInfo.PropertyType, propInfo.GetValue(obj));
            if (ObjectProcessing.ImplementsIEnumerable(newObject))
            {
                ObjectProcessing.ObjectQueue.Enqueue(newObject);
            }
        }

Which unfortunately doesn't work. I cant use CreatInstance<T> because it seems the compiler assumes T is the T in the method signature, which is the source object not the target object.





Aucun commentaire:

Enregistrer un commentaire