English 中文(简体)
如果一个班级安装某些接口,如何在操作时间检查?
原标题:How to check at runtime if a class implements certain interface?

Let s say I have some content classes like Page, TabGroup, Tab, etc. Certain of those will be implementing my IWidgetContainer interface - it means they will geet an additional field named ContainedItems from the interface and some methods for manipulating this field.

现在,我需要反映这样一个事实,即某些阶层通过在我的协会中进行某些特殊习俗控制来实施这一接口。 NET MVC 意见(如j Query Add/Remove/Move/Reorder buttons)。

例如,塔布集团将实施IWidgetContainer,因为它将含有表格,但一个表格不会执行,因为它赢得了控制任何东西的能力。

因此,我认为,当我提出内容反对时,我不得不作一些检查。 (问题在于,我认为,我的基类不是具体班级,而是使用IWidgetContainer。)

怎么可能,或者我完全错失了什么?

为了回答这个问题,你如何反映一般的“统一分类”(不一定是“ASP.NET MVC”)的某些特殊性质(如接口执行)?

我迄今为止的守则如下:

[DataContract]
public class ContentClass
{
    [DataMember]
    public string Slug;

    [DataMember]
    public string Title;

    [DataMember]
    protected ContentType Type;
}

[DataContract]
public class Group : ContentClass, IWidgetContainer
{
    public Group()
    {
        Type = ContentType.TabGroup;
    }

    public ContentList ContainedItems
    {
        get; set;
    }

    public void AddContent(ContentListItem toAdd)
    {
        throw new NotImplementedException();
    }

    public void RemoveContent(ContentListItem toRemove)
    {
        throw new NotImplementedException();
    }
}

[DataContract]
public class GroupElement : ContentClass
{
    public GroupElement()
    {
        Type = ContentType.Tab;
    }
}

接口:

interface IWidgetContainer
{
    [DataMember]
    ContentList ContainedItems { get; set; }

    void AddContent(ContentListItem toAdd);
    void RemoveContent(ContentListItem toRemove);

}
最佳回答

我认为,你正在寻求

 void Foo(ContentClass cc)
 {
    if (cc is IWidgetContainer) 
    {
        IWidgetContainer iw = (IWidgetContainer)cc;
        // use iw
    }

 }
问题回答

我可能误解了你的问题,但使用<条码>s关键词是否存在问题?

<% if (obj is IWidgetContainer) { %>
    <!-- Do something to render container-specific elements -->
<% } %>

如果某一班级/外围产物来自另一个班级/外围部分,你可以在下台检查。

public static class ObjectTools
{
        public static bool Implements(Type sourceType, Type implementedType)
        {
            if(implementedType.IsAssignableFrom(sourceType))
            {
                return true;
            }
            return false;
        }
}

你们需要做的是进入你正在检查的源Type参数(X)的类型,以及你正在测试的类型。

Console.WriteLine(ObjectTools.Implements(typeof(X), typeof(Y)));




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签