ComVisible settings

D

David Thielen

Hi;

I just want to verify that this is the right approach for making the COM
interface an a .NET add-in visible.

In AssemblyInfo.cs set:
[assembly:ComVisible(false)]

In my connect class set:
[ComVisible(true)]
for the class itself and the 5 IDTExtensibility2 methods.

Is this the best approach?
 
F

Fredrik Wahlgren

David Thielen said:
Hi;

I just want to verify that this is the right approach for making the COM
interface an a .NET add-in visible.

In AssemblyInfo.cs set:
[assembly:ComVisible(false)]

In my connect class set:
[ComVisible(true)]
for the class itself and the 5 IDTExtensibility2 methods.

Is this the best approach?

Shouldn't the add-in wizard do this? Actually, I had no idea about this
setting so I checked my own project. I found one entry only in
AssemblyInfo.cs and it was set to true.
/ Fredrik
 
D

David Thielen

If you set the assembly to true then it registers a ton of classes in your
add-in. You don't want this as it's just random garbage in your registry and
you are not designing the other classes to be COM callable.

- dave


Fredrik Wahlgren said:
David Thielen said:
Hi;

I just want to verify that this is the right approach for making the COM
interface an a .NET add-in visible.

In AssemblyInfo.cs set:
[assembly:ComVisible(false)]

In my connect class set:
[ComVisible(true)]
for the class itself and the 5 IDTExtensibility2 methods.

Is this the best approach?

Shouldn't the add-in wizard do this? Actually, I had no idea about this
setting so I checked my own project. I found one entry only in
AssemblyInfo.cs and it was set to true.
/ Fredrik
 
P

Peter Huang [MSFT]

Hi

I think the default setting of an Addin generated by the Addin Wizard is
just OK.

You may try to look into the ComVisible attribute, it is set to true by
default and what is your concern about the default setting by the Wizard?

You can apply this attribute to assemblies, interfaces, classes,
structures, delegates, enumerations, fields, methods, or properties.

The default is true, which indicates that the managed type is visible to
COM. This attribute is not needed to make public managed assemblies and
types visible; they are visible to COM by default. Only public types can be
made visible. The attribute cannot be used to make an otherwise internal or
protected type visible to COM or to make members of a nonvisible type
visible.

Setting the attribute to false on the assembly hides all public types
within the assembly. You can selectively make types within the assembly
visible by setting the individual types to true. Setting the attribute to
false on a specific type hides that type and its members. However, you
cannot make members of a type visible if the type is invisible. Setting the
attribute to false on a type prevents that type from being exported to a
type library; classes are not registered; interfaces are never responsive
to unmanaged QueryInterface calls.

Unless you explicitly set a class and its members to false, inherited
classes can expose to COM base class members that are invisible in the
original class. For example, if you set ClassA to false and do not apply
the attribute to its members, the class and its members are invisible to
COM. However, if you derive ClassB from ClassA and export ClassB to COM,
ClassA members become visible base class members of ClassB.




Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

Hello;

I agree with everything you say.

However...

I think it makes more sense to not make all classes visible and instead just
make the Connect class visible. This is not the setting used by the add-in
wizard but I don't see why you would want to make all classes COM visible.

Am I missing something?

thanks - dave
 
P

Peter Huang [MSFT]

Hi

Because by default there will be just one public class in the namespace
Connect, if we need other help class we can just declare the class as
internal.

internal class TestClass
{
public void Test()
{
System.Diagnostics.Debug.WriteLine("hello");
}
}

Also you do need to declare the help class with public and did not want it
is exposed you can use the set the ComVisible = false.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

Hi;

I may be missing something here. But I found (I think - maybe it was from
something I did earlier) a bunch of my public classes from other namespaces
in the registry. These were my general library classes I use in numerous
programs.

??? - thanks - dave

ps - again I am not sure when all of these were put in my registry, it may
have been from earlier when I was learning how to create and register an
Add-in.
 
P

Peter Huang [MSFT]

Hi

I did not understand your meaning very well.
I think the ComVisible attibute in the assembly level will be in effect
through all the assembly level, if we applied the this attribute to
assemblies.
For detailed information you may take a look at the ComVisible in MSDN
ComVisibleAttribute Class
You can apply this attribute to assemblies, interfaces, classes,
structures, delegates, enumerations, fields, methods, or properties.

I think the Wizard will just generate the code for general usage, if you
need extend it, you need to do more cusmized(e.g. set comvisible =false in
assembly) to make your addin more flexible.

Hope this help.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top