I have 4 public classes, with a bunch of properties each: MoveUpdateEntry, FullServiceEntry, SeamlessAcceptanceEntry, and UndocumentedEntry.
I have a dictionary containing excerpts of a given filename, along with a correlation in place to determine the string equivalent of the class's name I need to instantiate:
private static Dictionary<string, string> dEntryCorr = new Dictionary<string, string>()
{
{"MU_MU", "MoveUpdateEntry" },
{"FS_BQ", "FullServiceEntry" },
{"FS_BF", "FullServiceEntry" },
{"SE_NS", "SeamlessAcceptanceEntry" },
{"SE_U_", "UndocumentedEntry" }
};
I've tried using Reflections as stated here, but simply trying to run the following gives me an error:
string fileToProcess = "MU_MU.json";
Type entryType = Type.GetType( dEntryCorr[ fileToProcess.Substring(0,5) ], true );
System.TypeLoadException: 'Could not load type 'MoveUpdateEntry' from assembly 'DataLoaderJSON, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'
My end goal here is to be able to dynamically pick a type to make a List<> of, based on the filename. (e.g. MU_MU.json will create List<MoveUpdateEntry>, FS_BQ.json creates List<FullServiceEntry>, etc.)
Is this possible in C#?
TLDR: I want to create a List<> with a class type that is determined by a string. This is a matter of abstraction.
string typeName = "Namespace." + dEntryCorr[fileToProcess.Substring(0, 5)] + ", Assembly";. See this answer.typeof(MoveUpdateEntry).AssemblyQualifiedNameList<T>and assign it to adynamicorobjectvariable, I'm not sure that will help you meet your goals-- at compile time, your code will have no idea what type the list is so you won't be able to do anything useful with it. You may as well just use aList<object>or aList<dynamic>.