Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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) { ... }
    }
}

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,
Posted

1 solution

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

C#
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:

C#
B.Register(typeof(B))


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

C#
A.Register(typeof(B))


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

Hope this help to someone.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900