mercredi 5 décembre 2018

Using reflection to convert a textual instruction into an executable equivalent c#

I am trying to use reflection to examine all of the loaded assemblies and the types within these assemblies. I need to find a type that implements my Instruction class and has the same name as the string that is provided. The string is not case sensitive so when searching it should ignore the case. When the correct c# type is found, the search should stop and create an instance of the c# type.

This is for a virtual machine I am making in c# which reads instructions from a file (e.g. LoadString, Add) where each instruction has its own class (e.g. Add.cs)

How would i go about implementing this in my code? below is the code I have started working on, however it is not doing as intended and i dont know how to fix it.

        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
        Type testType = null;
        Type correctType = null;

        foreach(Assembly a in assemblies)
        {
            Type[] typeArray = a.GetTypes();
            //testType = a.GetType(opcode, false, true);          
            foreach (Type type in typeArray)
            {
                if (type.Name == opcode)
                {
                    Object dynamicObj = Activator.CreateInstance(type);
                }
            }
        }
        #endregion
        instruction = Activator.CreateInstance(correctType);
        return instruction;





Aucun commentaire:

Enregistrer un commentaire