C# and VS.net components - very basic question...

This is a no-brainer, but...OK...I've developed components in COM/COM+ for a long while...I am new to developing components in C# and VS.NET...the question is this: in COM/COM+, I'm used to having to go through and INTERFACE(such as IUnKnown, etc...)...IN C# you just ADD A COMPONENT within the IDE...the question is simple: do these C# components NEED to implement any type of interface, or do they communicate without a defined interface...and 2) can I add a C# INTERFACE or ABSTRACT CLASS to the C# component to emulate the type of interface I am used to in COM/COM+.... ......Thanks......

Answer this question

C# and VS.net components - very basic question...

  • Tubarão

    Thanks, Michael...since this was posted, I discovered 500+ pages of developer info on VS.Net 2003 developing components. I was able to read briefly(skim) through a lot of the material. My initial impression is that it is so much easier in .Net....you just add a "class component"...wow...then you can, optionally, add interfaces or abstract classes as you wish. The ICOMPONENT is a great improvement over the old COM interface requirements.

    Again...thanks for your direction to VS 2005...I am still working on studying the abundant material available on VS 2003..but I will definitely want to upgrade to VS 2005 in the future....


    Sincerely yours,

    Ken

  • Tobias Kober

    Adding a component adds a new class (derived from System.ComponentModel.Component) to the project. This does not generate a dll or exe. It simply adds some code to your project. When you compile your project, you get a .NET assembly, which can be either a DLL or EXE. (Typically if you're writing re-usable COM components in .NET, you'll select a class library and generate a DLL.) To control visibility of your classes/methods to COM clients, you should mark your assembly as:

    [assembly:ComVisible(false)]

    and then explicitly apply the [ComVisible(true)] attribute to whichever classes and methods you want to expose to COM. (Note that you can only apply ComVisible to public classes, methods, etc.)

    I would strongly recommend running FxCop (http://www.gotdotnet.com/team/fxcop) against your assemblies as it provides some great advice on how best to expose your types to COM (and catches some common mistakes on how not to expose them).

  • justingrant

    Hi,

    Thanks for your quick reply. I would like to ask a few more questions, if you would be kind enough to walk me through these questions. OK...what I meant by a "C# component" is in the COM/COM+ sense...a black-box, either in the form of an .exe or .dll file. The way that I have been introduced to this in the MSDN C# tutorial material is simply to go to the Project tab on the C# IDE, then select "ADD A Component". OK...my question is this: does that generate an .exe or .dll file when you do this...and...what are the ways to control ACCESS to the internal methods and attributes... ....what are the levels of the modifiers to restrict access at different levels... ......if this is an .exe or .dll file....how do you provide access to other projects files... ......

    Thanks,

    Ken

  • Sven Peeters

    COM/COM+ are very centred around interfaces, as you know. You can't do anything in COM/COM+ without interfaces. In C# (and .NET in general), you can do a lot with interfaces, but you don't need to. It's just one of many techniques.

    With that said, I'm a bit confused by your usage of "C# component". Do you mean a design-time component that integrates with VS.NET If so, you typically derive from System.ComponentModel.Component. (You can also implement System.ComponentModel.IComponent yourself as ASP.NET controls do.) You mark up your class with attributes to designate which properties are exposed to the designer, specify default values for those properties, specify a design helper class, etc. .NET is very centred around attributes.

    If you want to expose a .NET component to COM (via the ComVisible attribute) and want to implement specific interfaces, you can do so using the tlbimp.exe tool. It will read a typelib or COM DLL and generate a .NET assembly that defines the appropriate interfaces. You can then reference this assembly in your component code and implement the desired interface.


  • Agile Knaskefant

    Try the following link in the beta documentation for Visual Studio 2005, Components in Visual Studio-- http://msdn2.microsoft.com/library/ms171766(en-us,vs.80).aspx . 

    Michael Blome [MS]
    Visual C# Documentation Manager


  • Payam Shodjai

    OK...thanks for reply...I do have a lot of material (actually several whole 1,000 page books) on COM/.Net interop and exposure...but I will definitely look into FXCOP...last question...

    OK...so...to generate the typical "black-boxes" similar to COM .exes or .dll's...but NOT EXPOSING OR USING COM Interop...just strictly C# black-boxes...in order to generate stand-alone C# coded .exes or .dll's... I would generate a class library then complie it...would that give me a stand-alone black-box in .exe or .dll form.... ......

    Also....additional question...is there some reason why I would no longer need to or want to have black-box stand-alone C# coded files.... .....is there something about C# and VS.net that makes these stand-alone files obsolete or something... .....is there some "higher process" in C# assemblies that have overridden having to generate stand-alone .exes or .dll's... ......and if so....could you please give me a brief overview or where to find code samples showing how to generate reusable code "components" in C#.... ....


    Thanks,

    Ken

  • C# and VS.net components - very basic question...