1

I'm having a problem converting an interface that has already been implemented to a different class that I want to perform some additional methods.

public interface ISomeInterface { int Widgets { get; set;} } public class SomeClassA : ISomeInterface { int Widgets { get; set;} } public class SomeClassB : ISomeInterface { int Widgets { get; set;} public void DoExtraStuff() {...} } ISomeInterface someClassA = new SomeClassA(); SomeClassB someClassB = (ISomeInterface)someClassA; //InvalidCastException someClassB.DoExtraStuff(); //What i'm trying to execute 

Hope this makes sense...

1
  • Why are you trying to call a method on an instance that doesn't have that method? An instance of something cannot become something else: SomeClassA is not SomeClassB, even if both implement the same interface. Commented Nov 3, 2010 at 21:16

6 Answers 6

3

No, that's not going to work.

Suppose SomeClassB has some extra fields - what would you expect their values to be when you tried to convert from the reference which actually refers to an instance of SomeClassA to SomeClassB?

An object never changes its type in .NET, and you can't cast a reference to an incompatible type.

If you want an instance of SomeClassB, you'll have to create one - perhaps you could write a constructor for SomeClassB which takes an ISomeInterface to delegate to?

Sign up to request clarification or add additional context in comments.

5 Comments

Beat by the Skeet again! :-)
sigh... he is to fast. do you ever sleep Jon?
This is not Skeet, it's the android replica he wrote of himself.
I suppose this is the best way. I wanted to be lazy and avoid re-initializing all the properties. Was hoping the casting would copy them automatically.
@Jon: I look forward to reading it, I'm sad I can't correct the typo and change blig to bling.
1

This won't work, you can only cast up the tree.

You could say

ISomeInterface someClass = (ISomeInterface)someClassA; 

But you can't get to someClassB from there.

You need to have someClassB a child of someInterface and someClassA a child of B

Probably what you really want is for them to both implement the same interface (thus change your interface to include public void DoExtraStuff() {...}

Comments

1

You can only cast to a class or interface the class inherits or implements.

So SomeClassA can be cast to ISomeInterface since it derives from it, but not to SomeClassB because although they share an interface they are different classes (with potentially very different data).

Comments

0

You can't do that. You can upcast your someClassA to the base interface of ISomeInterface, but it remains a SomeClassA; you can't cast an instance of SomeClassA (even if you're referring to it as an ISomeInterface) to a SomeClassB; they're siblings.

Comments

0

If you want to run that method on ClassA's data, you should think about your architecture. If ClassA needs to run a method that ClassB provides, then they should both impliment the same method either through an interface or a common base class.

Comments

0

My understanding is that an interface ensures that your class contains certain methods and data. However, you can extend that interface and your class could include other types of data and methods. You can't expect C# to pull the relevant information from your class, which possibly contains extra data, and then cast it to your new object, which might also contain extra data.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.