Ali Soltani Ali Soltani - 3 months ago 8
C# Question

get field of constructor by reflection in C#

I have a string array like this:

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);
}
}
}


I compiled it using
CompileAssemblyFromSource
like this (
GetCode
gets string array) and store class in Memory:

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


I need to get
variableList
and show
Variable.Name
items in a listbox. I tested
GetFields
and
GetProperty
but none was not working properly.

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

Thanks.

Answer

I need to get variableList and show Variable.Name items in a listbox. I tested GetFields and GetProperty but none was not working properly.

Both variableList and Variable are local variables. They don't live outside the scope of DynamicCode() constructor.

Declare them as fields or properties in the DynamicCode class.