【问题标题】:How to determine if a type implements an interface in Powershell如何确定类型是否在 Powershell 中实现接口
【发布时间】:2013-05-18 09:39:41
【问题描述】:

我在一个项目中有一个成员类。我通过在 Visual Studio 的包管理器控制台上使用 powershell 找到了这个类。

public class Member : ICacheable
{
    public string FirstName;
    public string LastName;
    ...
}

它打印如下内容。如何检查此类是否可分配给 ICacheable。实际上,我正在尝试查找所有实现 ICacheable 的类,但我找不到任何可以帮助我找到它的属性。

IsDirty              : False
FileCount            : 1
Name                 : Member.cs
Collection           : System.__ComObject
Properties           : System.__ComObject
DTE                  : System.__ComObject
Kind                 : {6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}
ProjectItems         : System.__ComObject
Object               : System.__ComObject
ExtenderNames        : {}
ExtenderCATID        : {610D4615-D0D5-11D2-8599-006097C68E81}
Saved                : True
ConfigurationManager : 
FileCodeModel        : System.__ComObject
Document             : System.__ComObject
SubProject           : 
ContainingProject    : System.__ComObject

更新(解决方案)

注意:$memberItem 是我在上面向您展示的 ProjectItem。

$memberItem.FileCodeModel.CodeElements | % { $_.Children | % { $_.ImplementedInterfaces } }


DTE           : System.__ComObject
Collection    : System.__ComObject
Name          : ICacheable
FullName      : ApplicationBase.Core.Interface.ICacheable
ProjectItem   : 
Kind          : 8
IsCodeType    : True
InfoLocation  : 2
Children      : 
Language      : {B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}
StartPoint    : 
EndPoint      : 
ExtenderNames : {ExternalLocation}
ExtenderCATID : 
Parent        : System.__ComObject
Namespace     : System.__ComObject
Bases         : System.__ComObject
Members       : System.__ComObject
Access        : 1
Attributes    : System.__ComObject
DocComment    : 
Comment       : 
DerivedTypes  : 
IsGeneric     : False
DataTypeKind  : 1
Parts         : 

【问题讨论】:

    标签: c# visual-studio powershell envdte


    【解决方案1】:

    我不确定包管理器控制台中的工作方式,但在 powershell 中,您可以使用 implementedinterfaces 属性检查编译(和加载)类型是否实现接口。前任。 array-type:

    #[array].ImplementedInterfaces.Contains([System.Collections.ICollection])
    [array].ImplementedInterfaces.Contains([type]"System.Collections.Icollection")
    True
    

    您可以通过以下方式查看所有已实现的接口:

    [array].ImplementedInterfaces
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     False    ICloneable
    True     False    IList
    True     False    ICollection
    True     False    IEnumerable
    True     False    IStructuralComparable
    True     False    IStructuralEquatable   
    

    【讨论】:

    • 我检查了一下,这也适用于包管理器控制台。这给了我想法。然后我应该让类型到达 ImplementedInterfaces 属性。我现在正在寻找它。
    • 你给了我这个想法然后我找到了解决方案。我添加了问题的解决方案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2010-10-04
    • 2022-01-22
    • 2013-02-14
    • 1970-01-01
    • 2011-08-23
    • 2014-08-24
    • 2011-06-25
    相关资源
    最近更新 更多