简体   繁体   English

创建一个方法的DeclaringType实例:Activator.CreateInstance

[英]Create an instance of the DeclaringType of a method: Activator.CreateInstance

I once again am struggling with reflection.我又一次在反思中挣扎。 I just give you the piece of code that my debugger does not bite:我只是给你我的调试器不咬的一段代码:

public bool HandleCommand(string command)
        {
            
            MethodInfo m = methods.FirstOrDefault(t => t.GetCustomAttribute<CommandAttribute>().Name == command);
            ICommandSet set =(m.DeclaringType)Activator.CreateInstance(m.DeclaringType);
            m?.Invoke(set, null);
            return true;
        }

Basically, this code is inside a class called CommandHandler.基本上,此代码位于名为 CommandHandler 的 class 中。 When it's constructed, it loops over all the types in the executing assembly that implement a certain interface, and store all their methods that have a CustomAttribute attached to it in List;当它被构造时,它会循环执行程序集中实现某个接口的所有类型,并将它们所有附加了 CustomAttribute 的方法存储在 List 中; For this question's purpose I just assume that everything is working there.对于这个问题的目的,我只是假设一切都在那里工作。 The attribute has just one property:该属性只有一个属性:

[AttributeUsage(AttributeTargets.Method)]
    public class CommandAttribute : Attribute
    {
        public string Name { get; set; }

        public CommandAttribute(string name)
        {
            Name = name;
        }
    }

Now in the method you saw above, the HandleCommand() method, I store the method which's name property is equal to the string I passed in in a MethodInfo m.现在,在您在上面看到的方法中,HandleCommand() 方法中,我存储的方法的 name 属性等于我在 MethodInfo m 中传入的字符串。 Now my question essentially is, how I properly Invoke this method.现在我的问题本质上是,我如何正确调用此方法。 m.Invoke needs an object that calls it, and because passing in "this" does not work, and examples online always passed in an instance of the class it was defined in, I figured I needed to create an instance of the class m was defined in and just pass this into the Invoke() method. m.Invoke 需要一个调用它的 object,并且因为传入“this”不起作用,并且在线示例总是传入它定义的 class 的实例,我想我需要创建一个 ZA2F2ED4F8EBC2CBB4C21A2 的实例是在中定义并将其传递给 Invoke() 方法。 In practice, this is a lot harder than I thought, and my best guess is what you can see above, with the activator.在实践中,这比我想象的要困难得多,我最好的猜测是你可以在上面看到的,使用激活器。

ICommandSet set =(m.DeclaringType)Activator.CreateInstance(m.DeclaringType);

First of all, I know for sure that the class that m is declared in implements ICommandSet, because this is a creterium for a type to be inspected for the methods.首先,我确定 m 中声明的 class 实现了 ICommandSet,因为这是要检查方法的类型的 creterium。 So this is why I say "ICommandSet set".所以这就是我说“ICommandSet set”的原因。 Then the Activator shall create this instance.然后激活器将创建这个实例。 But it does not work.但它不起作用。 The only error message provided states that m is a variable but is used like a type.提供的唯一错误消息指出 m 是一个变量,但用作类型。 However, when I pass it in as a param to Activator.CreateInstance() the compiler seems to dig it just fine.但是,当我将它作为参数传递给 Activator.CreateInstance() 时,编译器似乎可以很好地挖掘它。 I absolutely don't know how I might fix this problem, as I don't really understand what is the problem.我绝对不知道如何解决这个问题,因为我真的不明白问题出在哪里。 Is there anyone out there who can help me?有没有人可以帮助我?

Also, all the methods are defined in different classes and even projects, so I don't know which class the methods are defined in.另外,所有方法都定义在不同的类甚至项目中,所以我不知道这些方法定义在哪个 class 中。

The reason you're receiving this is your syntax is wrong.您收到此消息的原因是您的语法错误。 You cannot perform a cast with a variable dereference as the desire type.您不能使用变量取消引用作为期望类型来执行强制转换。 As you already know you wish to treat "set" as an "ICommandSet" cast to that instead.正如您已经知道的那样,您希望将“set”视为“ICommandSet”强制转换。

var set = (ICommandSet) Activator.CreateInstance(m.DeclaringType);

You can also do it safe你也可以安全地做

var set = Activator.CreateInstance(m.DeclaringType) as ICommandSet;

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

相关问题 创建表单变形实例Activator.CreateInstance的实例 - Create instance of form trought Activator.CreateInstance Activator.CreateInstance我的实例吗? - Activator.CreateInstance my instance? 如何将 IHttpContextAccessor 作为参数传递给 Activator.createinstance 方法中的创建实例? - How to Pass IHttpContextAccessor as a parameter for crating instance in Activator.createinstance method? 使用Activator.CreateInstance创建类的实例并将Interface注入构造函数 - Using Activator.CreateInstance to create instance of a class and inject Interface to constructor 无法使用Activator.CreateInstance创建COM类的实例 - Unable to Create the instance of COM class using Activator.CreateInstance Activator.CreateInstance | 使用字符串创建Page.xaml实例 - Activator.CreateInstance | Create Page.xaml instance with a string Activator.CreateInstance()特定的类类型实例 - Activator.CreateInstance() specific class type instance 如何测试使用Activator.CreateInstance(…)的方法? - How to test a method that uses Activator.CreateInstance(…)? 用于创建Ironpython类的Activator.CreateInstance - Activator.CreateInstance for create Ironpython class 是否可以使用Activator.CreateInstance()模拟方法? - Is it possible to mock a method with using of Activator.CreateInstance()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM