[英]c# Reflection object[] issue
我正在嘗試使用反射來創建從反射創建的對象數組,如下所示:
Client[] newArray = new Client[] {client1, client2};
我需要某種方式獲取Client
對象類型來創建對象,以便可以通過它。
任何幫助將不勝感激。
干杯,羅布
object clientObject = testAssembly.CreateInstance(".Testing_Automation.Client");
Type client = testAssembly.GetType(".Testing_Automation.Client");
// Create Client Object Array
傳遞給:
public Appointment(IEnumerable<Client> client, string time)
您應該使用Array.CreateInstance
方法:
Array arr = Array.CreateInstance(client, lengthOfArray);
arr.SetValue(client1, 0); // Fill in the array...
arr.SetValue(client2, 1);
要從數組中獲取IEnumerable<Client>
,如果在編譯時知道Client
類型,則可以使用(IEnumerable<Client>)arr
。 如果不這樣做,則很可能應該發布有關該方法調用的可能性的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.