我在p-调用一些API函数方面取得了一些成功,但现在一些看似简单的事情让我感到困惑。
此代码有效:
Add-Type -TypeDefinition @'
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
public class WPAPI1 {
[StructLayout(LayoutKind.Sequential)]
public struct POINT {
public long x;
public long y;
public POINT( long x, long y)
{
this.x = x;
this.y = y;
}
};
}
'@
输出和测试:
PS > Add-Type -TypeDefinition @'
>> using System;
>> using System.IO;
>> using System.Text;
>> using System.Runtime.InteropServices;
>>
>> public class WPAPI1 {
>>
>> [StructLayout(LayoutKind.Sequential)]
>> public struct POINT {
>> public long x;
>> public long y;
>>
>> public POINT( long x, long y)
>> {
>> this.x = x;
>> this.y = y;
>> }
>> };
>> }
>> '@
>>
WARNING: The generated type defines no public methods or properties.
PS > $p = [WPAPI1+POINT]::new(5,10)
PS > $p
x y
- -
5 10
但这段代码无法编译:
Add-Type -TypeDefinition @'
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
public class WPAPI2 {
StructLayout(LayoutKind.Sequential)]
public struct RECT {
public long left;
public long top;
public long right;
public long bottom;
public RECT(long left, long top, long right, long botton)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
};
}
'@
引发以下错误:
Add-Type : c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(8) : Method must have a
return type
c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(7) :
c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(8) : >>>
StructLayout(LayoutKind.Sequential)]
c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(9) : public struct RECT {
At line:1 char:1
+ Add-Type -TypeDefinition @'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
mpilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
mmand
Add-Type : c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(8) : Identifier expected
c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(7) :
c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(8) : >>>
StructLayout(LayoutKind.Sequential)]
c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(9) : public struct RECT {
At line:1 char:1
+ Add-Type -TypeDefinition @'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
mpilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
mmand
Add-Type : c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(8) : ; expected
c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(7) :
c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(8) : >>>
StructLayout(LayoutKind.Sequential)]
c:UserskeithAppDataLocalTemp3v2a5y04.0.cs(9) : public struct RECT {
At line:1 char:1
+ Add-Type -TypeDefinition @'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
mpilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
mmand
Add-Type : Cannot add type. Compilation errors occurred.
At line:1 char:1
+ Add-Type -TypeDefinition @'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeComm
and
PS >
关注第一个错误:Method must have a return type
,我的互联网搜索没有帮助。构造函数不应该需要返回类型,我已经对RECT
的大小写和拼写进行了三次检查。
我被难住了。请告知。。。
编辑:
修复了丢失的[
(本以为我的错误已经改变,但为时已晚(。之后,代码会产生不同的错误:
Add-Type : c:UserskeithAppDataLocalTempsaoucata.0.cs(17) : Warning as Error:
Assignment made to same variable; did you mean to assign something else?
c:UserskeithAppDataLocalTempsaoucata.0.cs(16) : {
c:UserskeithAppDataLocalTempsaoucata.0.cs(17) : >>> this.left = left
; this.top = top ; this.right = right ; this.bottom = bottom;
c:UserskeithAppDataLocalTempsaoucata.0.cs(18) : }
At line:1 char:1
+ Add-Type -TypeDefinition @'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
mpilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
mmand
Add-Type : c:UserskeithAppDataLocalTempsaoucata.0.cs(17) : Use of possibly
unassigned field 'bottom'
c:UserskeithAppDataLocalTempsaoucata.0.cs(16) : {
c:UserskeithAppDataLocalTempsaoucata.0.cs(17) : >>> this.left = left
; this.top = top ; this.right = right ; this.bottom = bottom;
c:UserskeithAppDataLocalTempsaoucata.0.cs(18) : }
At line:1 char:1
+ Add-Type -TypeDefinition @'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
mpilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
mmand
Add-Type : Cannot add type. Compilation errors occurred.
At line:1 char:1
+ Add-Type -TypeDefinition @'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeComm
and
我很感激你的帮助。这里的视障人士——必须放大很多——会让人失去"视觉";大画面";,可以这么说!:D
Add-Type -TypeDefinition @'
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
public class WPAPI2 {
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public long left;
public long top;
public long right;
public long bottom;
public RECT(long left, long top, long right, long bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
};
}
'@
注意:";[在StructuredLayout之前的"botton"与RECT签名中的"bottom