Avoid static members in ComVisible types

Switch View :
ScriptFree
Visual Studio Team System
Avoid static members in ComVisible types

TypeName

AvoidStaticMembersInComVisibleTypes

CheckId

CA1407

Category

Microsoft.Interoperability

Breaking Change

NonBreaking

Cause

A type that is specifically marked as visible to COM contains a public static method.

Rule Description

COM does not support static methods.

This rule ignores property and event accessors, operator overloading methods or methods marked with 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.

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 Exclude Warnings

It is safe to exclude a warning from this rule if a COM client does not need access to the functionality provided by the static method.

Related Rules

Mark assemblies with ComVisible

Avoid int64 arguments for VB6 clients

Avoid non-public fields in ComVisible value types

See Also

Other Resources

Interoperating with Unmanaged Code

Community Content

David M. Kean
Example

The following example shows a static (Shared in Visual Basic) method that violates this rule.

[C#]
 
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;
        }
    }
}


In the above example, the Book.FromPages method is not callable from COM.

To fix this violation, you can either change the method to an instance method (which does not make sense in this example) or explicity apply ComVisible(false) to the method to make it clear to other developers that method is not visible from COM.

The following example fixes the above violation by apply the ComVisible property to the method.

[C#]
 
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;
        }
    }
}

David M. Kean
Code Sample

For this rule to fire, an assembly-level ComVisible attribute must be set to false, and the class-level ComVisible attribute set to true:

using System;
using System.Runtime.InteropServices;
 
[assembly: ComVisible(false)]
 
namespace Samples
{
[ComVisible(true)]
    public class Foo
    {
public static void Bar()
        {
        }
    }
}