Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Working with interfaces

Ben Longo edited this pageApr 10, 2022 ·3 revisions

Calling methods of explicitly implemented interfaces

If a class implements an interface explicitly, you will have to "cast" an object to that interface in order to before invoking its methods. Consider the following C# code:

namespaceExample{interfaceIExplicit{intAdd(inta,intb);}publicclassExplicitImpl:IExplicit{intIExplicit.Add(inta,intb){returna+b;}}}

The following example demonstrates how to callAdd:

fromExampleimportIExplicit,ExplicitImplimpl=ExplicitImpl()# Wont work:impl.Add(1,2)# Does work:ifc=IExplicit(impl)ifc.Add(1,2)

Extracting the implementation object from an interface object

Python.NET provides two properties for accessing the object implementing the interface:

  • __raw_implementation__ returns the pure CLR object.
  • __implementation__ returns the object after first passing it through Python.NETs encoding/marshalling logic.

This is a way to "cast" from an interface to the implementation class.

Here is an example:

importSystemclrVal=System.Int32(100)i=System.IComparable(clrVal)100==i.__implementation__# TrueclrVal==i.__raw_implementation__# True
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp