其实,我使用的是PowerShell版本1.0的脚本调用从DLL文件的方法,并使用了以下code加载DLL文件到PowerShell中。 [System.Reflection.Assembly] ::的LoadFile(DLL路径) 成功loded
GAC版本位置 --- ------- -------- DLL虚假V2.0.50727位置
本类包含一个公共的默认构造函数。我试着用下面的code创建一个类的对象。
$ OBJ =新的对象namespce.classname和它引发以下错误。
新对象:异常调用.ctor和0的说法(S)的类型初始namespce.classname'引发了异常。 在行:1字符:18 + $ OBJ =新的对象<<<< namespce.classname + CategoryInfo:InvalidOperation:(:) [新-对象],MethodInvocationException + FullyQualifiedErrorId:ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand`
当我试图调用类的方法,无需创建对象时,它抛出下面的错误,即使类包含的方法。
PS C:\ WINDOWS \ SYSTEM32> [namespace.classname] ::方法() 方法调用失败,因为[namespace.classname]不包含名为'方法'方法。 在行:1字符:39 + namespace.classname] ::方法<<<< () + CategoryInfo:InvalidOperation:(方法:字符串)[],RuntimeException的 + FullyQualifiedErrorId:MethodNotFound解决方案
最有可能的方法是一个实例方法,这意味着你将需要有一个类的实例。你可以得到通过在类如公共默认构造函数:
$ OBJ =新的对象namespace.classname $ obj.Method()
也许唯一的公共consctructor的要求参数,如:
$ OBJ =新的对象namespace.classname -argstring_arg',7 $ obj.Method()
也许没有公共构造,但有一个静态的创建或分析方法,它返回一个实例,例如:
$的obj = [namespace.classname] ::创建() $ obj.Method()Actually, I am using a PowerShell version 1.0 script to call a method from a DLL file and used the following code to load the DLL file into PowerShell. [System.Reflection.Assembly]::LoadFile("path of dll") is loded successfully
GAC Version Location --- ------- -------- False v2.0.50727 location of dllThe class contains a public default constructor. I tried to create an object of the class using the below code.
$obj = new-object namespce.classnameAnd it throws the following error.
New-Object : Exception calling ".ctor" with "0" argument(s): "The type initializer for 'namespce.classname' threw an exception." At line:1 char:18 + $obj = new-object <<<< namespce.classname + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand`
When I tried to call the method of the class without creating the object, it throws the below error even though the class contains the method.
PS C:\Windows\system32> [namespace.classname]::method() Method invocation failed because [namespace.classname] doesn't contain a method named 'method'. At line:1 char:39 + [namespace.classname]::method <<<< () + CategoryInfo : InvalidOperation: (method:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound解决方案
Most likely the method is an instance method which means you will need to have an instance of the class. You can get that via a public default constructor on the class e.g.:
$obj = new-object namespace.classname $obj.Method()Perhaps the only public consctructor's require parameters e.g.:
$obj = new-object namespace.classname -arg 'string_arg',7 $obj.Method()Or maybe there are no public constructors but there is a static Create or Parse method that returns an instance e.g.:
$obj = [namespace.classname]::Create() $obj.Method()
更多推荐
如何从PowerShell 1.0中调用的DLL方法
发布评论