dimanche 22 novembre 2015

Is it possible to pass a params string[ ] in by reflection? C#

I'm currently tasked with making a Stack based Virtual Machine and it is the first time I have used reflection to pass a text file with instruction e.g. loadstring "Calculating (6 + 4) - 3" which is stored in the string[] operands. I currently have the solution passing the loadstring part but not the actual text "Calculating etc.." or loadint 6.

Is it possible to pass this with the Activator or does a new foreach need creating to add the additional information to the instruction?

internal static IInstruction CompileInstruction(string opcode, params string[] operands)
            {
                IInstructionWithOperand instruction = null;

                Assembly assembly = Assembly.GetExecutingAssembly();
                Type[] Typeinassembly = assembly.GetTypes();
                try {
                    foreach (Type type in Typeinassembly)
                    {
                        if (type.Name.ToLower().Equals(opcode.ToLower()))
                        {
                            if (typeof(IInstructionWithOperand).Equals(type.GetInterface("IInstructionWithOperand", true)))
                            {
                                instruction = (IInstructionWithOperand)Activator.CreateInstance(type);
                            }
                        }
                    } 
                }
                catch (Exception e)
                {
                    throw new SvmCompilationException("Invalid SML instruction has been found : " + e);
                }
                return instruction;
            }

Thank you.





Aucun commentaire:

Enregistrer un commentaire