2 Replies - 520 Views - Last Post: 29 September 2012 - 08:04 AM Rate Topic: -----

#1 Ryxian  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 13-September 09

Custom implicit cast from interface to class

Posted 29 September 2012 - 04:41 AM

Hey! I have a little problem here. I don't think this actually works, but maybe there is a nice way around it...

Anyway, I have a method, say it looks like this:
public void Initialize(Device device);


Now, I have a class implementing an interface for abstraction, like this:
public interface IGraphicsDevice
{
	Device Device { get; }
}

public class GraphicsDevice : IGraphicsDevice
{
	private Device device;

	...

	public Device Device 
	{
		get { return device; }
	}
}


Since I only work with interfaces and not classes directly, I would call the Initialize method like this:
IGraphicsDevice graphicsDevice;
Initialize(graphicsDevice.Device);


Now, for the problem. I don't really want to expose the Device class from my interface. I would like to have a custom implicit cast so I could call the method just like this:
IGraphicsDevice graphicsDevice;
Initialize(graphicsDevice);


If I didn't use only interfaces, I could do it like this in the GraphicsDevice class...
public static implicit operator Device(GraphicsDevice graphicsDevice)
{
	return graphicsDevice.Device;
}


... and the call to Initialize would simply pass through without a problem. But is it possible to do this some way through interfaces? The compiler tells me interfaces cannot have operators. Does that mean there is no way to create an implicit cast from an interface to another class?

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Custom implicit cast from interface to class

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,646
  • Joined: 05-May 12

Re: Custom implicit cast from interface to class

Posted 29 September 2012 - 06:53 AM

I think you need to review your design and what your goals are.

If the IGraphicsDevice is meant to abstract away the Device, then don't even expose the Device property in the interface. Let everybody who uses the interface only ever play with the an IGraphicsDevice and have to make their calls that need to talk to a device have to be through the interface. ex:
interface IGraphicsDevice
{
    void Reset();
    void Render(byte [] data);
    Capabilities Caps { get; }
}



If on the other hand, you are trying to use the IGraphicsDevice more like a handle and want the ATL/MFC style programming where their CWindow/CWnd classes magically become HWNDs where appropriate, then call a spade a spade and declare a handle class rather than pretending to play with interfaces. That way with a concrete class you can do the implicit cast that you want to happen.
Was This Post Helpful? 1
  • +
  • -

#3 Ryxian  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 13-September 09

Re: Custom implicit cast from interface to class

Posted 29 September 2012 - 08:04 AM

Thanks for your input!

Well, the Device I'm trying to "abstract" away is a DirectX device, which has a lot of methods, properties and other classes that takes it as input, so I don't really want to provide methods in my IGraphicsDevice that delegates the calls to the Device and creates other classes such as Textures etc. The abstraction is not that big of a deal in this project, I just figured it would be cleaner code. So I just want the IGraphicsDevice to act like a Device, through an implicit cast, so I wouldn't need the Device in my interface. Guess I'll have to stick with this as it is, or skip the abstraction interface.

[EDIT]
Actually, now when I think about it, I only need a few methods and classes created with Device, so I could simply provide methods for just them... Like LoadTexture, which delegates to Texture2D.FromFile() etc.

This post has been edited by Ryxian: 29 September 2012 - 08:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1