c# - 如何确定类型是否实现 Powershell 中的接口(interface)

标签 c# visual-studio powershell envdte

我的项目中有一个 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         : 

最佳答案

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

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

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

[array].ImplementedInterfaces

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    ICloneable
True     False    IList
True     False    ICollection
True     False    IEnumerable
True     False    IStructuralComparable
True     False    IStructuralEquatable   

关于c# - 如何确定类型是否实现 Powershell 中的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16622797/

相关文章:

c# - 无法合并两个 NET Standard 项目中的 NuGet 包传递依赖项版本

visual-studio - 如何创建基于 F# 的 Visual Studio 插件?

powershell - 如何在Powershell中为可执行文件构建参数列表?

C# Rhino 在第二次调用中使用硬编码参数模拟 stubmethod

c# - TryParse 其他类型

c# - ProcessStartInfo 卡在 "WaitForExit"上?为什么?

c# - 当 "externalizing"某些配置设置进入外部文件时行为不同

c# - 断点和异常停止正常工作,xamarin android

arrays - 访问 PSCustomObject Powershell 内的对象数组

powershell - 如何将一组脚本文件变成一个模块?