最近有項目的個別需求需要使用.net中的一些東西來實現(xiàn),如其中需要用到泛型集合List
忽然想起之前有小伙伴在群里也問過這個問題,當時我好像給出了錯誤的答案,故總結(jié)一下順便糾正之前的錯誤。

看完這個激動不已,在System命名空間中通過Activator.CreateInstance 靜態(tài)方法進行創(chuàng)建,在.net函數(shù)選板中找到Invoke Node,然后在右鍵菜單中找到“Select Class >> .NET >> Browse...”,如下圖所示:

在彈出的對話框中,找到Assembly選中mscorlib(4.0.0),并在下面的列表中找到System,如下圖所示:

雙擊展開System命名空間,找到Activator,如下圖所示:
然后鼠標左鍵單擊Method,選擇CreateInstance(Type type),如下圖所示:


此時發(fā)現(xiàn)改方法需要傳入Type參數(shù),按圖索驥找到了System命名空間中的GetType(String)靜態(tài)方法,如下圖所示:

找到改靜態(tài)方法的方式同上述CreateInstance(Type type)一樣,這里不再贅述,如下圖所示:


此時發(fā)現(xiàn)改方法需要傳入typeName參數(shù),其實就是類型的程序集限定名稱,
文檔中給出了參考代碼,如下圖所示:
using System;
using System.Collections.Generic;
using System.Globalization;
public class Example
{
public static void Main()
{
Type t = typeof(String);
ShowTypeInfo(t);
t = typeof(List<>);
ShowTypeInfo(t);
var list = new List();
t = list.GetType();
ShowTypeInfo(t);
Object v = 12;
t = v.GetType();
ShowTypeInfo(t);
t = typeof(IFormatProvider);
ShowTypeInfo(t);
IFormatProvider ifmt = NumberFormatInfo.CurrentInfo;
t = ifmt.GetType();
ShowTypeInfo(t);
}
private static void ShowTypeInfo(Type t)
{
Console.WriteLine($"Name: {t.Name}");
Console.WriteLine($"Full Name: {t.FullName}");
Console.WriteLine($"ToString: {t}");
Console.WriteLine($"Assembly Qualified Name: {t.AssemblyQualifiedName}");
Console.WriteLine();
}
}
// The example displays output like the following:
// Name: String
// Full Name: System.String
// ToString: System.String
// Assembly Qualified Name: System.String, mscorlib, Version=4.0.0.0, Culture=neutr
// al, PublicKeyToken=b77a5c561934e089
//
// Name: List`1
// Full Name: System.Collections.Generic.List`1
// ToString: System.Collections.Generic.List`1[T]
// Assembly Qualified Name: System.Collections.Generic.List`1, mscorlib, Version=4.
// 0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
// Name: List`1
// Full Name: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4
// .0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
// ToString: System.Collections.Generic.List`1[System.String]
// Assembly Qualified Name: System.Collections.Generic.List`1[[System.String, mscor
// lib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorl
// ib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
// Name: Int32
// Full Name: System.Int32
// ToString: System.Int32
// Assembly Qualified Name: System.Int32, mscorlib, Version=4.0.0.0, Culture=neutra
// l, PublicKeyToken=b77a5c561934e089
//
// Name: IFormatProvider
// Full Name: System.IFormatProvider
// ToString: System.IFormatProvider
// Assembly Qualified Name: System.IFormatProvider, mscorlib, Version=4.0.0.0, Cult
// ure=neutral, PublicKeyToken=b77a5c561934e089
//
// Name: NumberFormatInfo
// Full Name: System.Globalization.NumberFormatInfo
// ToString: System.Globalization.NumberFormatInfo
// Assembly Qualified Name: System.Globalization.NumberFormatInfo, mscorlib, Versio
// n=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
我們在傳入改typeName參數(shù)時,傳入Full Name即可,如果你不知道你想創(chuàng)建的類型的Full Name可以先修改參考代碼并執(zhí)行,即可獲取。比如我想創(chuàng)建List

實際使用中我想創(chuàng)建的是List>對象,如下圖所示:

理解之后你就可以創(chuàng)建原本你以為無法創(chuàng)建的對象實例了。
審核編輯:劉清
-
LabVIEW
+關(guān)注
關(guān)注
2013文章
3681瀏覽量
344312
原文標題:如何在LabVIEW中創(chuàng)建.net中的List
文章出處:【微信號:LabVIEW QT 修煉之路,微信公眾號:LabVIEW QT 修煉之路】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
在OrCAD中輕松創(chuàng)建并調(diào)整shape的操作步驟詳解
VC# .Net中瀏覽Crystal Report
labview中 list 列表實時讀取問題
在LABVIEW中 WORD型數(shù)據(jù)怎么表示,或怎么創(chuàng)建
labview中調(diào)用.net的richtextbox,當內(nèi)容出現(xiàn)中文時,selectionstart不準確的問題,請問怎么解決?
在ENV中配置硬件RTC+軟件RTC的步驟簡析
LabVIEW中調(diào)用DLL的高級技巧后續(xù)資源包
LabVIEW8.5中文評估版軟件安裝步驟
鼠標HID例程(中)簡析
NANDFLASH在WINCE_NET系統(tǒng)中的應用設(shè)計
LabVIEW中虛擬采集卡的創(chuàng)建教程資料說明
labview與sql數(shù)據(jù)庫連接5種方法
巖土工程監(jiān)測中振弦采集儀的布設(shè)方案及實施步驟簡析

在LabVIEW中創(chuàng)建.net中List的步驟簡析
評論