如何在 Powershell 中确定一个类型是否实现了接口

4

我在一个项目中有一个Member类。我通过在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         : 
1个回答

7

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

#[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属性。我正在寻找它。 - Barış Velioğlu
1
你给了我灵感,然后我找到了解决方案。我把解决方案添加到问题中。谢谢。 - Barış Velioğlu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接