CA1407: Avoid static members in COM visible types

Item Value
RuleId CA1407
Category Microsoft.Interoperability
Breaking change Non-breaking

Cause

A type that is specifically marked as visible to Component Object Model (COM) contains a public``static method.

Rule description

COM doesn't support static methods.

This rule ignores property and event accessors, operator overloading methods, or methods that are marked by using either the System.Runtime.InteropServices.ComRegisterFunctionAttribute attribute or the System.Runtime.InteropServices.ComUnregisterFunctionAttribute attribute.

By default, the following are visible to COM: assemblies, public types, public instance members in public types, and all members of public value types.

For this rule to occur, an assembly-level ComVisibleAttribute must be set to false and the class- ComVisibleAttribute must be set to true, as the following code shows.

using System;
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
namespace Samples
{
    [ComVisible(true)]
    public class MyClass
    {
        public static void DoSomething()
        {
        }
    }
}

How to fix violations

To fix a violation of this rule, change the design to use an instance method that provides the same functionality as the static method.

When to suppress warnings

It's safe to suppress a warning from this rule if a COM client doesn't require access to the functionality that is provided by the static method.

Example Violation

Description

The following example shows a static method that violates this rule.

Code

using System;
using System.Runtime.InteropServices;
using System.Collections.ObjectModel; 

[assembly: ComVisible(false)] 

namespace Samples
{    
    [ComVisible(true)]    
    public class Book    
    {        
        private Collection<string> _Pages = new Collection<string>();         
        
        public Book()        
        {        
        }         
        
        public Collection<string> Pages        
        {            
            get { return _Pages; }        
        }                
        
        // Violates this rule        
        public static Book FromPages(string[] pages)        
        {            
            if (pages == null)                
                throw new ArgumentNullException("pages");             
                
            Book book = new Book();             
            
            foreach (string page in pages)            
            {                
                book.Pages.Add(page);            
            }             return book;        
        }    
    }
}

Comments

In this example, the Book.FromPages method can't be called from COM.

Example Fix

Description

To fix the violation in the previous example, you could change the method to an instance method, but that does not make sense in this instance. A better solution is to explicitly apply ComVisible(false) to the method to make it clear to other developers that the method can't be seen from COM.

The following example applies ComVisibleAttribute to the method.

Code

using System;
using System.Runtime.InteropServices;
using System.Collections.ObjectModel;

[assembly: ComVisible(false)]

namespace Samples
{
    [ComVisible(true)]
    public class Book
    {
        private Collection<string> _Pages = new Collection<string>();

        public Book()
        {
        }

        public Collection<string> Pages
        {
            get { return _Pages; }
        }

        [ComVisible(false)]
        public static Book FromPages(string[] pages)
        {
            if (pages == null)
                throw new ArgumentNullException("pages");

            Book book = new Book();

            foreach (string page in pages)
            {
                book.Pages.Add(page);
            }

            return book;
        }
    }
}

CA1017: Mark assemblies with ComVisibleAttribute

CA1406: Avoid Int64 arguments for Visual Basic 6 clients

CA1413: Avoid non-public fields in COM visible value types

See also

Interoperating with Unmanaged Code