mercredi 5 décembre 2018

Creating an instance of a type using reflection

I am trying to use reflection to create an instance of one of my classes. The interface for the class (IInstructions) has one variable shown below.

    string[] Operands { get; set; }

I am trying to create an instance of this class using reflection that has the operand variables set. The code I have got so far is shown below.

        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
        Console.WriteLine(operands[0]);
        foreach (Assembly a in assemblies)
        {
            Type[] typeArray = a.GetTypes();
            foreach (Type type in typeArray)
            {
                if (opcode.Equals(type.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    Object dynamicObj = Activator.CreateInstance(type);
                    instruction = (IInstructionWithOperand)dynamicObj;

                }
            }
        }

This, so far searches through the loaded assemblies and retrieves the correct assembly and type for that assembly. However, I'm unsure how to set the variable for this type and correctly create an instance of it?





Aucun commentaire:

Enregistrer un commentaire