亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

3 個問題,每個問題都相關...是否可以循環函數聲明、調用系統對象以及從字符串加載程序集?

3 個問題,每個問題都相關...是否可以循環函數聲明、調用系統對象以及從字符串加載程序集?

C#
慕容3067478 2023-04-29 18:11:27
我有一些我認為很酷的想法,但我不知道如何讓它全部發揮作用......所以,我會問......我有一個問題,他們都很相似,所以我覺得他們可能都有相同的答案,但我不確定。我試過使用 TypeDefinitions 、 Add-Type 、 New-Object 等,但這些都不起作用。我一直在搞亂所涉及的數學......盡管我盡了最大的努力?仍然沒有弄清楚如何讓它工作。我問過其他程序員,但他們可能都對我要完成的事情有錯誤的想法。所以這里...# Being able to call system objects, or system type objects from an array of common strings ( lets call each one it's own 'lego' ). ## Instead of ... #if ([ Security.Principal.WindowsPrincipal ][ Security.Principal.WindowsIdentity ]::GetCurrent()).IsInRole([ Security.Principal.WindowsBuiltInRole ]'Administrator'){ execute the script... }# You could do this...$0 = "." , "Windows" , "Security" , "Principal" , "Identity" , "BuiltInRole"$1 = -join $0[ 2 , 0 , 3 , 0 , 1 ]$2 = $0[ 3..5 ] | % { New-Object $1 + $_ }if (-join $2[0..1]::GetCurrent()).IsInRole($2[2]){ execute script }# Haven't gotten it working yet though, can't really call methods or types from a string as far as I know, but I know that SID strings are essentially calling types from a numerical index hence "S-1-5-21-...etc"```# Calling assemblies or assembly types from an array of strings (in the same manner as above for loading ASP.Net Assemblies - which are also system objects# Example of how they're loaded now....using System ;using System.Collections.Generic ;using System.Diagnostics ;using System.Linq ;using System.Threading.Tasks ;using Microsoft.AspNetCore.Mvc ;using securedigitsplus.Models ;# and how I'd like to load them in PowerShell and not even need the .cs files$0 = "System" , ".Collections.Generic" ,  ".Diagnostics" , ".Linq" , ".Threading.Tasks" , "#etc.....#"$1 = $0[0] , @( foreach ( $j in 1..4 ) | % { -join $0[0,$j] } )0..$1.count | % { using ( New-Object or Add-Type -TypeDeclaration $_ I've tried both with no success... }```我希望我已經對我正在嘗試做的事情給出了足夠好的解釋或示例。一般的想法是我希望能夠在某個時候在循環中創建 CmdLetBinding() ,特別是如果有一堆類似的并且 DefaultParameterSetName 似乎不是一個好的解決方案,但我'我現在只想用簡單的功能/開關來做到這一點。如果有人認為“你想在這里重新發明輪子”......我想這有一些優點......但我的看法是,“沒有什么是完美的,即使是其他人使用的輪子也不行......你有一個想要成長的想法。請問可以幫助實現這一目標的人?!?
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

從索引+連接字符串的數組中調用系統對象

如果您將.NET 類型名稱存儲在字符串中,

  • 轉換為[type]將它們轉換為類型對象;在 PSv5+ 中,您可以在此類類型對象上調用靜態::new()方法創建該類型的實例。

    • $typeName = 'System.DateTime'; $type = [type] $typeName; $instance = $type::new(0)

  • 或者(PSv4-),將類型名稱字符串傳遞給以New-Object創建一個實例

    • $typeName = 'System.DateTime'; New-Object $typeName -Args 0

注意:在這兩種情況下,您都需要知道要傳遞的適當構造函數參數(如果有)。


從字符串數組調用程序集或程序集類型

您的示例表明您希望將名稱空間導入代碼中,這樣您就可以更方便地僅通過名稱(例如,[Encoding])來引用類型,而不必使用類型的全名(例如,[System.Text.Encoding]),C# 將其實現為using <namespace>.

PowerShell 的等效功能是using namespace <type-name>,但它僅適用于文字類型名稱(如在 C# 中)。

此外,與類型字面量(如 )不同[Text.Encoding],省略System組件不是可選的,因此using namespace System.Text有效,但using namespace Text沒有(它被悄悄接受,但無效)。

Invoke-Expression您可以通過使用(否則應避免使用)來解決此問題:

$namespace = 'System.Collections'


Invoke-Expression "using namespace $namespace"


# Now you can access the types in namespace System.Collections by

# their mere name.

[ArrayList]? # short for: [System.Collections.ArrayList]

冗余相似函數調用的示例......減少冗余的想法


看起來您正在嘗試動態定義函數。


由于 PowerShell 函數公開為名為 的PowerShell驅動器Function:,因此您可以使用Set-Content動態定義函數:


$f = "Console" , "Warning" , "Success" , "Error"

$m = "White" , "Yellow" , "Green" , "Red"


0..($f.Count-1) | ForEach-Object {


? ? if ( $_ -eq 0 ) {??

? ? ? Function Log { param($MSG) $MSG | Out-File $LogFile -Append -Force }

? ? }

? ? else {

? ? ? $funcName = $f[$_]

? ? ? Set-Content Function:$funcName @"

? ? ? ? param(`$MSG)

? ? ? ? Write-Host `$MSG -ForegroundColor $($m[$_])

? ? ? ? Log `$MSG

"@

? ? }


}

這將定義函數Log和包裝函數Warning,Success和Error,它們在帶有彩色控制臺輸出的調用之前Log。


請注意使用可擴展的here-string ( @"<newline>...<newline>"@) 將函數體定義為多行字符串(為了便于閱讀),以及如何嵌入$字符。不應預先展開的必須轉義為`$.


查看完整回答
反對 回復 2023-04-29
  • 1 回答
  • 0 關注
  • 141 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號