Why is marking an assembly ComVisible(true) discouraged?

后端 未结 5 2058
日久生厌
日久生厌 2021-01-07 18:53

I have always marked my .NET assemblies as visible to COM with [assembly: ComVisible(true)], thinking that I never know when someone might need to call them fro

5条回答
  •  爱一瞬间的悲伤
    2021-01-07 19:23

    It is easy and given on MSDN. This is the way how to fix this warning:

    using System;
    using System.Runtime.InteropServices;
    
    [assembly: ComVisible(false)]
    namespace InteroperabilityLibrary
    {
       [ComVisible(false)]
       public class BaseClass
       {
          public void SomeMethod(int valueOne) {}
       }
    
       // This class violates the rule.
       [ComVisible(true)] 
       public class DerivedClass : BaseClass
       {
          public void AnotherMethod(int valueOne, int valueTwo) {}
       }
    }
    

    If Base class is in any dll which you are referring in your code. Then, Make [COMVisibible(True)] for derived class. It works in my scenario.

提交回复
热议问题