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.
}
10
  • Does IronPython support Interfaces..? sounds like you need to seek an IronPython expert Commented Jan 22, 2013 at 9: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. Commented Jan 22, 2013 at 9:21
  • How to use a specific interface on a C# object in IronPython look at this and perhaps you can use the example to work for your case Commented Jan 22, 2013 at 9:28
  • 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. Commented Jan 22, 2013 at 9: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 Commented Jan 22, 2013 at 10:05

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

6 Comments

-1: error CS0144: Cannot create an instance of the abstract class or interface 'IFoo'
+1 now ;p Added missing bits, and now compiles. Interesting ;p
The C# compiler replaces new IFoo() with new Foo()
Thanks for your replies. I edited the interface definition parts to my message. The "Agilent34980A()" is defined as an interface.
@shihuan83: The solution would be then to instantiate Agilent34980AClass or use a method in the Marshall class.
|
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.

4 Comments

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();
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).
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)
Thanks for your replies. I edited the interface definition parts to my message. The "Agilent34980A()" is defined as an interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.