I have class library project, where I have some methods and dictionaries
public static Dictionary<string, Instruments> globalInstruments;
public static Dictionary<string, WidgetInfo> _MinMaxStorageMinute;
public static Dictionary<string, WidgetInfo> _MinMaxStorageFive;
public static Dictionary<string, WidgetInfo> _MinMaxStorageFifteen;
public static Dictionary<string, WidgetInfo> _MinMaxStorageHalfHour;
public static Dictionary<string, WidgetInfo> _MinMaxStorageHour;
public static Dictionary<string, WidgetInfo> _MinMaxStorageFourHour;
public static Dictionary<string, WidgetInfo> _MinMaxStorageDay;
public static Dictionary<string, WidgetInfo> _MinMaxStorageWeek;
How can I get this dictionaries dynamically? I try to do this
Assembly asm = Assembly.LoadFrom(@"C:\DFListener.dll");
Type type = asm.GetType("DFListener.Listener");
FieldInfo f = type.GetField("globalInstruments");
But how can I get access to dictionary values and keys?
EDIT
When I use same namespace I get this error
Additional information: [A]System.Collections.Generic.Dictionary`2[System.String,DFListener.Instruments] cannot be cast to [B]System.Collections.Generic.Dictionary`2[System.String,DFListener.Instruments]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.`
You're currently just getting the field - you need to get the value of the field using the GetValue
method:
var dictionary = (Dictionary<string, WidgetInfo>) f.GetValue(null);
Here the null
argument is because it's a static field. If it were an instance field, you'd pass a target reference in, to specify the instance to get the value from.
See more on this question at Stackoverflow