簡體   English   中英

使用ObservableCollection的Powershell嵌入式C#

[英]powershell embedded c# using ObservableCollection

我正在嘗試使用可觀察性集合從c#代碼在powershell中添加一種類型。 我已經嘗試了一兩天了,但我什么都無法工作。

我試圖提出的是wpf的視圖模型,而不依賴任何種類的額外powershell模塊。

這是我最新提出的:

$code = @"

using System;
using System.Collections.ObjectModel;

namespace Test
{
    public class TestClass
    {
        public ObservableCollection<string> Items { get; set; }

        public TestClass()
        {
            Items = new ObservableCollection<string>();
        }
    }
}    

"@


Add-Type -TypeDefinition $code -ReferencedAssemblies @("WindowsBase") -Language CSharpVersion3

我無法克服以下錯誤:

Add-Type:c:\\ Users \\ ncollier \\ AppData \\ Local \\ Temp \\ wjq4ciwc.0.cs(9):找不到類型或名稱空間名稱“ ObservableCollection”(您是否缺少using指令或程序集引用? )

編輯我正在嘗試在powergui中運行此程序,但是如果我在powershell ISE中運行它,則可能有效...在powergui中是否可能做錯了什么?

嘗試:

Add-Type -TypeDefinition $code

在我的框中,我可以添加類型。

評論后編輯:

我發現使用powershell.exe.config (或ISE的powershell_ise.exe.config )加載.net 4.0時,無需添加ReferencedAssemblies(在此代碼中,其他情況下需要引用system.core或可能需要其他程序集引用)。 我懷疑PowerGui也會加載.net 4.0,但我可以對其進行測試。 您可以嘗試使用PowerGui:

 [appdomain]::currentdomain.getassemblies()

看看是否有程序集版本4.0.xxxx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM