views:

38

answers:

4

I would like to pass an interface to a method signature which takes Object as its parameter, so I wonder about this question

public Stream GetViewStream(string viewName, object model, ControllerContext context)
  1. instead of object I shall like to pass an interface Imodel, without modifying the signature. Is there a base class for interfaces?

  2. Also in the new mvc2 is there a way to avoid controllercontext altogether?

A: 

No, there is no base class for interfaces. Nor there is base interface for interfaces.

As for your second question (and partly first one) - what are actually you trying to do?

Anton Gogolev
Interfaces can be cast as object, so in that method I can pass (Object)(interface), does that imply the base class for all interfaces is also Object. In the framework library interface must be defined somewhere as a class(guessing...). I would like to get a hook to the controller, but in mvc2 many times controllercontext returns null (as far as I remember during my last check), and another thing is I cannot bind to a particular controller if I wanted to use Icontroller because controllers are decided at run time using any dependency injectors.
I was wondering if interfaces are just compiler constructs or is there a real definition to it in coding in .net and if so, then what is it.
A: 

There is no base class for interfaces, but you can pass any interface variable e.g:

 private IEnumerable<int> myInterfaceVariable = new List<int>();

to your method because by definition anything that is stored in that variable must be an instance of a class that inherits from the interface - therefore it must be an object.

The following compiles fine:

public class InterfaceAsObject
{
    private IEnumerable<int> myInterfaceVariable = new List<int>();

    private void CallDoSomething()
    {
        DoSomething(myInterfaceVariable);
    }

    private void DoSomething(object input)
    {


    }
}
Giles
A: 

Re 1, there is no base interface, but if I understand you correctly, you can achieve what I think you want by just passing your object that implements IModel via the model parameter and cast (and check!) the parameter to IModel. I use 'as' and check for null.

If you don't need total flexibility, a better way of doing this is to define the interface that the model parameter must support. If the specific objects support derived interfaces (e.g. IDerivedModel : IModel) this will work too.

Look up a text-book on polymorphism.

Kev
in the parameter above I can literally pass any interface by casting it as an object because object is the most general scope, I just wondering if somewhere it is defined that all interfaces are also objects? (the above method takes object as parmeter), regarding my second part can i get rid of controllercontext? thanks
Interfaces aren't objects - they define a "policy" that an object supports if it implements that interface. Just because you *can* pass any object into that parameter, it doesn't mean you should :-). You've got a strongly-typed language - use that fact and don't rely on casting everywhere. Your code will be easier to maintain and more robust.Don't know anything about question 2 - sorry.
Kev
+2  A: 

I'd only answer the first question - Why there's no common base interface for all interfaces ?

First of all, there's no common pre-defined base interface for all interfaces, unlike the System.Object case. Explaining this can get very interesting.

Let us assume, you could have a common interface for all interfaces in the system. That means, all interfaces will need to force their implementations to provide implementation-details for that common base interface. In general, interface are used to give specific special behaviors to their concrete implementation classes. Obviously you only want to define an interface when you only know what to do and don't know HOW to do that. So, if you let there be a common base interface for all interface and force the implementations to expect them to provide details of how to do it - why would you want to do it ? What common task each class should do that varies from one another ?

Lets look at the other side of the coin, why we have System.object as base class of any .Net type - It is simple it gives you some methods that have COMMON implementation for any .Net type and for those methods that it might vary from type-to-type they have made it virtual ex: .ToString()

There's possibly no assumption of any system-wide interface method which is virtual/abstract to all its implementations.

One common practice of using Interface is say, defining a particular behavior to any type. Like I'd have an interface IFlyable which will give Fly() to all types that implement IFlyable. This way I can play with any Flyable object regardless of its inheritance hierarchy coming into picture. I can write a method like this..

public void FlyTheObject(IFlyable flyingObject)
{ 
    flyginObject.Fly();
}

It does not demand anything from the object but the implementation of the Fly() method.

EDIT

Additionally, All interfaces will resolve to Object because interfaces cannot be instantiated. The object is always of a concrete class that can be instantiated. This class may or may not implement your interface but as we know, any .Net type is ultimately based to System.Object, so you will be able to take the instance into an object type regardless of the fact if it implements a particular interface or not.

this. __curious_geek
Yes I gathered the reason for interface being different, but how then in the parameter above I can literally pass any interface by casting it as an object because object is the most general scope, I just wondering if somewhere it is defined that all interfaces are also objects? Of course all interfaces eventually resolve to objects???
read the edit..
this. __curious_geek