开发者

What is the base of all interfaces in .net, just like the base for all classes is the object

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 mo开发者_如何学Cdel, 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?


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.


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?


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)
    {


    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜