我有一個函數,它使用反射從對象B設置對象A的屬性。 有一點,我需要實例化一個通用集合。但是,我無法得到它的工作。這裏是我現在有:使用反射創建一個通用列表
IList list = destProperty.PropertyType.GetGenericTypeDefinition()
.MakeGenericType(destProperty.PropertyType.GetGenericArguments())
.GetConstructor(Type.EmptyTypes)
.Invoke(null) as IList;
我想設置destProperty的值。它必須是列表 在運行時,destProperty的類型爲ICollection <>。我認爲發生的事情是因爲ICollection是一個接口,它沒有構造函數。那麼實例化它的正確方法是什麼?
謝謝!