0

As suggested by @John, here is re-write:

Consider this code:

public object Foo(object original)
{
    List<object> origList = original as List<object>;    //problematic line, origList is null even though it was initialized as [1,2,3] in main method
    List<object> copy = new List<object>();

    foreach (var item in origList)
        copy.Add(item);

    return copy;
}

class Example
{
    List<int> someList = new List<int>() { 1,2,3 };
    // and also other Lists, I dont know their type at compile time
    // and other fields and methods
}

//in usage method:

Example e1 = new Example();
object obj1 = e1;    // this is original
Example e2 = new Example();
object obj2 = e2;   // this is copy

FieldInfo[] fields_of_class = obj1.GetType().GetFields(
            BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

foreach(FieldInfo fi in fields_of_class)
{
    object currentObject = fi.GetValue(obj1);

    // do type checking
    if (IsList(currentObject)
    {
        fi.SetValue(obj2, Foo(currentObject));    // here I have to retrive copy of the list, but I dont know generic argument beforehand as it can be List<insert any type here>
    }
}

How can I get that original list from System.Object in Foo method?

Preferably, I would like not to use generic methods as I would have to MakeGenericMethod which is slow for me. But if that's only solution I take it.

Now I know this isn't the smallest code example. Right now, I am looking into some other solutions of the problem.

sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
  • 2
    A `List` is not a `List` otherwise you'd be able to add non int instances to it. Why not `List origList = original as List`? – vc 74 Oct 20 '19 at 10:01
  • Definitely read [Eric's explanation](https://stackoverflow.com/a/1817341/3181933). – ProgrammingLlama Oct 20 '19 at 10:04
  • I've added another link to the list: [How do I clone a generic list in C#?](https://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c) - the simplest option is to use the LINQ extension method `.ToList()` (e.g. `var newList = someList.ToList();`), which will produce a new list. – ProgrammingLlama Oct 20 '19 at 10:05
  • @sanitizedUser Can you edit your question to clarify what you want? Olivier's answer answers the question "how do I convert any list to `List`?" whereas your question seeks to create a `List` when a `List` is provided. You've accepted the answer but it doesn't seem to match your question. – ProgrammingLlama Oct 20 '19 at 10:26
  • @John I probably should have told that: It's Reflection. I don't know what type field would be when copying, If it's list I use this method. I have to get back an object to assign it to the field with FieldInfo.SetValue() again. – sanitizedUser Oct 20 '19 at 10:27
  • You'll likely need a reflection solution to a reflection problem. I recommend editing your question to show how you retrieve the list you need to clone, and how you set the list on the object once you're finished. Since it's not really a duplicate of the questions I linked, I'll reopen yours once you have done so :-) – ProgrammingLlama Oct 20 '19 at 10:35
  • @John Thank you for your effort. I rewrote it. – sanitizedUser Oct 20 '19 at 12:22

1 Answers1

1

You can try this:

public object Foo(object original)
{
  if ( original is System.Collections.IEnumerable )
  {
    List<object> copy = new List<object>();

    foreach ( var item in original as System.Collections.IEnumerable )
      copy.Add(item);

    return copy;
  }
  else
    return null;
}