473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Declaring and using COM interfaces in C#

I have two C# projects:
- In project1 TestClass implements a COM interface ITest and builds a
class library (Register for COM interop = true)
- In project2 I create TestClass and want to typecast it to ITest to
call Init. The cast leads to an InvalidCastException.

Does anyone know what I have to do to get this to work?
I also don't understand why the explicit implementation of Init in
TestClass, "public void ITest.Init(){}" leads to the compiler error
"The modifier public is not valid for this item"

Project1:
--------
file ITest.cs:
....
[ComImport]
[Guid("89BCB740-6119-101A-BCB7-00DD010655AF")]
public interface ITest
{ void Init(); }

file Main.cs:
....
[Guid("520D5B3E-96A7-4b9a-B165-439B4E1FCC92")]
[ClassInterface(ClassInterfaceType.None)]
public class TestClass : ITest
{ public void Init(){} }

Project2:
---------
file ITest.cs: see Project 1

file Main.cs
....
string classKey = "{520D5B3E-96A7-4b9a-B165-439B4E1FCC92}";
Type objType = Type.GetTypeFromCLSID(new Guid(classKey));
ITest itest = (ITest) Activator.CreateInstance(objType);
->> System.InvalidCastException

I have tried several attributes for ClassInterfaceType and also for
the ComInterfaceType of ITest but with always the same result.

Any help would be appreciated.
Thank you
Peter
Nov 16 '05 #1
1 3021
Peter,
I have two C# projects:
- In project1 TestClass implements a COM interface ITest and builds a
class library (Register for COM interop = true)
- In project2 I create TestClass and want to typecast it to ITest to
call Init. The cast leads to an InvalidCastException.
For communication between .NET objects, type identity is scoped by the
assembly that defines the type. So ITest in your first assembly is
different from ITest in the second, even if they happen to be defined
identically and exposed to COM with a single IID.

Why are you using COM here at all?

I also don't understand why the explicit implementation of Init in
TestClass, "public void ITest.Init(){}" leads to the compiler error
"The modifier public is not valid for this item"


Because explicitly implemented interface members are always private,
and you're not allowed to apply any access modifier.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter Berry | last post by:
I was surprised and somewhat disappointed by the fact that you cannot declare types in an interface. I discovered this when defining an interface that included events, where the definition of the...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
3
by: Peter Zwosta | last post by:
>I have two C# projects: >- In project1 TestClass implements a COM interface ITest and builds a >class library (Register for COM interop = true) >- In project2 I create TestClass and want to...
1
by: Joe Jax | last post by:
I have an object which has a base object hierarchy as well as a number of implemented interfaces. Given any one property on that object, how do I find out which base class or interface defines that...
2
by: NG | last post by:
Hi All, I want to declare an interface in C#. I have the equivalent code for VB.NET but when I try to write the corresponding code in C#, it does not work. Can anyone please help me by...
1
by: Ruffin Bailey | last post by:
I want to declare an interface that forces the presence of a certain constructor (in this case, "Sub New(ByVal ds As DataSet)"). When I try, I get a 'Sub New' cannot be declared in an...
15
by: Chris Saunders | last post by:
I have made a declaration like this: private const Complex I = new Complex(0.0, 1.0); When I try to build this I get the error: The expression being assigned to...
7
by: desktop | last post by:
I am not sure I quite understand the use of iterators. I have this int array: int a = {1,2,3,4,5} I would now like to make an iterator to the first and last element: std::iterator<intiter;...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.