是否可以构建类型名称并将对象重铸为特定类型?目标类型将包含在同一个程序集中,并且将有数百个。 示例代码:
var isPropertyInfo = objCollection as PropertyInfo;
if (isPropertyInfo != null)
{
var propType = ((PropertyInfo)objCollection).PropertyType;
var containerType = StripContainerType(propType.FullName);
var itemType = StripItemType(propType.FullName);
var fullTypeName = containerType + "<" + itemType + ">";
// ex: "System.Collections.Generic.ICollection<Customer>"
var fullType = Type.GetType(itemType);
dynamic coll;
// now I want to do something *LIKE* (pseudocode-ish)
// coll = objCollection as fullType;
不要将最后一行视为有效代码。它就在我被困的地方。