I am analyzing code files that have unkown object and unkown container classes with Reflection.
Example of Object Class to find:
class Object
{
public string Name{ get; set; }
public Travel(string Name)
{
this.Name= Name;
}
}
Example of Container Class to find:
class Container
{
List<Object> list = new List<Object>();
public Container(List<Object> objects)
{
foreach (Object object in objects)
{
list.Add(object);
}
}
The code my program analyzes has to find an Object Class and a Container Class.
I want to access list from Container Class.
Using Reflection at runtime I can get Object Class and Container Class Types.
I have this method to get FieldInfo
public FieldInfo GetFieldByType(Type type, string typeName)
{
foreach (FieldInfo fi in type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static))
{
if (fi.FieldType.ToString().Contains(typeName))
return fi;
}
return null;
}
Using
FieldInfo list = _parser.GetFieldByType(_ContainerClass, "System.Collections.Generic.List");
passing Container Type I get list's FieldInfo.
My question is:
How to get List<(uknown class)>
from FieldInfo or maybe cast it to be List<> so that I could get for example List.Count or List containing objects at runtime?
Or do I need to use PropertyInfo to achieve this?