0

Edit: I found the relative page which is about C# and COM:How does the C# compiler detect COM types?

As in title, when I was converting a program in C# to IronPython, I couldn't create instance of a class.

The original C# program:IAgilent34980A2 host = new Agilent34980A();

The rewritten IronPython program:host = Agilent34980A()

The C# program works well, while the IronPython program gets an error as:

TypeError: Cannot create instances of Agilent34980A because it is abstract

Actually Agilent34980A() is an interface, so the error is reasonable.

My question is why it works in C#? The instance is also can't be created in C# which is an interface, right?

Addition:

The C# code is from test machine marker.

The IAgilent34980A2 definition part of source code is as following:

using Ivi.Driver.Interop;

using System;

using System.Runtime.InteropServices;

namespace Agilent.Agilent34980A.Interop

{

  //     IAgilent34980A interface.

[TypeLibType(256)]
[Guid("07678A7D-048A-42A6-8884-6CC8C575BD1F")]
[InterfaceType(1)]
public interface IAgilent34980A2 : IIviDriver
{
   IAgilent34980AMeasurement Measurement { get; }
   IAgilent34980AVoltage Voltage { get; }
  //  There are some similar functions following.
}

}

The Agilent34980A definition part

using System.Runtime.InteropServices;

namespace Agilent.Agilent34980A.Interop

{

  //     Agilent34980A driver class.
[Guid("07678A7D-048A-42A6-8884-6CC8C575BD1F")]
[CoClass(typeof(Agilent34980AClass))]
public interface Agilent34980A : IAgilent34980A2
{
}

}

And IIviDriver definition part

using System;

using System.Runtime.InteropServices;

namespace Ivi.Driver.Interop

{

//     IVI Driver root interface
[TypeLibType(256)]
[InterfaceType(1)]
[Guid("47ED5184-A398-11D4-BA58-000064657374")]
public interface IIviDriver
{
   //     Pointer to the IIviDriverOperation interface
   [DispId(1610678272)]
   IIviDriverOperation DriverOperation { get; }
   
   //     Pointer to the IIviDriverIdentity interface
   [DispId(1610678273)]
   IIviDriverIdentity Identity { get; }
   //  There are some similar functions following.
}
Community
  • 1
  • 1
shihuan83
  • 15
  • 6
  • Does IronPython support Interfaces..? sounds like you need to seek an IronPython expert – MethodMan Jan 22 '13 at 09:15
  • Thank you, DJ KRAZE. As you said, I should find an IronPython expert. But before that, I really don't understand why it works well in C#, though Agilent34980() is an interface. – shihuan83 Jan 22 '13 at 09:21
  • [How to use a specific interface on a C# object in IronPython](http://stackoverflow.com/questions/6081127/how-to-use-a-specific-interface-on-a-c-sharp-object-in-ironpython) look at this and perhaps you can use the example to work for your case – MethodMan Jan 22 '13 at 09:28
  • try pasting your C# code into this online Conversion site http://www.developerfusion.com/tools/convert/csharp-to-python/ – MethodMan Jan 22 '13 at 09:31
  • Thank you for your reply,DJ KRAZE. I am sorry I didn't describe my question exactly. I' d just like to know why it works in C# while creating instant for an interface. – shihuan83 Jan 22 '13 at 09:38
  • 2 different languages / frameworks good luck coding – MethodMan Jan 22 '13 at 09:40
  • No, sorry, you are NOT creating an instance of an interface. Agilent34980 is not an interface, IAgilent34980A is. Interesting. You mix things up left and right, it seems, and expect the compiler to sort that out - bad news: does not work like that. – TomTom Jan 22 '13 at 09:46
  • 2
    Could you check your code again? Your question uses `IAgilent34980A`, `Agilent34980` and `Agilent34980A` and I'm not sure if this is part of the problem or just a typo. List the definitions in c# for these three types as well. – Daren Thomas Jan 22 '13 at 09:47
  • 1
    based on the general naming convention IAgilent34980A host = new Agilent34980(); IAgilent34980A --> interface Agilent34980 --> class implementing that interface. This is perfectly legal in C#. You are not creating an object of Interface but an object of the class that implements that interface – NoviceProgrammer Jan 22 '13 at 10:05
  • Thank you,everyone. I edited the interface definition parts to my message. The "Agilent34980A()" is defined as an interface. – shihuan83 Jan 22 '13 at 10:50

2 Answers2

1

Actualy, you could create an interface instance through the co-class with both [CoClass] and [Guid] attributes.

That allow you to:

[Guid("000208D5-0000-0000-C000-000000000046")] // Anything
[CoClass(typeof(Foo))]
[ComImport]
public interface IFoo { void Bar(); }

public class Foo : IFoo { public void Bar() { return; } }

void Main()
{
    IFoo instance = new IFoo();
}
leppie
  • 115,091
  • 17
  • 196
  • 297
AgentFire
  • 8,944
  • 8
  • 43
  • 90
  • -1: error CS0144: Cannot create an instance of the abstract class or interface 'IFoo' – leppie Jan 22 '13 at 10:07
  • +1 now ;p Added missing bits, and now compiles. Interesting ;p – leppie Jan 22 '13 at 10:12
  • The C# compiler replaces `new IFoo()` with `new Foo()` – huseyint Jan 22 '13 at 10:21
  • Thanks for your replies. I edited the interface definition parts to my message. The "Agilent34980A()" is defined as an interface. – shihuan83 Jan 22 '13 at 10:53
  • @shihuan83: The solution would be then to instantiate `Agilent34980AClass` or use a method in the `Marshall` class. – leppie Jan 22 '13 at 18:48
  • @leppie: So actually as the same to the example given above, the CoClass part makes it work, right? I see. Thank you very much. (I started C# recently and there is much more need to learn...) – shihuan83 Jan 22 '13 at 23:40
0

You can't create an instance of an interface in C#. Example code:

void Main()
{
    var test = new ITest();
}

interface ITest {
    void Test();
}

will give a compilation error:

Cannot create an instance of the abstract class or interface "UserQuery.ITest"

The problem is just that your class is NOT correctly declared as an interface in the library.

The code like:

IAgilent34980A host = new Agilent34980();

works, because it means "variable 'host' is a reference to some object which is an instance of class implementing IAgilent34980A interface". C# non-trivial objects are reference types, hence, this is valid.

DarkWanderer
  • 8,739
  • 1
  • 25
  • 56
  • By the looks of it, he/she isn't trying to create an interface instance in C#, but a class instance that implements an interface - cf. `IAgilent34980A host = new Agilent34980();` – O. R. Mapper Jan 22 '13 at 09:45
  • No, this is not creating an instance of an interface. He is instantiating a class (Agilent34980), not the interface. He then assigns this class to a variable (host) that is of this interface, so the class must implement the interface, which it seems to do (no compiler error). – TomTom Jan 22 '13 at 09:47
  • Well, from the question it seems to me like author thinks it is actually the interface what he is instantiating. I'm explaining that instantiating an interface is really impossible, and elaborate what is actually happening (see edited answer) – DarkWanderer Jan 22 '13 at 09:51
  • Thanks for your replies. I edited the interface definition parts to my message. The "Agilent34980A()" is defined as an interface. – shihuan83 Jan 22 '13 at 10:57