通过C#中的反射获取构造函数的字段

时间:2016-08-28 14:58:16

标签: c# reflection

我有一个像这样的字符串数组:

namespace DynamicCore
{
    public class DynamicCode
    {
        public string ProcessName { get; set; }
        public DynamicCode()
        {
            List<Variable> variableList = new List<Variable>();
            Variable Variable = new Variable();

            Variable.ID = "Variable_26545789";
            Variable.Name = "var1";
            variableList_Process_1.Add(Variable);

            Variable = new Variable();

            Variable.ID = "Variable_vdvd3679";
            Variable.Name = "var2";    

            variableList_Process_1.Add(Variable);
        }
    }
}

我使用CompileAssemblyFromSource这样编译它(GetCode获取字符串数组)并将类存储在内存中:

CompilerResults results = provider.CompileAssemblyFromSource(parameters, GetCode());

我需要获取variableList并在列表框中显示Variable.Name个项目。我测试了GetFieldsGetProperty,但都没有正常工作。

如果有人可以解释这个问题的解决方案,那将非常有用。

1 个答案:

答案 0 :(得分:5)

  

我需要获取variableList并在列表框中显示Variable.Name项。我测试了GetFields和GetProperty但没有一个没有正常工作。

variableListVariable都是本地变量。他们生活在DynamicCode()构造函数的范围之外。

将它们声明为DynamicCode类中的字段或属性。

相关问题