Hi all,

I am working with a C# library that has two classes with no inheritance relationship, Class1 and Class2, where Class2 has an op_Implicit that allows you to construct an instance of it from an instance of Class1. In the same library there is a method Class3.TheMethod, which takes an instance of Class2. There is C# code for this below.

I have an instance of Class1, c1, and I want to be able to call it in IronPython with an instance of Class2 that has been created using the op_Implicit; C# code that does this is also below.

So far I've had no luck; I understand that op_Implicit isn't very well supported in IP currently, but any workarounds that avoid having to drop into C# would be much appreciated.

I've tried getting it to work two ways - again, IP code is below:

   * Doing it directly with C3.TheMethod(c1) just says "expected
     Class2, got Class1", as you would expect.
   * Trying the trick that Dino Viehland suggested on 22 June, which
     involves putting the instance into a generic list parameterised to
     hold instances of Class2 just moves the problem to the call to
     list.Add.

Am I missing something? If not, can anyone think of any other workarounds? Or am I just going to have to grit my teeth and drop into C#?

Regards,

Giles
--

Giles Thomas
[EMAIL PROTECTED]
+44 (0) 20 7253 6372

Resolver Systems Ltd
17a Clerkenwell Road, London EC1M 5RD, UK
VAT No.: GB 893 5643 79 Registered in England and Wales as company number 5467329.
Registered address: 843 Finchley Road, London NW11 8NA, UK



C# library code:
=============================================


namespace OpImplicitTest
{
   public class Class1
   {
       public int value;
       public Class1(int value)
       {
           this.value = value;
       }
   }

   public class Class2
   {
       public int otherValue;

       private Class2(int value)
       {
           this.otherValue = value;
       }


       public static implicit operator Class2(Class1 c)
       {
           return new Class2(c.value);
       }
   }

   public class Class3
   {
       public void TheMethod(Class2 c2)
       {
           Console.Out.WriteLine(c2.otherValue);
       }
   }

}


C# library code:
=============================================

           Class1 c1 = new Class1(23);
           Class3 c3 = new Class3();
           c3.TheMethod(c1);




IP code
=============================================


import clr
clr.AddReference('OpImplicitTest')
from OpImplicitTest import Class1, Class2, Class3
c3 = Class3()
c1 = Class1(23)

# Try calling it directly as per the C# - this blows up
c3.TheMethod(c1)


# Try Dino's workaround...
from System.Collections.Generic import List
l = List[Class2]()
# ...and this blows up.
l.Add(c1)




_______________________________________________
users mailing list
users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to