简体   繁体   English

使用反射创建类型的实例

[英]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. 类的接口(IInstructions)具有如下所示的一个变量。

    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? 但是,我不确定如何为此类型设置变量并正确创建它的实例?

If the type is always IInstructionWithOperand then you could declare 如果类型始终为IInstructionWithOperand则可以声明

IInstructionWithOperand instruction;

and then assign it exactly as you have: 然后完全按照您的要求进行分配:

instruction = (IInstructionWithOperand)dynamicObj;

Or, suppose all of the code above is within a function. 或者,假设上面的所有代码都在一个函数内。 You could make IInstructionWithOperand the return type of the function, like 您可以使IInstructionWithOperand函数的返回类型,例如

IInstructionWithOperand GetInstruction(string opcode)
{
    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);
                return (IInstructionWithOperand)dynamicObj;
            }
        }
    }
} 

You don't have to declare a variable within the function. 您不必在函数内声明变量。 When you call the function, it will look like this: 调用该函数时,它将如下所示:

var instruction = GetInstruction(someOpCode);

and the declared type of instruction will be IInstructionWithOperand . 声明的instruction类型为IInstructionWithOperand

That's probably a good idea anyway because it will keep parts of your code separate. 无论如何,这可能是一个好主意,因为它将使您的部分代码分开。 One function creates an instance of a class that implements IInstructionWithOperand , and another function (or class) uses it to do something. 一个函数创建实现IInstructionWithOperand的类的实例,而另一个函数(或类)使用它来执行某些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM