为什么使用ComRegisterFunctionAttribute的方法采用类型参数? [英] Why Does a Method with the ComRegisterFunctionAttribute Take a Type Parameter?

查看:188
本文介绍了为什么使用ComRegisterFunctionAttribute的方法采用类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,为什么注册和取消注册方法需要一个类型参数?

In the following code, why do the methods Register and Unregister require a type parameter?

using System.Runtime.InteropServices;

namespace Foo {
    [Guid("...")]
    [ComVisible(true)]
    public class Bar {
        [ComRegisterFunction]
        private static void Register  (Type type) { ... }

        [ComUnregisterFunction]
        private static void Unregister(Type type) { ... }
    }
}



我想也许是因为每个程序集只需要一个这样的函数来注册该程序集中的所有类型,但这不是案件。无论我尝试什么(从可导出COM的Bar继承的类,可导出COM的Bar的子类,可导出COM的其他无关类),这些方法只能调用一次(type == typeof(Bar) )。我怀疑设计师会要求一个无用的参数,所以我的问题是,为什么这些方法需要类型参数?我错过了类型参数可能与包含方法的类不同的情况吗?谢谢,


I thought that perhaps it was because you only need one such function per assembly to register all of the types in that assembly, but that is not the case. No matter what I try (a class inheriting from Bar that is COM exportable, a subclass of Bar that is COM exportable, other unrelated classes that are COM exportable), the methods are only ever called once with (type == typeof(Bar)). I doubt the designers would require a useless parameter, so my question is, why is the type parameter required on these methods? Am I missing a case where the type parameter could differ from the class containing the method? Thanks,

推荐答案

您缺少类型参数与包含该方法的类不同的情况。



You are missing the case where the type parameter differs from the class containing the method.

using System.Runtime.InteropServices;

namespace Test {

    [ComVisible(true)]
    public abstract class A {
        public static void Register(Type type) {

        }

        public static void Unregister(Type type) {

        }
    }

    [ComVisible(true)]
    public class B : A {

    }
}





注册时,以下方法将被称为:





When registerring, the following method will be called:

B.Register(typeof(B))





由于Register方法是A类的一部分,因此该调用将被转换为





Since the Register method is part of the class A, the call will be translated to

A.Register(typeof(B))





使用typeof(class)而不是type参数时可能会遇到问题。



希望这个帮助某人。



You may get problems when using typeof(class) instead of type argument.

Hope this help to someone.


这篇关于为什么使用ComRegisterFunctionAttribute的方法采用类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆