Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
Powershell 如何查找类的内容?_Powershell_Class - Fatal编程技术网

Powershell 如何查找类的内容?

Powershell 如何查找类的内容?,powershell,class,Powershell,Class,在powershell中,我可以创建一个类 class testClass { [string]$testvar } 但我不知道,有没有办法得到这个?像 [testClass] | % {$_.ScriptCode} 会回来吗 class testClass { [string]$testvar } 我找不到任何与Get Member相关的操作,不过: ([testClass] | gm | % {$_.name}) | % {$([testClass]).$_} | findst

在powershell中,我可以创建一个类

class testClass {
  [string]$testvar
}
但我不知道,有没有办法得到这个?像

[testClass] | % {$_.ScriptCode}
会回来吗

class testClass {
  [string]$testvar
}
我找不到任何与
Get Member
相关的操作,不过:

([testClass] | gm | % {$_.name}) | % {$([testClass]).$_} | findstr "class testClass"
这就有了结果

bool IsSubclassOf(type type)
bool _Type.IsSubclassOf(type c)
DefinedTypes        : {testClass, testClass_<staticHelpers>}
testClass, ?powershell, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
DeclaringType             : testClass
ReflectedType             : testClass
ReflectedType          : testClass
DeclaringType          : testClass
ReflectedType          : testClass
DeclaringType          : testClass
DeclaringType              : testClass
ReflectedType              : testClass
DeclaringType              : testClass
ReflectedType              : testClass
DeclaringType             : testClass
ReflectedType             : testClass
DeclaringType    : testClass
ReflectedType    : testClass
ReflectedType          : testClass
DeclaringType          : testClass
ReflectedType          : testClass
DeclaringType          : testClass
DeclaringType              : testClass
ReflectedType              : testClass
DeclaringType              : testClass
ReflectedType              : testClass
DeclaringType    : testClass
ReflectedType    : testClass
testClass
testClass
UnderlyingSystemType       : testClass
FullName                   : testClass
AssemblyQualifiedName      : testClass, ?powershell, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Name                       : testClass
bool IsSubclassOf(类型)
布尔类型。发行类别(c类)
定义的类型:{testClass,testClass}
testClass,powershell,版本=0.0.0.0,区域性=中性,PublicKeyToken=null
DeclaringType:testClass
ReflectedType:testClass
ReflectedType:testClass
DeclaringType:testClass
ReflectedType:testClass
DeclaringType:testClass
DeclaringType:testClass
ReflectedType:testClass
DeclaringType:testClass
ReflectedType:testClass
DeclaringType:testClass
ReflectedType:testClass
DeclaringType:testClass
ReflectedType:testClass
ReflectedType:testClass
DeclaringType:testClass
ReflectedType:testClass
DeclaringType:testClass
DeclaringType:testClass
ReflectedType:testClass
DeclaringType:testClass
ReflectedType:testClass
DeclaringType:testClass
ReflectedType:testClass
测试类
测试类
UnderlineingSystemType:testClass
全名:testClass
AssemblyQualifiedName:testClass,?powershell,版本=0.0.0.0,区域性=neutral,PublicKeyToken=null
名称:testClass

请提供帮助

老实说,我不知道如何获取命令返回的实际源代码,但是如果您想查找未知类的属性,可以使用
Get Member
.GetType().properties()

我用另外两个属性和一个方法扩展了示例类

class testClass {
    [string]$name
    [int]$age
    [datetime]$birthday

    [testClass]SayHi(){
        return "Hello World!"
    }
}
$obj = [testClass]::new()
运行
Get Member
时,您将获得所有属性和方法。对于属性,您还可以获得它们期望的类型

$obj | Get-Member

   TypeName: testClass

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
SayHi       Method     testClass SayHi()
ToString    Method     string ToString()
age         Property   int age {get;set;}
birthday    Property   datetime birthday {get;set;}
name        Property   string name {get;set;}
或者,您可以使用
.GetType().GetProperties()
方法获取有关属性的其他信息

$obj.GetType().GetProperties() | Format-Table


MemberType Name     DeclaringType ReflectedType MetadataToken Module                         IsCollectible PropertyType    Attributes CanRead
---------- ----     ------------- ------------- ------------- ------                         ------------- ------------    ---------- -------
  Property name     testClass     testClass         385875969 RefEmit_InMemoryManifestModule          True System.String         None    True
  Property age      testClass     testClass         385875970 RefEmit_InMemoryManifestModule          True System.Int32          None    True
  Property birthday testClass     testClass         385875971 RefEmit_InMemoryManifestModule          True System.DateTime       None    True

老实说,我不知道如何获得命令返回的实际源代码,但如果要查找未知类的属性,可以使用
Get Member
.GetType().properties()

我用另外两个属性和一个方法扩展了示例类

class testClass {
    [string]$name
    [int]$age
    [datetime]$birthday

    [testClass]SayHi(){
        return "Hello World!"
    }
}
$obj = [testClass]::new()
运行
Get Member
时,您将获得所有属性和方法。对于属性,您还可以获得它们期望的类型

$obj | Get-Member

   TypeName: testClass

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
SayHi       Method     testClass SayHi()
ToString    Method     string ToString()
age         Property   int age {get;set;}
birthday    Property   datetime birthday {get;set;}
name        Property   string name {get;set;}
或者,您可以使用
.GetType().GetProperties()
方法获取有关属性的其他信息

$obj.GetType().GetProperties() | Format-Table


MemberType Name     DeclaringType ReflectedType MetadataToken Module                         IsCollectible PropertyType    Attributes CanRead
---------- ----     ------------- ------------- ------------- ------                         ------------- ------------    ---------- -------
  Property name     testClass     testClass         385875969 RefEmit_InMemoryManifestModule          True System.String         None    True
  Property age      testClass     testClass         385875970 RefEmit_InMemoryManifestModule          True System.Int32          None    True
  Property birthday testClass     testClass         385875971 RefEmit_InMemoryManifestModule          True System.DateTime       None    True