可能重复:
将类型动态传递给 <T>
我将如何做类似下面的事情? historyType不是 Adapt 中可识别的类型。
Type historyType = Type.GetType("Domain.MainBoundedContext.CALMModule.Aggregates.LocationAgg." + modifiedEntry.GetType().Name + "History");
ServiceLocationHistory history = adapter.Adapt<ServiceLocation, historyType>(modifiedEntry as ServiceLocation);
historyRepository.Add(history);
编辑:我最终这样做了:
ServiceLocationHistory history = adapter.GetType()
.GetMethod("Adapt", new Type[] { typeof(ServiceLocation) })
.MakeGenericMethod(typeof(ServiceLocation), typeof(ServiceLocationHistory))
.Invoke(adapter, new object[] { modifiedEntry as ServiceLocation })
as ServiceLocationHistory;