[英]C# pipe ( | ) equivalent in Powershell for Add-Type imported functions?
[英]PowerShell Add-Type Simple C# Structures -- One works and one doessn't
我在 p-Invoking 一些 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;
}
};
}
'@
Output 和測試:
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:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : Method must have a
return type
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(7) :
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : >>>
StructLayout(LayoutKind.Sequential)]
c:\Users\keith\AppData\Local\Temp\3v2a5y04.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:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : Identifier expected
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(7) :
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : >>>
StructLayout(LayoutKind.Sequential)]
c:\Users\keith\AppData\Local\Temp\3v2a5y04.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:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : ; expected
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(7) :
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : >>>
StructLayout(LayoutKind.Sequential)]
c:\Users\keith\AppData\Local\Temp\3v2a5y04.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:\Users\keith\AppData\Local\Temp\saoucata.0.cs(17) : Warning as Error:
Assignment made to same variable; did you mean to assign something else?
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(16) : {
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(17) : >>> this.left = left
; this.top = top ; this.right = right ; this.bottom = bottom;
c:\Users\keith\AppData\Local\Temp\saoucata.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:\Users\keith\AppData\Local\Temp\saoucata.0.cs(17) : Use of possibly
unassigned field 'bottom'
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(16) : {
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(17) : >>> this.left = left
; this.top = top ; this.right = right ; this.bottom = bottom;
c:\Users\keith\AppData\Local\Temp\saoucata.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 之前的“[”和 RECT 簽名中的“botto n ”與“botto m ”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.