lundi 29 août 2016

Cast FieldInfo to list in C#

I have string array like this:

namespace DynamicCore
    {
        public class DynamicCode
        {
            List<Variable> variableList = new List<Variable>();
            public DynamicCode()
            {
                Variable Variable = new Variable();                    
                Variable.ID = "Variable_26545789";
                Variable.Type = 1;

                variableList.Add(Variable);

                Variable = new Variable();
                Variable.ID = "Variable_vdvd3679";
                Variable.Type = 2;

                variableList.Add(Variable);
            }
        }
    }

I compiled this array and store it to memory. I get variableList by this code:

string name = "DynamicCore.DynamicCode";
Type type = results.CompiledAssembly.GetType(name, true);
object instance = Activator.CreateInstance(type);    
FieldInfo filed = type.GetField("variableList", 
                                 BindingFlags.Instance | 
                                 BindingFlags.NonPublic);

I try to cast filed(variableList) to List<Variable> like this:

List<Variable> Variables = (List<Variable>)filed;

But I got this error:

Cannot convert type 'System.Reflection.FieldInfo' to 'System.Collections.Generic.List<Variable>'    

It would be very helpful if someone could explain solution for this problem.





Aucun commentaire:

Enregistrer un commentaire