Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在运行时检查类是否实现了某些接口?_Asp.net Mvc_Interface_Abstraction - Fatal编程技术网

Asp.net mvc 如何在运行时检查类是否实现了某些接口?

Asp.net mvc 如何在运行时检查类是否实现了某些接口?,asp.net-mvc,interface,abstraction,Asp.net Mvc,Interface,Abstraction,假设我有一些内容类,比如Page、TabGroup、Tab等。 其中一些将实现我的IWidgetContainer接口——这意味着他们将从接口中获取一个名为ContainedItems的附加字段以及一些用于操作该字段的方法 现在,我需要反映这样一个事实:一些类通过在我的ASP.NET MVC视图中呈现一些特殊的自定义控件(如jQuery添加/删除/移动/重新排序按钮)来实现这个接口 例如,TabGroup将实现IWidgetContainer,因为它将包含选项卡,但选项卡将不会实现它,因为它将无

假设我有一些内容类,比如Page、TabGroup、Tab等。 其中一些将实现我的IWidgetContainer接口——这意味着他们将从接口中获取一个名为ContainedItems的附加字段以及一些用于操作该字段的方法

现在,我需要反映这样一个事实:一些类通过在我的ASP.NET MVC视图中呈现一些特殊的自定义控件(如jQuery添加/删除/移动/重新排序按钮)来实现这个接口

例如,TabGroup将实现IWidgetContainer,因为它将包含选项卡,但选项卡将不会实现它,因为它将无法包含任何内容

因此,当我呈现我的内容对象时(问题是,我在视图中将基类用作强类型而不是具体类),我必须以某种方式在视图中检查它是否实现了IWidgetContainer

这怎么可能,或者我完全错过了什么

为了重新表述这个问题,您通常如何在UI(不一定是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);

}

我可能误解了你的问题,但是使用
is
关键字有问题吗

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

我可能误解了你的问题,但是使用
is
关键字有问题吗

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

我想你在找

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

 }

我想你在找我

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

 }

您可以在运行时检查一个类/接口是否派生自另一个类/接口,如

public static class ObjectTools
{
        public static bool Implements(Type sourceType, Type implementedType)
        {
            if(implementedType.IsAssignableFrom(sourceType))
            {
                return true;
            }
            return false;
        }
}
您只需在sourceType参数(X)中输入要检查的类型,在implementedType参数(Y)中输入要测试的类型,如下所示


您可以在运行时检查一个类/接口是否派生自另一个类/接口,如

public static class ObjectTools
{
        public static bool Implements(Type sourceType, Type implementedType)
        {
            if(implementedType.IsAssignableFrom(sourceType))
            {
                return true;
            }
            return false;
        }
}
您只需在sourceType参数(X)中输入要检查的类型,在implementedType参数(Y)中输入要测试的类型,如下所示

不,“是”没问题。我忘了一个人也可以在接口中使用“is”。)不,“是”没问题。我忘了一个人也可以在接口中使用“is”。)